经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Kubernetes » 查看文章
【Azure K8S | AKS】在AKS集群中创建 PVC(PersistentVolumeClaim)和 PV(PersistentVolume) 示例
来源:cnblogs  作者:路边两盏灯  时间:2023/8/4 9:02:00  对本文有异议

问题描述

在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 内容为:

  1. # the relates PV
  2. ---
  3. kind: PersistentVolume
  4. apiVersion: v1
  5. metadata:
  6. name: test-pv-001
  7. spec:
  8. capacity:
  9. storage: 200Gi
  10. azureDisk:
  11. diskName: test-pv-001-disk
  12. diskURI: >-
  13. /subscriptions/<subscriptions id>/resourceGroups/<resource gorup name>/providers/Microsoft.Compute/disks/test-pv-001-disk
  14. cachingMode: ReadWrite
  15. fsType: ''
  16. readOnly: false
  17. kind: Managed
  18. accessModes:
  19. - ReadWriteOnce
  20. persistentVolumeReclaimPolicy: Delete
  21. storageClassName: default
  22. volumeMode: Filesystem
  23. status:
  24. phase: Bound

以上yaml文件定义了名为 test-pv-001的PV, 大小为200GiB,使用的Disk在第一步中创建,然后把 Disk的 Resource ID 设置到 diskURI 。

使用 kubectl apply -f mypvtest.yaml 部署PV到AKS中

 

第三步:创建PVC

文件 mypvctest.yaml 的内容为:

  1. kind: PersistentVolumeClaim
  2. apiVersion: v1
  3. metadata:
  4. name: test-pvc-001
  5. spec:
  6. accessModes:
  7. - ReadWriteOnce
  8. resources:
  9. requests:
  10. storage: 200Gi
  11. volumeName: test-pv-001
  12. storageClassName: default
  13. volumeMode: Filesystem

使用 kubectl apply -f mypvctest.yaml 部署PVC到AKS中

查看PV, PVC状态

  • kubectl get pv
  • kubectl get pvc

 

第四步:创建Pod,并且指定PVC的mount路径

POD的yaml文件内容为:(文件名mypodtest.yaml)

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: mypod-pv-pvc-test
  5. spec:
  6. nodeSelector:
  7. kubernetes.io/os: linux
  8. containers:
  9. - image: mcr.microsoft.com/oss/nginx/nginx:1.15.5-alpine
  10. name: mypod-pv-pvc-test
  11. resources:
  12. requests:
  13. cpu: 100m
  14. memory: 128Mi
  15. limits:
  16. cpu: 250m
  17. memory: 256Mi
  18. volumeMounts:
  19. - name: testazure
  20. mountPath: /mnt/testazure
  21. volumes:
  22. - name: testazure
  23. persistentVolumeClaim:
  24. 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

 

原文链接:https://www.cnblogs.com/lulight/p/17604441.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号