一、基本概念
1、访问模式:
ReadWriteOnce:该卷可以被单个节点以读/写模式挂载
ReadOnlyMany:该卷可以被多个节点以只读模式挂载
ReadWriteMany:该卷可以被多个节点以读/写模式挂载
命令行中,访问模式缩写为:
RWO-ReadWriteOnce
ROX-ReadOnlyMany
RWX-ReadWriteMany
2、回收策略:
Retain(保留):手动回收
Recycle(回收):基本擦除(rm -rf )。注:目前新版本只有HostPath支持
Delete(删除):关联的存储资产将被删除
3、卷的状态:
Available(可用):一块空闲资源还没有被任何声明绑定
Bound(已绑定):卷已经被声明绑定
Released(已释放):声明被删除,但是资源还未被集群重新声明
Failed(失败):该卷的自动回收失败
二、实例,NFS的持久化演示
1、安装配置nfs服务端
[root@nginx02 /]# yum install -y nfs-common nfs-utils rpcbind [root@nginx02 ~]# mkdir /data1 [root@nginx02 ~]# chmod 666 /data1 [root@nginx02 ~]# chown nfsnobody /data1 [root@nginx02 /]# cat etc/exports /data1 *(rw,no_root_squash,no_all_squash,sync) /data2 *(rw,no_root_squash,no_all_squash,sync) /data3 *(rw,no_root_squash,no_all_squash,sync) /data4 *(rw,no_root_squash,no_all_squash,sync) [root@nginx02 /]# systemctl restart rpcbind [root@nginx02 /]# systemctl restart nfs
2、node节点安装nfs工具,所有node节点都要安装
[root@k8s-node01 ~]# yum -y install nfs-utils [root@k8s-node02 ~]# yum -y install nfs-utils
3、部署PV
[root@k8s-master01-etcd01 pv-statefulset]# cat nfsdata1.yaml apiVersion: v1 kind: PersistentVolume metadata: name: nfsdata1 spec: capacity: storage: 1Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain storageClassName: nfs nfs: path: /data1 server: 10.16.8.156 --- apiVersion: v1 kind: PersistentVolume metadata: name: nfsdata2 spec: capacity: storage: 1.5Gi accessModes: - ReadOnlyMany persistentVolumeReclaimPolicy: Retain storageClassName: nfs nfs: path: /data2 server: 10.16.8.156 --- apiVersion: v1 kind: PersistentVolume metadata: name: nfsdata3 spec: capacity: storage: 2Gi accessModes: - ReadWriteMany persistentVolumeReclaimPolicy: Retain storageClassName: nfs nfs: path: /data3 server: 10.16.8.156 --- apiVersion: v1 kind: PersistentVolume metadata: name: nfsdata4 spec: capacity: storage: 1Gi accessModes: - ReadWriteMany persistentVolumeReclaimPolicy: Retain storageClassName: nfs nfs: path: /data4 server: 10.16.8.156
[root@k8s-master01-etcd01 pv-statefulset]# kubectl apply -f nfsdata1.yaml [root@k8s-master01-etcd01 pv-statefulset]# kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE nfsdata1 1Gi RWO Retain Available data1 8m47s nfsdata2 1536Mi ROX Retain Available data2 49s nfsdata3 2Gi RWX Retain Available data3 49s nfsdata4 1Gi RWX Retain Available data4 49s
4、deployment使用pvc
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mypvc namespace: default spec: accessModes: ["ReadWriteMany"] storageClassName: "nfs" resources: requests: storage: 1Gi --- apiVersion: v1 kind: Service metadata: name: xiangwei-service labels: app: nginx spec: type: NodePort ports: - port: 80 targetPort: 80 nodePort: 30801 selector: app: xiangwei-web --- apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment namespace: default spec: replicas: 3 selector: matchLabels: app: xiangwei-web template: metadata: labels: app: xiangwei-web spec: nodeSelector: app: dev containers: - name: nginx image: nginx:1.15 volumeMounts: - name: wwwroot mountPath: /usr/share/nginx/html ports: - containerPort: 80 volumes: - name: wwwroot persistentVolumeClaim: claimName: mypvc
5、StatefulSet控制器使用pvc
apiVersion: v1 kind: Service metadata: name: nginx labels: app: nginx spec: ports: - port: 80 name: web clusterIP: None selector: app: nginx --- apiVersion: apps/v1 kind: StatefulSet metadata: name: web spec: selector: matchLabels: app: nginx serviceName: "nginx" replicas: 3 template: metadata: labels: app: nginx spec: nodeSelector: app: dev containers: - name: nginx image: nginx:1.15 ports: - containerPort: 80 name: web volumeMounts: - name: www mountPath: /usr/share/nginx/html volumeClaimTemplates: - metadata: name: www spec: accessModes: [ "ReadWriteOnce" ] storageClassName: "nfs" resources: requests: storage: 1Gi