介绍
阿里云Kubernetes集群目前支持NAS、OSS、CPFS等阿里云共享存储服务,并通过flexvolume进行挂载;
共享存储支持一个存储卷被多个Pod同时挂载,多个Pod可能同时修改相同数据,这时需要应用自行实现数据的同步功能;
NAS:适合于IO较高的应用,读写性能相对OSS高,可实现跨主机文件共享;如文件服务器;
OSS:适用于低IO服务,配置文件、图片、小视频等共享业务;
使用NAS卷创建应用
创建NAS存储盘
- 在NAS控制台创建一个NAS文件系统盘;
- 创建NAS挂载点,挂载点配置成集群所在的VPC网络;
- 可以在集群中一个节点挂载NAS卷测试是否可行;
创建PV
通过NAS卷创建PV资源类型,配置PV唯一性标签alicloud-pvname: pv-nas;
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-nas
labels:
alicloud-pvname: pv-nas
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteMany
flexVolume:
driver: "alicloud/nas"
options:
server: "***-**.cn-beijing.nas.aliyuncs.com"
path: "/k8s1"
vers: "4.0"
options支持的参数:
server:NAS的挂载点;
path:所想挂载NAS文件系统的目录,如果目录不存在会自动创建;
vers:使用NFS的版本,支持3.0、4.0;
mode:挂载目录后配置目录的访问权限,如果挂载的目录文件量很大,此操作会很慢,不建议配置;
options:挂载NAS时,附加的参数,可选;
创建应用
PVC中通过selector来显式指定所要绑定的pv;
NAS为共享存储,可以在Deployment中配置replicas为2;
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc-nas
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
selector:
matchLabels:
alicloud-pvname: pv-nas
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nas-static
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
volumeMounts:
- name: pvc-nas
mountPath: "/data"
volumes:
- name: pvc-nas
persistentVolumeClaim:
claimName: pvc-nas
共享存储验证
创建服务:
# kubectl create -f nas.yaml
# kubectl get pod
NAME READY STATUS RESTARTS AGE
nas-static-6bcf7895dd-ddf7t 1/1 Running 0 15m
nas-static-6bcf7895dd-mhv5w 1/1 Running 0 15m
NAS盘上创建临时文件:
# kubectl exec nas-static-6bcf7895dd-ddf7t ls /data
# kubectl exec nas-static-6bcf7895dd-mhv5w ls /data
# kubectl exec nas-static-6bcf7895dd-ddf7t touch /data/nas1
# kubectl exec nas-static-6bcf7895dd-ddf7t ls /data
nas1
# kubectl exec nas-static-6bcf7895dd-mhv5w ls /data
nas1
高可用验证
删除所有Pod:
# kubectl delete pod nas-static-6bcf7895dd-ddf7t nas-static-6bcf7895dd-mhv5w
pod "nas-static-6bcf7895dd-ddf7t" deleted
pod "nas-static-6bcf7895dd-mhv5w" deleted
# kubectl get pod
NAME READY STATUS RESTARTS AGE
nas-static-6bcf7895dd-5zgxz 0/1 ContainerCreating 0 3s
nas-static-6bcf7895dd-99ppg 0/1 ContainerCreating 0 3s
nas-static-6bcf7895dd-ddf7t 0/1 Terminating 0 18m
nas-static-6bcf7895dd-mhv5w 0/1 Terminating 0 18m
查看新创建Pod数据:
# kubectl exec nas-static-6bcf7895dd-5zgxz ls /data
nas1
# kubectl exec nas-static-6bcf7895dd-99ppg ls /data
nas1