环境准备
集群创建、依赖配置、CSI插件部署等请参考:CSI部署详解
创建动态PV
通过下面模板创建动态卷PV:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: disk-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 25Gi
storageClassName: alicloud-disk-ssd
storage:定义申请云盘大小,最小为20Gi;
storageClassName:定义创建云盘使用的存储类,目前主要支持下面4类云盘类型;
alicloud-disk-available:会通过efficiency、ssd、essd的顺序依次尝试创建云盘;
alicloud-disk-essd:创建essd类型云盘;
alicloud-disk-ssd:创建ssd类型云盘;
alicloud-disk-efficiency:创建高效云盘;
查看PVC、PV:默认生成的PV名字 = pvc + "-" + pvc-uid
# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
disk-pvc Bound pvc-5e90da27-a458-11e9-8dec-00163e0a6ecc 25Gi RWO alicloud-disk-ssd 9s
# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-5e90da27-a458-11e9-8dec-00163e0a6ecc 25Gi RWO Retain Bound default/disk-pvc alicloud-disk-ssd 8s
# kubectl describe pv pvc-5e90da27-a458-11e9-8dec-00163e0a6ecc
Name: pvc-5e90da27-a458-11e9-8dec-00163e0a6ecc
Labels: <none>
Annotations: pv.kubernetes.io/provisioned-by: diskplugin.csi.alibabacloud.com
Finalizers: [kubernetes.io/pv-protection]
StorageClass: alicloud-disk-ssd
Status: Bound
Claim: default/disk-pvc
Reclaim Policy: Retain
Access Modes: RWO
VolumeMode: Filesystem
Capacity: 25Gi
Node Affinity: <none>
Message:
Source:
Type: CSI (a Container Storage Interface (CSI) volume source)
Driver: diskplugin.csi.alibabacloud.com
VolumeHandle: d-bp190t0frv9044z60t79
ReadOnly: false
VolumeAttributes: storage.kubernetes.io/csiProvisionerIdentity=1562849418025-8081-diskplugin.csi.alibabacloud.com
type=cloud_ssd
Events: <none>
创建应用
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-disk
labels:
app: nginx
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
volumeMounts:
- name: disk-pvc
mountPath: "/data"
volumes:
- name: disk-pvc
persistentVolumeClaim:
claimName: disk-pvc
验证挂载、高可用
查看pod,验证云盘挂载成功,创建测试文件;
# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-disk-6d5659d745-vl4c4 1/1 Running 0 24s
# kubectl exec nginx-disk-6d5659d745-vl4c4 ls /data
lost+found
# kubectl exec nginx-disk-6d5659d745-vl4c4 mount | grep /data
/dev/vdc on /data type ext4 (rw,relatime,data=ordered)
# kubectl exec nginx-disk-6d5659d745-vl4c4 touch /data/test
# kubectl exec nginx-disk-6d5659d745-vl4c4 ls /data
lost+found
test
删除Pod,查看重建Pod是否数据稳定;
# kubectl delete pod nginx-disk-6d5659d745-vl4c4
pod "nginx-disk-6d5659d745-vl4c4" deleted
# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-disk-6d5659d745-nz2pv 1/1 Running 0 5s
# kubectl exec nginx-disk-6d5659d745-nz2pv ls /data
lost+found
test