环境准备
集群创建、依赖配置、CSI插件部署等请参考:CSI部署详解
创建NAS卷
到NAS控制台创建NAS盘:参考文档
到NAS控制台创建NAS挂载点:参考文档
注意:NAS挂载点需要和集群节点在同一个vpc内;
创建静态PV、PVC
通过下面模板创建静态卷PV、PVC:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nas-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: nas-csi-pv
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
csi:
driver: nasplugin.csi.alibabacloud.com
volumeHandle: 0790b4a325
volumeAttributes:
host: "0790b4a325-xyn4.cn-hangzhou.nas.aliyuncs.com"
path: "/csi"
vers: "3"
驱动类型为:nasplugin.csi.alibabacloud.com,表示使用阿里云NAS CSI插件;
volumeHandle:PV Handler,可以配置为一个随机值;
host、path、vers:分别表示NAS挂载点、挂载子目录、nas目标版本;
# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
nas-pvc Bound nas-csi-pv 5Gi RWO 34m
# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
nas-csi-pv 5Gi RWO Retain Bound default/nas-pvc 34m
创建应用
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-nas
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: nas-pvc
mountPath: "/data"
volumes:
- name: nas-pvc
persistentVolumeClaim:
claimName: nas-pvc
验证挂载、高可用
查看pod,验证NAS挂载成功,创建测试文件;
# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-nas-6744df4f6b-krqc2 1/1 Running 0 20m
# kubectl exec nginx-nas-6744df4f6b-krqc2 mount | grep nfs
0790b4a325-xyn4.cn-hangzhou.nas.aliyuncs.com:/csi on /data type nfs (rw,relatime,vers=3,rsize=1048576,wsize=1048576,namlen=255,hard,nolock,noresvport,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=192.168.1.252,mountvers=3,mountport=4002,mountproto=tcp,local_lock=all,addr=192.168.1.252)
# kubectl exec nginx-nas-6744df4f6b-krqc2 ls /data
# kubectl exec nginx-nas-6744df4f6b-krqc2 touch /data/test
# kubectl exec nginx-nas-6744df4f6b-krqc2 ls /data
test
删除Pod,查看重建Pod是否数据稳定;
# kubectl delete pod nginx-nas-6744df4f6b-krqc2
pod "nginx-nas-6744df4f6b-krqc2" deleted
# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-nas-6744df4f6b-d6ds9 1/1 Running 0 10s
# kubectl exec nginx-nas-6744df4f6b-d6ds9 ls /data
test