在 Azure Kubernetes 服务 (AKS) 中动态创建永久性卷并将其用于 Azure 磁盘Dynamically create and use a persistent volume with Azure disks in Azure Kubernetes Service (AKS)

永久性卷表示已经过预配可以用于 Kubernetes Pod 的存储块。A persistent volume represents a piece of storage that has been provisioned for use with Kubernetes pods. 永久性卷可供一个或多个 Pod 使用,并可动态或静态预配。A persistent volume can be used by one or many pods, and can be dynamically or statically provisioned. 本文介绍如何使用 Azure 磁盘动态创建永久性卷,以供 Azure Kubernetes 服务 (AKS) 群集中的单个 Pod 使用。This article shows you how to dynamically create persistent volumes with Azure disks for use by a single pod in an Azure Kubernetes Service (AKS) cluster.

备注

Azure 磁盘只能使用“访问模式”类型 ReadWriteOnce 进行装载,这使其只可供 AKS 中的一个节点使用。An Azure disk can only be mounted with Access mode type ReadWriteOnce, which makes it available to one node in AKS. 如果需要在多个节点之间共享永久性卷,请使用 Azure 文件存储If you need to share a persistent volume across multiple nodes, use Azure Files.

有关 Kubernetes 卷的详细信息,请参阅 AKS 中应用程序的存储选项For more information on Kubernetes volumes, see Storage options for applications in AKS.

准备阶段Before you begin

本文假定你拥有现有的 AKS 群集。This article assumes that you have an existing AKS cluster. 如果需要 AKS 群集,请参阅 AKS 快速入门使用 Azure CLI使用 Azure 门户If you need an AKS cluster, see the AKS quickstart using the Azure CLI or using the Azure portal.

还需安装并配置 Azure CLI 2.0.59 或更高版本。You also need the Azure CLI version 2.0.59 or later installed and configured. 运行  az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅 安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

内置存储类Built-in storage classes

存储类用于定义使用永久性卷动态创建存储单位的方式。A storage class is used to define how a unit of storage is dynamically created with a persistent volume. 有关 Kubernetes 存储类的详细信息,请参阅 Kubernetes 存储类For more information on Kubernetes storage classes, see Kubernetes Storage Classes.

每个 AKS 群集包含四个预先创建的存储类,其中两个配置为使用 Azure 磁盘:Each AKS cluster includes four pre-created storage classes, two of them configured to work with Azure disks:

  • default 存储类可预配标准 SSD Azure 磁盘。The default storage class provisions a standard SSD Azure disk.
    • 标准存储由标准 SSD 提供支持,可以在交付可靠性能的同时提供经济高效的存储。Standard storage is backed by Standard SSDs and delivers cost-effective storage while still delivering reliable performance.
  • managed-premium 存储类可预配高级 Azure 磁盘。The managed-premium storage class provisions a premium Azure disk.
    • 高级磁盘由基于 SSD 的高性能、低延迟磁盘提供支持。Premium disks are backed by SSD-based high-performance, low-latency disk. 完美适用于运行生产工作负荷的 VM。Perfect for VMs running production workload. 如果群集中的 AKS 节点使用高级存储,请选择 managed-premium 类。If the AKS nodes in your cluster use premium storage, select the managed-premium class.

如果使用默认存储类之一,则创建存储类后将无法更新卷大小。If you use one of the default storage classes, you can't update the volume size after the storage class is created. 若要能够在创建存储类后更新卷大小,请将行 allowVolumeExpansion: true 添加到其中一个默认存储类,或者也可以创建自己的自定义存储类。To be able to update the volume size after a storage class is created, add the line allowVolumeExpansion: true to one of the default storage classes, or you can create you own custom storage class. 注意,不支持减小 PVC 的大小(以防数据丢失)。Note that it's not supported to reduce the size of a PVC (to prevent data loss). 可以使用 kubectl edit sc 命令编辑现有存储类。You can edit an existing storage class by using the kubectl edit sc command.

例如,如果要使用大小为 4 TiB 的磁盘,需要创建一个定义 cachingmode: None 的存储类,因为磁盘缓存不支持 4 TiB 及更大的磁盘For example, if you want to use a disk of size 4 TiB, you must create a storage class that defines cachingmode: None because disk caching isn't supported for disks 4 TiB and larger.

有关存储类和创建自己的存储类的详细信息,请参阅 AKS 中应用程序的存储选项For more information about storage classes and creating your own storage class, see Storage options for applications in AKS.

使用 kubectl get sc 命令查看预先创建的存储类。Use the kubectl get sc command to see the pre-created storage classes. 以下示例显示了 AKS 群集中可用的预先创建存储类:The following example shows the pre-create storage classes available within an AKS cluster:

$ kubectl get sc

NAME                PROVISIONER                AGE
default (default)   kubernetes.io/azure-disk   1h
managed-premium     kubernetes.io/azure-disk   1h

备注

永久卷声明在 GiB 中指定,但 Azure 托管磁盘由 SKU 针对特定大小计费。Persistent volume claims are specified in GiB but Azure managed disks are billed by SKU for a specific size. 这些 SKU 的范围从用于 S4 或 P4 磁盘的 32 GiB 到用于 S80 或 P80 磁盘的 32 TiB(预览版)。These SKUs range from 32GiB for S4 or P4 disks to 32TiB for S80 or P80 disks (in preview). 高级托管磁盘的吞吐量和 IOPS 性能取决于 SKU 和 AKS 群集中节点的实例大小。The throughput and IOPS performance of a Premium managed disk depends on the both the SKU and the instance size of the nodes in the AKS cluster. 有关详细信息,请参阅托管磁盘的定价和性能For more information, see Pricing and Performance of Managed Disks.

创建永久性卷声明Create a persistent volume claim

永久卷声明 (PVC) 用于基于存储类自动预配存储。A persistent volume claim (PVC) is used to automatically provision storage based on a storage class. 在这种情况下,PVC 可以使用预先创建的存储类之一创建标准或高级 Azure 托管磁盘。In this case, a PVC can use one of the pre-created storage classes to create a standard or premium Azure managed disk.

创建名为 azure-premium.yaml 的文件,并将其复制到以下清单中。Create a file named azure-premium.yaml, and copy in the following manifest. 该声明请求名为 azure-managed-disk、大小为 5 GB、具有 ReadWriteOnce 访问权限的磁盘。The claim requests a disk named azure-managed-disk that is 5GB in size with ReadWriteOnce access. managed-premium 存储类指定为存储类。The managed-premium storage class is specified as the storage class.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: azure-managed-disk
spec:
  accessModes:
  - ReadWriteOnce
  storageClassName: managed-premium
  resources:
    requests:
      storage: 5Gi

提示

若要创建使用标准存储的磁盘,请使用 storageClassName: default 而不是 managed-premiumTo create a disk that uses standard storage, use storageClassName: default rather than managed-premium.

使用 kubectl apply 命令创建永久性卷声明,并指定 azure-premium.yaml 文件:Create the persistent volume claim with the kubectl apply command and specify your azure-premium.yaml file:

$ kubectl apply -f azure-premium.yaml

persistentvolumeclaim/azure-managed-disk created

使用永久性卷Use the persistent volume

创建永久性卷声明并成功预配磁盘以后,即可创建可以访问磁盘的 Pod。Once the persistent volume claim has been created and the disk successfully provisioned, a pod can be created with access to the disk. 以下清单创建的基本 NGINX Pod 使用名为 azure-managed-disk 的永久性卷声明将 Azure 磁盘装载到 /mnt/azure 路径。The following manifest creates a basic NGINX pod that uses the persistent volume claim named azure-managed-disk to mount the Azure disk at the path /mnt/azure. 对于 Windows Server 容器,请使用 Windows 路径约定指定 mountPath,例如“D:”。For Windows Server containers, specify a mountPath using the Windows path convention, such as 'D:'.

创建名为 azure-pvc-disk.yaml 的文件,并将其复制到以下清单中。Create a file named azure-pvc-disk.yaml, and copy in the following manifest.

kind: Pod
apiVersion: v1
metadata:
  name: mypod
spec:
  containers:
  - name: mypod
    image: dockerhub.azk8s.cn/library/nginx:1.15.5
    resources:
      requests:
        cpu: 100m
        memory: 128Mi
      limits:
        cpu: 250m
        memory: 256Mi
    volumeMounts:
    - mountPath: "/mnt/azure"
      name: volume
  volumes:
    - name: volume
      persistentVolumeClaim:
        claimName: azure-managed-disk

使用 kubectl apply 命令创建 Pod,如以下示例所示:Create the pod with the kubectl apply command, as shown in the following example:

$ kubectl apply -f azure-pvc-disk.yaml

pod/mypod created

现在你有一个正在运行的 Pod,其中 Azure 磁盘被装载到 /mnt/azure 目录中。You now have a running pod with your Azure disk mounted in the /mnt/azure directory. 通过 kubectl describe pod mypod 检查 Pod 时可以看到此配置,如以下精简示例所示:This configuration can be seen when inspecting your pod via kubectl describe pod mypod, as shown in the following condensed example:

$ kubectl describe pod mypod

[...]
Volumes:
  volume:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  azure-managed-disk
    ReadOnly:   false
  default-token-smm2n:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-smm2n
    Optional:    false
[...]
Events:
  Type    Reason                 Age   From                               Message
  ----    ------                 ----  ----                               -------
  Normal  Scheduled              2m    default-scheduler                  Successfully assigned mypod to aks-nodepool1-79590246-0
  Normal  SuccessfulMountVolume  2m    kubelet, aks-nodepool1-79590246-0  MountVolume.SetUp succeeded for volume "default-token-smm2n"
  Normal  SuccessfulMountVolume  1m    kubelet, aks-nodepool1-79590246-0  MountVolume.SetUp succeeded for volume "pvc-faf0f176-8b8d-11e8-923b-deb28c58d242"
[...]

备份永久性卷Back up a persistent volume

若要备份持久卷中的数据,请为该卷的托管磁盘创建快照。To back up the data in your persistent volume, take a snapshot of the managed disk for the volume. 然后,可以使用此快照创建还原的磁盘,并通过还原数据的方式附加到 Pod。You can then use this snapshot to create a restored disk and attach to pods as a means of restoring the data.

首先,使用 kubectl get pvc 命令获取卷名称,例如,获取 PVC 名称 azure-managed-diskFirst, get the volume name with the kubectl get pvc command, such as for the PVC named azure-managed-disk:

$ kubectl get pvc azure-managed-disk

NAME                 STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS      AGE
azure-managed-disk   Bound     pvc-faf0f176-8b8d-11e8-923b-deb28c58d242   5Gi        RWO            managed-premium   3m

此卷名称构成了基础 Azure 磁盘名称。This volume name forms the underlying Azure disk name. 使用 az disk list 查询磁盘 ID 并提供 PVC 卷名称,如以下示例所示:Query for the disk ID with az disk list and provide your PVC volume name, as shown in the following example:

$ az disk list --query '[].id | [?contains(@,`pvc-faf0f176-8b8d-11e8-923b-deb28c58d242`)]' -o tsv

/subscriptions/<guid>/resourceGroups/MC_MYRESOURCEGROUP_MYAKSCLUSTER_chinaeast2/providers/MicrosoftCompute/disks/kubernetes-dynamic-pvc-faf0f176-8b8d-11e8-923b-deb28c58d242

运行 az snapshot create,使用磁盘 ID 创建快照磁盘。Use the disk ID to create a snapshot disk with az snapshot create. 以下示例在 AKS 群集所在的同一资源组 (MC_myResourceGroup_myAKSCluster_chinaeast2) 中创建名为 pvcSnapshot 的快照。The following example creates a snapshot named pvcSnapshot in the same resource group as the AKS cluster (MC_myResourceGroup_myAKSCluster_chinaeast2). 如果在 AKS 群集无权访问的资源组中创建快照和还原磁盘,可能会遇到权限问题。You may encounter permission issues if you create snapshots and restore disks in resource groups that the AKS cluster does not have access to.

$ az snapshot create \
    --resource-group MC_myResourceGroup_myAKSCluster_chinaeast2 \
    --name pvcSnapshot \
    --source /subscriptions/<guid>/resourceGroups/MC_myResourceGroup_myAKSCluster_chinaeast2/providers/MicrosoftCompute/disks/kubernetes-dynamic-pvc-faf0f176-8b8d-11e8-923b-deb28c58d242

根据磁盘上的数据量,可能需要花费几分钟时间来创建快照。Depending on the amount of data on your disk, it may take a few minutes to create the snapshot.

还原并使用快照Restore and use a snapshot

若要还原磁盘并将其用于 Kubernetes Pod,请在使用 az disk create 创建磁盘时,将快照用作源。To restore the disk and use it with a Kubernetes pod, use the snapshot as a source when you create a disk with az disk create. 如果以后需要访问原始数据快照,此操作可保留原始资源。This operation preserves the original resource if you then need to access the original data snapshot. 以下示例基于名为 pvcSnapshot 的快照创建名为 pvcRestored 的磁盘:The following example creates a disk named pvcRestored from the snapshot named pvcSnapshot:

az disk create --resource-group MC_myResourceGroup_myAKSCluster_chinaeast2 --name pvcRestored --source pvcSnapshot

若要使用包含 Pod 的已还原磁盘,请指定清单中磁盘的 ID。To use the restored disk with a pod, specify the ID of the disk in the manifest. 使用 az disk show 命令获取磁盘 ID。Get the disk ID with the az disk show command. 以下示例获取上一步骤中创建的 pvcRestored 的磁盘 ID:The following example gets the disk ID for pvcRestored created in the previous step:

az disk show --resource-group MC_myResourceGroup_myAKSCluster_chinaeast2 --name pvcRestored --query id -o tsv

创建名为 azure-restored.yaml 的 Pod 清单,并指定上一步骤中获取的磁盘 URI。Create a pod manifest named azure-restored.yaml and specify the disk URI obtained in the previous step. 以下示例创建一个基本的 NGINX Web 服务器,其中的 mnt/azure 处已将还原的磁盘装载为卷:The following example creates a basic NGINX web server, with the restored disk mounted as a volume at /mnt/azure:

kind: Pod
apiVersion: v1
metadata:
  name: mypodrestored
spec:
  containers:
  - name: mypodrestored
    image: dockerhub.azk8s.cn/library/nginx:1.15.5
    resources:
      requests:
        cpu: 100m
        memory: 128Mi
      limits:
        cpu: 250m
        memory: 256Mi
    volumeMounts:
    - mountPath: "/mnt/azure"
      name: volume
  volumes:
    - name: volume
      azureDisk:
        kind: Managed
        diskName: pvcRestored
        diskURI: /subscriptions/<guid>/resourceGroups/MC_myResourceGroupAKS_myAKSCluster_chinaeast2/providers/Microsoft.Compute/disks/pvcRestored

使用 kubectl apply 命令创建 Pod,如以下示例所示:Create the pod with the kubectl apply command, as shown in the following example:

$ kubectl apply -f azure-restored.yaml

pod/mypodrestored created

可以使用 kubectl describe pod mypodrestored 查看 Pod 详细信息,例如,以下精简示例显示了卷信息:You can use kubectl describe pod mypodrestored to view details of the pod, such as the following condensed example that shows the volume information:

$ kubectl describe pod mypodrestored

[...]
Volumes:
  volume:
    Type:         AzureDisk (an Azure Data Disk mount on the host and bind mount to the pod)
    DiskName:     pvcRestored
    DiskURI:      /subscriptions/19da35d3-9a1a-4f3b-9b9c-3c56ef409565/resourceGroups/MC_myResourceGroupAKS_myAKSCluster_chinaeast2/providers/Microsoft.Compute/disks/pvcRestored
    Kind:         Managed
    FSType:       ext4
    CachingMode:  ReadWrite
    ReadOnly:     false
[...]

后续步骤Next steps

如需相关的最佳做法,请参阅在 AKS 中存储和备份的最佳做法For associated best practices, see Best practices for storage and backups in AKS.

详细了解使用 Azure 磁盘的 Kubernetes 永久性卷。Learn more about Kubernetes persistent volumes using Azure disks.