问题描述
在AKS集群中创建 PVC(PersistentVolumeClaim)和 PV(PersistentVolume) 示例
问题解答
在Azure Kubernetes Service(AKS)的官方网站中,关于存储的选项介绍中,并没有具体的yaml实例来创建PV, PVC。特别是使用自定义的Disk的情况。

本文将根据以上图片中的 Azure Managed Disk + Persistent Volume + Persistent Volume Claim (Storage Class 使用 default )+ Pod 方案,一一在AKS集群中创建。
第一步:通过Azure门户,创建 Managed Disk,把资源放置在与AKS 资源组中

1: 进入Managed Disk的创建页面:https://portal.azure.cn/#create/Microsoft.ManagedDisk
2: Resource Group 资源组选择与AKS系统资源相同,如 MC_xxxxx_xxxxxx_<region name>
3: Disk name,自定义字符串,如 test-pv-001-disk
4: Availability Zone 需要选择Zone 1,避免出现 “cannot be attached to the VM because it is not in zone '1'.”的错误。
其他则使用默认。创建成功后,进入下一步。
第二步:创建PV,并且使用第一步中的Disk URI
文件 mypvtest.yaml 内容为:
- # the relates PV
- ---
- kind: PersistentVolume
- apiVersion: v1
- metadata:
- name: test-pv-001
- spec:
- capacity:
- storage: 200Gi
- azureDisk:
- diskName: test-pv-001-disk
- diskURI: >-
- /subscriptions/<subscriptions id>/resourceGroups/<resource gorup name>/providers/Microsoft.Compute/disks/test-pv-001-disk
- cachingMode: ReadWrite
- fsType: ''
- readOnly: false
- kind: Managed
- accessModes:
- - ReadWriteOnce
- persistentVolumeReclaimPolicy: Delete
- storageClassName: default
- volumeMode: Filesystem
- status:
- phase: Bound
-
以上yaml文件定义了名为 test-pv-001的PV, 大小为200GiB,使用的Disk在第一步中创建,然后把 Disk的 Resource ID 设置到 diskURI 。
使用 kubectl apply -f mypvtest.yaml 部署PV到AKS中

第三步:创建PVC
文件 mypvctest.yaml 的内容为:
- kind: PersistentVolumeClaim
- apiVersion: v1
- metadata:
- name: test-pvc-001
- spec:
- accessModes:
- - ReadWriteOnce
- resources:
- requests:
- storage: 200Gi
- volumeName: test-pv-001
- storageClassName: default
- volumeMode: Filesystem
使用 kubectl apply -f mypvctest.yaml 部署PVC到AKS中

查看PV, PVC状态
- kubectl get pv
- kubectl get pvc

第四步:创建Pod,并且指定PVC的mount路径
POD的yaml文件内容为:(文件名mypodtest.yaml)
- apiVersion: v1
- kind: Pod
- metadata:
- name: mypod-pv-pvc-test
- spec:
- nodeSelector:
- kubernetes.io/os: linux
- containers:
- - image: mcr.microsoft.com/oss/nginx/nginx:1.15.5-alpine
- name: mypod-pv-pvc-test
- resources:
- requests:
- cpu: 100m
- memory: 128Mi
- limits:
- cpu: 250m
- memory: 256Mi
- volumeMounts:
- - name: testazure
- mountPath: /mnt/testazure
- volumes:
- - name: testazure
- persistentVolumeClaim:
- claimName: test-pvc-001
定义vlumnes的名称为 testazure并且指定PVC, 然后在Container中定义volumeMounts路径

至此, Managed Disk + PV + PVC + POD 试验成功。
附录:在创建POD中遇见的错误
查看POD一直处于Container Creating , 于是用 describe 查看POD日志
kubectl describe pod <pod name>
一:Warning FailedAttachVolume Disk Not Found
错误消息: AttachVolume.Attach failed for volume "lbpv-test-001" : rpc error: code = NotFound desc = Volume not found, failed with error: Retriable: false, RetryAfter: 0s, HTTPStatusCode: 404, RawError: {"error":{"code":"ResourceNotFound","message":"The Resource 'Microsoft.Compute/disks/lbpv-test-001' under resource group 'MC_xxx_xxx_xxx' was not found. For more details please go to https://aka.ms/ARMResourceNotFoundFix"}}
错误原因:没有提前创建 Disk资源。在Azure中创建PV中使用的Disk后,问题解决。
二:Warning FailedAttachVolume --- cannot be attached to the VM because it is not in zone '1'
错误消息: AttachVolume.Attach failed for volume "lbpv-test-001" : rpc error: code = Internal desc = Attach volume /subscriptions/xxxx/xxx/xxx/providers/Microsoft.Compute/disks/lbpv-test-001 to instance aks-xxxx-vmss000004 failed with Retriable: false, RetryAfter: 0s, HTTPStatusCode: 400, RawError: {\r
"error": {\r
"code": "BadRequest",\r
"message": "Disk xxxxxxx/lbpv-test-001 cannot be attached to the VM because it is not in zone '1'."
错误原因:创建Disk资源的时候,没有设置 Availability Zone 的值,根据消息提示,删除Disk后重建并且选择Zone值为1.
参考资料
在 Azure Kubernetes 服务 (AKS) 中通过 Azure 磁盘创建并使用卷 : https://docs.azure.cn/zh-cn/aks/azure-csi-disk-storage-provision#mount-disk-as-a-volume
Azure Kubernetes 服务 (AKS) 中的应用程序存储选项 : https://docs.azure.cn/zh-cn/aks/concepts-storage#persistent-volume-claims