如我們所知,Kubernetes通過 Volume 為集群中的容器提供存儲,通過Persistent Volume 和 Persistent Volume Claim實現Volume 的靜態供給和動態供給。Azure File和Azure Disk 也在Kubernetes 支持的動態供給 PV 的 Provisioner之列(如下圖:https://kubernetes.io/docs/concepts/storage/storage-classes/#provisioner),本篇文章就帶領大家操作一遍,如何動態創建Azure File 文件共享,以供集群中的多個Pod使用。
(1)准備工作:
按照上一篇文章中的步驟創建好一個AKS集群,並且升級Azure CLI到最新版本(AKS初體驗:創建集群並登錄到node節點);
登錄到你創建好的集群中,確認下各Node節點狀態是否正常:
(2)添加StorageClass:
創建一個azure-file-sc.yaml文件並編輯如下:
如上我們創建的這個Storage Class封裝的名稱(name)為azurefile, provisioner指定為kubernetes.io/azure-file,參數parameter部分,指定冗余形式,目前支持Standard的三張,其他暫不支持:
- Standard_LRS - standard locally redundant storage (LRS)
- Standard_GRS - standard geo-redundant storage (GRS)
- Standard_RAGRS - standard read-access geo-redundant storage (RA-GRS)
創建好之后執行 kubectl apply -f azure-file-sc.yaml。
(3)創建集群角色並綁定
作為Azure平台上的服務,AKS仍然使用的RBAC去控制集群的權限和安全。為了使Azure平台能夠創建所需要的存儲資源,這一步我們需要添加一個集群角色:
--- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: system:azure-cloud-provider rules: - apiGroups: [''] resources: ['secrets'] verbs: ['get','create'] --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: system:azure-cloud-provider roleRef: kind: ClusterRole apiGroup: rbac.authorization.k8s.io name: system:azure-cloud-provider subjects: - kind: ServiceAccount name: persistent-volume-binder namespace: kube-system
執行kubectl apply -f azure-pvc-roles.yaml 如下:
(4)創建PVC:
這一步就是動態申請存儲資源的文件了,命名這個PVC yaml文件為azure-file-pvc.yml,編輯如下,指定metadata為第(2)步中的StorageClassName,配置好訪問模式和容量,編輯好保存並運行,可以看到這個PVC已經被成功創建。
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: azurefile spec: accessModes: - ReadWriteMany storageClassName: azurefile resources: requests: storage: 5Gi
(5)使用並檢驗
首先我們創建一個pod,命令為mypod1.yml,這個pod運行的是一個busybox鏡像,通過PVC將Azure File mount 到容器的/datatest目錄中。
apiVersion: v1 kind: Pod metadata: name: mypod1 spec: containers: - image: busybox name: mycontainer1 volumeMounts: - mountPath: /datatest name: datatest args: - /bin/sh - -c - sleep 30000 volumes: - name: datatest persistentVolumeClaim: claimName: my-azurefile-pvc
然后依次執行:
kubectl apply -f mypod1.yaml #查看pod狀態 kubectl get pod -o wide #在pod里的datatest目錄下創建一個名字為hello的文件 kubectl exec mypod1 touch /datatest/hello
如上是截圖,在pod中創建完名字為hello的文件后,我們檢驗下這個文件有沒有更新到Azure File中,這里說明一下,AKS動態配置完后的AzureFile會默認創建在MC_的集群中,portal上找到這個存儲賬戶進去,找到Azure File下面的文件,如截圖,發現hello.txt已經存在了。
以上就是整個實驗過程,演示了AKS如何使用Azure File實現動態持久化存儲。希望對大家有用。