准备开始
在节点主机上创建一个 /mnt/data 目录:
mkdir -p /mnt/data
 
创建一个index.html文件
echo 'Hello from Kubernetes storage' > /mnt/data/index.html
 
创建PV
创建一个
hostPath类型的PersistentVolume。 Kubernetes 支持用于在单节点集群上开发和测试的
hostPath 类型的 PersistentVolume。 hostPath 类型的 PersistentVolume
使用节点上的文件或目录来模拟网络附加存储。
下面是pv-volume.yaml配置文件
apiVersion: v1	
kind: PersistentVolume	
metadata:	
  name: task-pv-volume		# pv 名称
  labels:
    type: local				
spec:
  storageClassName: manual		# 设置 PV 属于哪一个类,PVC 可以绑定此这个类
  capacity:				
    storage: 10Gi				# 指定卷的容量为10GB
  accessModes:
    - ReadWriteOnce				# 单个节点以读写方式安装
  hostPath:				# PV 类型为 hostPath
    path: "/mnt/data"		# 主机目录为 /mnt/data	
 
创建 PersistentVolume
kubectl apply -f pv-volume.yaml
 
查看PV
[root@k8s-master home]# kubectl get pv
NAME             CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
task-pv-volume   10Gi       RWO            Retain           Available           manual                  3s
 
RWO: 仅单节点可读写Retain: 回收策略手动回收Available:未被PVC绑定manual:指定存储类为 manual ,只有pvc选择 存储类为 manual 才能绑定PV
查看PV的相关信息
kubectl describe pv task-pv-volume
 
[root@k8s-master home]# kubectl describe pv task-pv-volume
Name:            task-pv-volume
Labels:          type=local
Annotations:     <none>
Finalizers:      [kubernetes.io/pv-protection]
StorageClass:    manual
Status:          Available
Claim:           
Reclaim Policy:  Retain
Access Modes:    RWO
VolumeMode:      Filesystem
Capacity:        10Gi
Node Affinity:   <none>
Message:         
Source:
    Type:          HostPath (bare host directory volume)
    Path:          /mnt/data
    HostPathType:  
Events:            <none>
 
创建PersistentVolumeClaim
下一步是创建一个 PersistentVolumeClaim。Pod 使用 PersistentVolumeClaim 来请求物理存储。申请10GB存储,请求模式为单节点可读写。
以下是 pv-claim.yaml配置文件
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual	# 绑定 和PV相同的 storageClass
  accessModes:
    - ReadWriteOnce		# 访问模式为单节点可读写
  resources:
    requests:
      storage: 10Gi		# 存储请求10GB,不能比PV卷大
 
创建PersistentVolumeClaim
kubectl apply -f pv-claim.yaml
 
[root@k8s-master home]# kubectl apply -f pv-claim.yaml
persistentvolumeclaim/task-pv-claim created
 
查看PV和PVC状态
[root@k8s-master home]# kubectl get pvc
NAME            STATUS   VOLUME           CAPACITY   ACCESS MODES   `STORAGECLASS`   AGE
task-pv-claim   Bound    task-pv-volume   10Gi       RWO            manual         16s
[root@k8s-master home]# kubectl get pv
NAME             CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                   STORAGECLASS   REASON   AGE
task-pv-volume   10Gi       RWO            Retain           Bound    default/task-pv-claim   manual                  13m
 
PV 和 PVC 属于相同的 STORAGECLASS
STATUS 都是 Bound 表示是PV 是有绑定PVC,而PVC是有绑定PV
在PV CLAIM 字段可以看出 贝PVC default/task-pv-claim 绑定着(default为命名空间)
创建Pod
Pod的存储卷绑定PVC
以下是pv.-pod.yaml的配置
apiVersion: v1
kind: Pod
metadata:
  name: task-pv-pod		
spec:
  volumes:
    - name: task-pv-storage			# 给存储卷命名
      persistentVolumeClaim:	# 绑定PVC,PVC名称为 task-pv-claim
        claimName: task-pv-claim
  containers:
    - name: task-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"		# 容器路径
          name: task-pv-storage		# 绑定 新命名的存储卷名称
 
kubectl create -f  pv.-pod.yaml
 
查看Pod更多相关信息
kubectl describe pod  task-pv-pod
 

访问Pod查看是否能访问到 index.html文件
kubectl get pod task-pv-pod  -o wide
 

 
清理
删除Pod
kubectl delete pod task-pv-pod
 
删除PVC
kubectl delete pvc task-pv-claim
 
删除PV
kubectl delete pv task-pv-volume
 
删除节点目录
rm -rf /mnt/data
 
扩展:
所有工作负载api的参考:https://kubernetes.io/zh-cn/docs/reference/kubernetes-api/workload-resources/
查看所有命令空间的资源
kubectl get all  --all-namespaces
 
查看指定命名空间下所有的资源
kubectl get all -n <namespace>
 
删除指定namespace所有资源
kubectl delete all --all -n <namespace>
                


















