在 Azure Kubernetes 服务 (AKS) 中动态创建永久性卷并将其用于 Azure 文件Dynamically create and use a persistent volume with Azure Files 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. 如果多个 Pod 需要同时访问同一存储卷,则可以使用 Azure 文件存储通过服务器消息块 (SMB) 协议进行连接。If multiple pods need concurrent access to the same storage volume, you can use Azure Files to connect using the Server Message Block (SMB) protocol. 本文介绍如何动态创建 Azure 文件共享以供 Azure Kubernetes 服务 (AKS) 群集中的多个 Pod 使用。This article shows you how to dynamically create an Azure Files share for use by multiple pods in an Azure Kubernetes Service (AKS) cluster.

有关 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.

创建存储类Create a storage class

存储类用于定义如何创建 Azure 文件共享。A storage class is used to define how an Azure file share is created. 节点资源组中会自动创建一个存储帐户来与存储类配合使用,以保存 Azure 文件共享。A storage account is automatically created in the node resource group for use with the storage class to hold the Azure file shares. 为 skuName 选择下列任一 Azure 存储冗余Choose of the following Azure storage redundancy for skuName:

  • Standard_LRS - 标准本地冗余存储 (LRS)Standard_LRS - standard locally redundant storage (LRS)

  • Standard_GRS - 标准异地冗余存储 (GRS)Standard_GRS - standard geo-redundant storage (GRS)

  • Standard_RAGRS - 标准读取访问异地冗余存储 (RA-GRS)Standard_RAGRS - standard read-access geo-redundant storage (RA-GRS)

  • Premium_LRS - 高级本地冗余存储 (LRS)Premium_LRS - premium locally redundant storage (LRS)

备注

Azure 文件存储在运行 Kubernetes 1.13 或更高版本的 AKS 群集中支持高级存储,最小高级文件共享为 100GBAzure Files support premium storage in AKS clusters that run Kubernetes 1.13 or higher, minimum premium file share is 100GB

有关 Azure 文件存储的 Kubernetes 存储类的详细信息,请参阅 Kubernetes 存储类For more information on Kubernetes storage classes for Azure Files, see Kubernetes Storage Classes.

创建名为 azure-file-sc.yaml 的文件,并将其复制到以下示例清单中。Create a file named azure-file-sc.yaml and copy in the following example manifest. 有关 mountOptions 的详细信息,请参阅 装载选项部分。For more information on mountOptions, see the Mount options section.

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: my-azurefile
provisioner: kubernetes.io/azure-file
mountOptions:
  - dir_mode=0777
  - file_mode=0777
  - uid=0
  - gid=0
  - mfsymlinks
  - cache=strict
  - actimeo=30
parameters:
  skuName: Standard_LRS

使用 kubectl apply 命令创建存储类:Create the storage class with the kubectl apply command:

kubectl apply -f azure-file-sc.yaml

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

永久性卷声明 (PVC) 使用存储类对象来动态预配 Azure 文件共享。A persistent volume claim (PVC) uses the storage class object to dynamically provision an Azure file share. 可使用以下 YAML 创建大小为 5 GB、访问权限为 ReadWriteMany 的永久性卷声明。The following YAML can be used to create a persistent volume claim 5 GB in size with ReadWriteMany access. 有关访问模式的详细信息,请参阅 Kubernetes 永久性卷文档。For more information on access modes, see the Kubernetes persistent volume documentation.

现在,创建名为 azure-file-pvc.yaml 的文件,并将其复制到以下 YAML 中。Now create a file named azure-file-pvc.yaml and copy in the following YAML. 请确保 storageClassName 与上一步骤中创建的存储类匹配:Make sure that the storageClassName matches the storage class created in the last step:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-azurefile
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: my-azurefile
  resources:
    requests:
      storage: 5Gi

备注

如果将 Premium_LRS SKU 用于存储类,则存储的最小值必须为 100Gi。If using the Premium_LRS sku for your storage class, the minimum value for storage must be 100Gi.

使用 kubectl apply 命令创建永久性卷声明:Create the persistent volume claim with the kubectl apply command:

kubectl apply -f azure-file-pvc.yaml

完成此步骤后,文件共享即创建完毕。Once completed, the file share will be created. 同时还会创建一个包含连接信息和凭据的 Kubernetes 机密。A Kubernetes secret is also created that includes connection information and credentials. 可以使用 kubectl get 命令查看 PVC 的状态:You can use the kubectl get command to view the status of the PVC:

$ kubectl get pvc my-azurefile

NAME           STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS      AGE
my-azurefile   Bound     pvc-8436e62e-a0d9-11e5-8521-5a8664dc0477   5Gi        RWX            my-azurefile      5m

使用永久性卷Use the persistent volume

以下 YAML 创建的 Pod 使用永久性卷声明 my-azurefile 将 Azure 文件共享装载到 /mnt/azure 路径 。The following YAML creates a pod that uses the persistent volume claim my-azurefile to mount the Azure file share at the /mnt/azure path. 对于 Windows Server 容器,请使用 Windows 路径约定指定 mountPath,例如“D:”。For Windows Server containers, specify a mountPath using the Windows path convention, such as 'D:'.

创建名为 azure-pvc-files.yaml 的文件,并将其复制到以下 YAML 中。Create a file named azure-pvc-files.yaml, and copy in the following YAML. 请确保 claimName 与上一步骤中创建的 PVC 匹配。Make sure that the claimName matches the PVC created in the last step.

kind: Pod
apiVersion: v1
metadata:
  name: mypod
spec:
  containers:
  - name: mypod
    image: mcr.microsoft.com/oss/nginx/nginx:1.15.5-alpine
    resources:
      requests:
        cpu: 100m
        memory: 128Mi
      limits:
        cpu: 250m
        memory: 256Mi
    volumeMounts:
    - mountPath: "/mnt/azure"
      name: volume
  volumes:
    - name: volume
      persistentVolumeClaim:
        claimName: my-azurefile

使用 kubectl apply 命令创建 Pod。Create the pod with the kubectl apply command.

kubectl apply -f azure-pvc-files.yaml

现有一个正在运行的 Pod,其中的 Azure 文件存储共享已装载到 /mnt/azure 目录中。You now have a running pod with your Azure Files share 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. 以下精简示例输出显示容器中装载的卷:The following condensed example output shows the volume mounted in the container:

Containers:
  mypod:
    Container ID:   docker://053bc9c0df72232d755aa040bfba8b533fa696b123876108dec400e364d2523e
    Image:          mcr.microsoft.com/oss/nginx/nginx:1.15.5-alpine
    Image ID:       docker-pullable://nginx@sha256:d85914d547a6c92faa39ce7058bd7529baacab7e0cd4255442b04577c4d1f424
    State:          Running
      Started:      Fri, 01 Mar 2019 23:56:16 +0000
    Ready:          True
    Mounts:
      /mnt/azure from volume (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-8rv4z (ro)
[...]
Volumes:
  volume:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  my-azurefile
    ReadOnly:   false
[...]

装载选项Mount options

对于 Kubernetes 版本 1.13.0 及更高版本,fileMode 和 dirMode 的默认值为 0777。The default value for fileMode and dirMode is 0777 for Kubernetes version 1.13.0 and above. 如果使用存储类动态创建永久性卷,则可以在存储类对象上指定装载选项。If dynamically creating the persistent volume with a storage class, mount options can be specified on the storage class object. 以下示例设置 0777The following example sets 0777:

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: my-azurefile
provisioner: kubernetes.io/azure-file
mountOptions:
  - dir_mode=0777
  - file_mode=0777
  - uid=0
  - gid=0
  - mfsymlinks
  - cache=strict
  - actimeo=30
parameters:
  skuName: Standard_LRS

后续步骤Next steps

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

有关存储类参数,请参阅动态预配For storage class parameters, see Dynamic Provision.

详细了解使用 Azure 文件的 Kubernetes 永久性卷。Learn more about Kubernetes persistent volumes using Azure Files.