环境准备
集群创建、依赖配置、CSI插件部署等请参考:CSI部署详解
创建云盘
需要创建按量付费云盘:参考文档
创建静态PV、PVC
通过云盘控制台创建云盘后,记录其DiskId:d-bp1fqy1enb2rtymx5g5y;
通过下面模板创建静态卷PV、PVC:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: disk-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 25Gi
selector:
matchLabels:
alicloud-pvname: static-disk-pv
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: disk-pv
labels:
alicloud-pvname: static-disk-pv
spec:
capacity:
storage: 25Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
csi:
driver: diskplugin.csi.alibabacloud.com
volumeHandle: d-bp1fqy1enb2rtymx5g5y
驱动类型为:diskplugin.csi.alibabacloud.com,表示使用阿里云云盘CSI插件;
volumeHandle:定义云盘ID;
# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
disk-pvc Bound disk-pv 25Gi RWO 8m15s
# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
disk-pv 25Gi RWO Retain Bound default/disk-pvc 8m16s
创建应用
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-f8ds6 1/1 Running 0 12m
# kubectl exec nginx-disk-6d5659d745-f8ds6 ls /data
lost+found
# kubectl exec nginx-disk-6d5659d745-f8ds6 mount | grep /data
/dev/vdb on /data type ext4 (rw,relatime,data=ordered)
# kubectl exec nginx-disk-6d5659d745-f8ds6 touch /data/test
# kubectl exec nginx-disk-6d5659d745-f8ds6 ls /data
lost+found
test
删除Pod,查看重建Pod是否数据稳定;
# kubectl delete pod nginx-disk-6d5659d745-f8ds6
pod "nginx-disk-6d5659d745-f8ds6" deleted
# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-disk-6d5659d745-55fcj 1/1 Running 0 53s
# kubectl exec nginx-disk-6d5659d745-55fcj ls /data
lost+found
test