本地存储
emptyDir
物理机随机生成目录,挂在到容器中,容器删除则挂在目录删除,不保留数据.
主要用于容器之间通信,共享数据.
[root@bjcy-200 volume]# cat volume1.yaml apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: nginx-v1 name: nginx-v1 spec: volumes: - name: volume1 emptyDir: {} containers: - image: harbor.tcc.com/public/nginx name: nginx-v1 volumeMounts: - name: volume1 mountPath: /dir resources: {} dnsPolicy: ClusterFirst restartPolicy: Always status: {}
hostPath
hostpath方式可以实现持久存储.但是不方便管理,和主机耦合性比较强,比较依赖主机.
[root@bjcy-200 volume]# cat hostpath.yaml apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: nginx-v1 name: nginx-v1 spec: volumes: - name: volume1 hostPath: path: /data/volume/nginx containers: - image: harbor.tcc.com/public/nginx name: nginx-v1 volumeMounts: - name: volume1 mountPath: /dir resources: {} dnsPolicy: ClusterFirst restartPolicy: Always status: {}
网络存储
kubernetes支持的网络存储: https://kubernetes.io/docs/concepts/storage/volumes/
NFS
nfs-server: /tmp *(rw,async,no_root_squash)
[root@bjcy-200 volume]# cat nfs.yaml apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: nginx-v1 name: nginx-v1 spec: volumes: - name: volume1 nfs: path: /tmp server: 172.17.0.2 containers: - image: harbor.tcc.com/public/nginx name: nginx-v1 volumeMounts: - name: volume1 mountPath: /dir resources: {} dnsPolicy: ClusterFirst restartPolicy: Always status: {}
iscsi
--- apiVersion: v1 kind: Pod metadata: name: iscsipd spec: containers: - name: iscsipd-rw image: kubernetes/pause volumeMounts: - mountPath: "/mnt/iscsipd" name: iscsipd-rw volumes: - name: iscsipd-rw iscsi: targetPortal: 10.0.2.15:3260 iqn: iqn.2001-04.com.example:storage.kube.sys1.xyz lun: 0 fsType: xfs readOnly: true
持久性存储
官网地址: https://kubernetes.io/docs/concepts/storage/persistent-volumes/
persistentvolumes 通过pv关联后端存储,pv对全局可见.用户通过创建vpc使用持久性存储, pvc不是全局可见.
PVC和PV是一一对应的。
主要参数
accessmode
pvc和pv关联主要通过: accessmode
参数 | 说明 |
ReadWriteOnce | 可读可写单node节点挂载 |
ReadOnlyMany | 可读权限单node节点挂载 |
ReadWriteMany | 可读可写多node节点挂载 |
pv回收策略
persistentVolumeReclaimPolicy
参数 | 说明 |
Recycle |
pvc被删除则删除数据,系统会生成一个pod把对应的数据删除掉. pv状态变成Available,此时pv可以被复用. |
Retain |
不会被删除数据,但是删除pvc后,pv仍然不可用. pv状态长期保持为Released |
storageClassName
类似name值,pvc要和pv关联 storageClassName 值必须相同.
storage
pvc指定的 storage 大小不能大于 pv storage 大小.
示例
pv
[root@bjcy-200 volume]# cat nfs-pv.yaml apiVersion: v1 kind: PersistentVolume metadata: name: nfs-pv spec: capacity: storage: 5Gi volumeMode: Filesystem accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Recycle storageClassName: slow mountOptions: - hard - nfsvers=4 nfs: path: /vdisk server: 172.16.0.141 检查 [root@bjcy-200 volume]# kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE nfs-pv 5Gi RWO Recycle Available slow 97
pvc
[root@bjcy-200 volume]# cat pvc.yaml apiVersion: v1 kind: PersistentVolumeClaim metadata: name: nfs-pvc2 spec: accessModes: - ReadWriteOnce volumeMode: Filesystem resources: requests: storage: 5Gi storageClassName: slow [root@bjcy-200 volume]# kubectl apply -f pvc.yaml persistentvolumeclaim/nfs-pvc2 created [root@bjcy-200 volume]# kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE nfs-pv 5Gi RWO Recycle Bound default/nfs-pvc2 slow 55s [root@bjcy-200 volume]# kubectl get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE nfs-pvc2 Bound nfs-pv 5Gi RWO slow 9s