在 Azure Kubernetes 服务 (AKS) 中通过 Azure Blob 存储创建和使用静态卷

基于容器的应用程序通常需要访问数据并将数据保存在外部数据卷中。 如果多个 Pod 需要并发访问同一存储卷,你可以使用 Azure Blob 存储通过 blobfuse 或网络文件系统 (NFS) 进行连接。

本文介绍如何创建 Azure Blob 存储容器或使用现有容器并将其附加到 AKS 中的 Pod。

有关 Kubernetes 卷的详细信息,请参阅 AKS 中应用程序的存储选项

准备阶段

静态预配参数

名称 步骤 示例 必需 默认值
volumeAttributes.resourceGroup 指定 Azure 资源组名称。 myResourceGroup 如果为空,驱动程序将使用与当前群集相同的资源组名称。
volumeAttributes.storageAccount 指定现有的 Azure 存储帐户名称。 storageAccountName
volumeAttributes.containerName 指定现有的容器名称。 container
volumeAttributes.protocol 指定 blobfuse 装载或 NFS v3 装载。 fusenfs fuse
--- 以下参数仅适用于 blobfuse --- --- ---
volumeAttributes.secretName 存储存储帐户名称和密钥的机密名称(仅适用于 SMB)。
volumeAttributes.secretNamespace 指定用于存储帐户密钥的机密的命名空间。 default Pvc 命名空间
nodeStageSecretRef.name 指定用于存储信息的机密名称(参阅以下示例):
azurestorageaccountkey
azurestorageaccountsastoken
msisecret
azurestoragespnclientsecret
现有 Kubernetes 机密名称
nodeStageSecretRef.namespace 指定机密的命名空间。 k8s 命名空间
--- 以下参数仅适用于 NFS 协议 --- --- ---
volumeAttributes.mountPermissions 指定装载的文件夹权限。 0777
--- 以下参数仅适用于 NFS VNet 设置 --- --- ---
vnetResourceGroup 指定托管虚拟网络的 VNet 资源组。 myResourceGroup 如果为空,驱动程序将使用 Azure 云配置文件中指定的 vnetResourceGroup 值。
vnetName 指定虚拟网络名称。 aksVNet 如果为空,驱动程序将使用 Azure 云配置文件中指定的 vnetName 值。
subnetName 指定代理节点的现有子网名称。 aksSubnet 如果为空,驱动程序将使用 Azure 云配置文件中的 subnetName 值。
--- 以下参数仅适用于功能:blobfuse
托管标识和服务主体名称身份验证
--- --- ---
volumeAttributes.AzureStorageAuthType 指定身份验证类型。 KeySASMSISPN Key
volumeAttributes.AzureStorageIdentityClientID 指定标识客户端 ID。
volumeAttributes.AzureStorageIdentityObjectID 指定标识对象 ID。
volumeAttributes.AzureStorageIdentityResourceID 指定标识资源 ID。
volumeAttributes.MSIEndpoint 指定 MSI 终结点。
volumeAttributes.AzureStorageSPNClientID 指定 Azure 服务主体名称 (SPN) 客户端 ID。
volumeAttributes.AzureStorageSPNTenantID 指定 Azure SPN 租户 ID。
volumeAttributes.AzureStorageAADEndpoint 指定 Azure Active Directory (Azure AD) 终结点。
--- 以下参数仅适用于功能:blobfuse 从密钥保管库读取帐户密钥或 SAS 令牌 --- --- ---
volumeAttributes.keyVaultURL 指定 Azure Key Vault DNS 名称。 {vault-name}.vault.azure.cn
volumeAttributes.keyVaultSecretName 指定 Azure Key Vault 机密名称。 现有 Azure Key Vault 机密名称。
volumeAttributes.keyVaultSecretVersion Azure Key Vault 机密版本。 现有版本 如果为空,驱动程序将使用当前版本。

创建 Blob 存储容器

创建用于 AKS 的 Azure Blob 存储资源时,可以在节点资源组中创建该资源。 此方法允许 AKS 群集访问和管理 Blob 存储资源。 如果改为在单独的资源组中创建 Blob 存储资源,则必须向集群的 Azure Kubernetes 服务托管标识授予 Blob 存储资源组的参与者角色。

对于本文,请在节点资源组中创建容器。 首先,使用 az aks show 命令获取资源组名称并添加 --query nodeResourceGroup 查询参数。 以下示例在名为 myResourceGroup 的资源组中获取名为 myAKSCluster 的 AKS 群集的节点资源组:

az aks show --resource-group myResourceGroup --name myAKSCluster --query nodeResourceGroup -o tsv

该命令的输出类似于以下示例:

MC_myResourceGroup_myAKSCluster_chinaeast2

接下来,按照管理 Blob 存储中的步骤创建一个用于存储 Blob 的容器来授权访问,然后创建容器。

使用 NFS 将 Blob 存储装载为卷

如果通过 NFS v3 协议装载 Blob 存储,则不使用帐户密钥进行身份验证。 AKS 群集需要驻留在与代理节点位置相同或对等的虚拟网络中。 保护存储帐户中的数据的唯一方法是使用虚拟网络和其他网络安全设置。 若要详细了解如何设置对存储帐户的 NFS 访问,请参阅使用网络文件系统 (NFS) 3.0 协议装载 Blob 存储

以下示例演示如何使用 NFS 协议将 Blob 存储容器装载为永久性卷。

  1. 创建名为 pv-blob-nfs.yaml 的文件,并将其复制到以下 YAML 中。 在 storageClass 下,更新 resourceGroupstorageAccountcontainerName

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv-blob
    spec:
      capacity:
        storage: 10Gi
      accessModes:
        - ReadWriteMany
      persistentVolumeReclaimPolicy: Retain  # If set as "Delete" container would be removed after pvc deletion
      storageClassName: azureblob-nfs-premium
      csi:
        driver: blob.csi.azure.com
        readOnly: false
        # make sure this volumeid is unique in the cluster
        # `#` is not allowed in self defined volumeHandle
        volumeHandle: unique-volumeid
        volumeAttributes:
          resourceGroup: resourceGroupName
          storageAccount: storageAccountName
          containerName: containerName
          protocol: nfs
    
  2. 运行以下命令,使用 kubectl create 命令引用之前创建的 YAML 文件来创建永久性卷:

    kubectl create -f pv-blob-nfs.yaml
    
  3. 使用 PersistentVolume 创建一个 pvc-blob-nfs.yaml 文件。 例如:

    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
      name: pvc-blob
    spec:
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 10Gi
      volumeName: pv-blob
      storageClassName: azureblob-nfs-premium
    
  4. 运行以下命令,使用 kubectl create 命令引用之前创建的 YAML 文件来创建永久性卷声明:

    kubectl create -f pvc-blob-nfs.yaml
    

使用 Blobfuse 将 Blob 存储装载为卷

Kubernetes 需要凭据(即 Azure 访问密钥或 SAS 令牌)才能访问之前创建的 Blob 存储容器。 这些凭据存储在 Kubernetes 机密中,创建 Kubernetes Pod 时将引用它。

  1. 使用 kubectl create secret command 创建机密。 可以使用 Kubernetes 机密共享访问签名 (SAS) 令牌进行身份验证。

    以下示例创建一个名为 azure-secret 的机密对象,并填充 azurestorageaccountname 和 azurestorageaccountkey。 你需要提供现有 Azure 存储帐户的帐户名和密钥。

    kubectl create secret generic azure-secret --from-literal azurestorageaccountname=NAME --from-literal azurestorageaccountkey="KEY" --type=Opaque
    
  2. 创建 pv-blobfuse.yaml 文件。 在 volumeAttributes 下,更新 containerName。 在 nodeStateSecretRef 下,使用之前创建的机密对象的名称更新 name。 例如:

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv-blob
    spec:
      capacity:
        storage: 10Gi
      accessModes:
        - ReadWriteMany
      persistentVolumeReclaimPolicy: Retain  # If set as "Delete" container would be removed after pvc deletion
      storageClassName: azureblob-fuse-premium
      mountOptions:
        - -o allow_other
        - --file-cache-timeout-in-seconds=120
      csi:
        driver: blob.csi.azure.com
        readOnly: false
        # make sure this volumeid is unique in the cluster
        # `#` is not allowed in self defined volumeHandle
        volumeHandle: unique-volumeid
        volumeAttributes:
          containerName: containerName
        nodeStageSecretRef:
          name: azure-secret
          namespace: default
    
  3. 运行以下命令,使用 kubectl create 命令引用之前创建的 YAML 文件来创建永久性卷:

    kubectl create -f pv-blobfuse.yaml
    
  4. 使用 PersistentVolume 创建一个 pvc-blobfuse.yaml 文件。 例如:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: pvc-blob
    spec:
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 10Gi
      volumeName: pv-blob
      storageClassName: azureblob-fuse-premium
    
  5. 运行以下命令,使用 kubectl create 命令引用之前创建的 YAML 文件来创建永久性卷声明:

    kubectl create -f pvc-blobfuse.yaml
    

使用永久性卷

以下 YAML 创建一个 Pod,该 Pod 使用前面创建的名为 pvc-blob 的永久性卷或永久性卷声明在“/mnt/blob”路径装载 Azure Blob 存储。

  1. 创建名为 nginx-pod-blob.yaml 的文件,并将其复制到以下 YAML 中。 确保在为 NFS 或 Blobfuse 创建永久性卷时,claimName 与上一步中创建的 PVC 相匹配。

    kind: Pod
    apiVersion: v1
    metadata:
      name: nginx-blob
    spec:
      nodeSelector:
        "kubernetes.io/os": linux
      containers:
        - image: mcr.azk8s.cn/oss/nginx/nginx:1.17.3-alpine
          name: nginx-blob
          volumeMounts:
            - name: blob01
              mountPath: "/mnt/blob"
      volumes:
        - name: blob01
          persistentVolumeClaim:
            claimName: pvc-blob
    
  2. 运行以下命令,使用 kubectl create 命令引用之前创建的 YAML 文件来创建 Pod 并装载 PVC:

    kubectl create -f nginx-pod-blob.yaml
    
  3. 运行以下命令来创建与 Pod 的交互式 shell 会话,从而确认 Blob 存储已装载:

    kubectl exec -it nginx-blob -- df -h
    

    命令的输出如下例所示:

    Filesystem      Size  Used Avail Use% Mounted on
    ...
    blobfuse         14G   41M   13G   1% /mnt/blob
    ...
    

后续步骤