你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

为 Azure Kubernetes 服务预配 Azure NetApp 文件 SMB 卷

为 Azure Kubernetes 服务配置 Azure NetApp 文件后,可为 Azure Kubernetes 服务预配 Azure NetApp 文件卷。

Azure NetApp 文件支持使用 NFS(NFSv3 或 NFSv4.1)、SMB 和双重协议(NFSv3 和 SMB,或 NFSv4.1 和 SMB)的卷。

静态配置使用 SMB 卷的应用程序

本部分介绍如何在 Azure NetApp 文件上创建 SMB 卷,以及如何以静态方式向 Kubernetes 公开卷,以供容器化应用程序使用。

创建 SMB 卷

  1. 定义变量供以后使用。 将 myresourcegroupmylocationmyaccountnamemypool1premiummyfilepathmyvolsizemyvolnamevirtnetid 替换为适合你的环境的值。 文件路径在所有 ANF 帐户中必须是唯一的。

    RESOURCE_GROUP="myresourcegroup"
    LOCATION="mylocation"
    ANF_ACCOUNT_NAME="myaccountname"
    POOL_NAME="mypool1"
    SERVICE_LEVEL="premium" # Valid values are standard, premium, and ultra
    UNIQUE_FILE_PATH="myfilepath"
    VOLUME_SIZE_GIB="myvolsize"
    VOLUME_NAME="myvolname"
    VNET_ID="vnetId"
    SUBNET_ID="anfSubnetId"
    
  2. 使用 az netappfiles volume create 命令创建卷。

    az netappfiles volume create \
        --resource-group $RESOURCE_GROUP \
        --location $LOCATION \
        --account-name $ANF_ACCOUNT_NAME \
        --pool-name $POOL_NAME \
        --name "$VOLUME_NAME" \
        --service-level $SERVICE_LEVEL \
        --vnet $VNET_ID \
        --subnet $SUBNET_ID \
        --usage-threshold $VOLUME_SIZE_GIB \
        --file-path $UNIQUE_FILE_PATH \
        --protocol-types CIFS
    

使用域凭据创建机密

  1. 使用 kubectl create secret 命令在 AKS 群集上创建用于访问 Active Directory (AD) 服务器的机密。 Kubernetes 永久性卷将使用此机密来访问 Azure NetApp 文件 SMB 卷。 使用以下命令创建机密,将 USERNAME 替换为你的用户名,将 PASSWORD 替换为你的密码,并将 DOMAIN_NAME 替换为你的 AD 域名。

        kubectl create secret generic smbcreds --from-literal=username=USERNAME --from-literal=password="PASSWORD" --from-literal=domain='DOMAIN_NAME'
    
  2. 检查机密是否已创建。

       kubectl get secret
       NAME       TYPE     DATA   AGE
       smbcreds   Opaque   2      20h
    

安装 SMB CSI 驱动程序

必须安装容器存储接口 (CSI) 驱动程序才能创建 Kubernetes SMB PersistentVolume

  1. 使用 Helm 在群集上安装 SMB CSI 驱动程序。 请务必将 windows.enabled 选项设置为 true

    helm repo add csi-driver-smb https://raw.githubusercontent.com/kubernetes-csi/csi-driver-smb/master/charts   
    helm install csi-driver-smb csi-driver-smb/csi-driver-smb --namespace kube-system --version v1.13.0 --set windows.enabled=true
    

    有关安装 SMB CSI 驱动程序的其他方法,请参阅在 Kubernetes 群集上安装 SMB CSI 驱动程序主版本

  2. 使用 kubectl get pods 命令验证 csi-smb 控制器 Pod 是否正在运行,并且每个工作器节点是否都有一个 Pod 正在运行:

    kubectl get pods -n kube-system | grep csi-smb
    
    csi-smb-controller-68df7b4758-xf2m9   3/3     Running   0          3m46s
    csi-smb-node-s6clj                    3/3     Running   0          3m47s
    csi-smb-node-win-tfxvk                3/3     Running   0          3m47s
    

创建永久性卷

  1. 使用 az netappfiles volume show 列出卷的详细信息。 如果未在上一步中定义,请将变量替换为 Azure NetApp 文件帐户和环境中的适当值。

    az netappfiles volume show \
        --resource-group $RESOURCE_GROUP \
        --account-name $ANF_ACCOUNT_NAME \
        --pool-name $POOL_NAME \
        --volume-name "$VOLUME_NAME -o JSON
    

    以下输出是以实际值执行上述命令的示例。

    {
      ...
      "creationToken": "myvolname",
      ...
      "mountTargets": [
        {
          ...
          "
             "smbServerFqdn": "ANF-1be3.contoso.com",
          ...
        }
      ],
      ...
    }
    
  2. 创建名为 pv-smb.yaml 的文件,并将其复制到以下 YAML 中。 如有必要,请将 myvolname 替换为 creationToken,并将 ANF-1be3.contoso.com\myvolname 替换为上一步中 smbServerFqdn 的值。 请务必包含你在上一步中创建的 AD 凭据机密及其所在命名空间。

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: anf-pv-smb
    spec:
      storageClassName: ""
      capacity:
        storage: 100Gi
      accessModes:
        - ReadWriteMany
      persistentVolumeReclaimPolicy: Retain
      mountOptions:
        - dir_mode=0777
        - file_mode=0777
        - vers=3.0
      csi:
        driver: smb.csi.k8s.io
        readOnly: false
        volumeHandle: myvolname  # make sure it's a unique name in the cluster
        volumeAttributes:
          source: \\ANF-1be3.contoso.com\myvolname
        nodeStageSecretRef:
          name: smbcreds
          namespace: default
    
  3. 使用 kubectl apply 命令创建永久性卷:

    kubectl apply -f pv-smb.yaml
    
  4. 使用 kubectl describe 命令验证永久性卷的状态是否为“Available”:

    kubectl describe pv pv-smb
    

创建永久性卷声明

  1. 创建名为 pvc-smb.yaml 的文件,并将其复制到以下 YAML 中。

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: anf-pvc-smb
    spec:
      accessModes:
        - ReadWriteMany
      volumeName: anf-pv-smb
      storageClassName: ""
      resources:
        requests:
          storage: 100Gi
    
  2. 使用 kubectl apply 命令创建永久性卷声明:

    kubectl apply -f pvc-smb.yaml
    

    使用 kubectl describe 命令验证永久性卷声明的状态是否为“Bound”:

    kubectl describe pvc pvc-smb
    

装载到 Pod

  1. 创建名为 iis-smb.yaml 的文件,并将其复制到以下 YAML 中。 此文件将用于创建 Internet Information Services Pod,以将卷装载到路径 /inetpub/wwwroot

    apiVersion: v1
    kind: Pod 
    metadata:
      name: iis-pod
      labels:
         app: web
    spec:
      nodeSelector:
        "kubernetes.io/os": windows
      volumes:
      - name: smb
        persistentVolumeClaim:
          claimName: anf-pvc-smb 
      containers:
      - name: web
        image: mcr.microsoft.com/windows/servercore/iis:windowsservercore 
        resources:
          limits:
            cpu: 1
            memory: 800M
        ports:
          - containerPort: 80
        volumeMounts:
        - name: smb
          mountPath: "/inetpub/wwwroot"
          readOnly: false
    
  2. 使用 kubectl apply 命令创建 Pod:

    kubectl apply -f iis-smb.yaml
    
  3. 使用 kubectl describe 命令验证 Pod 是否正在运行,并且 /inetpub/wwwroot 是否是从 SMB 装载的:

    kubectl describe pod iis-pod
    

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

    Name:         iis-pod
    Namespace:    default
    Priority:     0
    Node:         akswin000001/10.225.5.246
    Start Time:   Fri, 05 May 2023 09:34:41 -0400
    Labels:       app=web
    Annotations:  <none>
    Status:       Running
    IP:           10.225.5.248
    IPs:
      IP:  10.225.5.248
    Containers:
      web:
        Container ID:   containerd://39a1659b6a2b6db298df630237b2b7d959d1b1722edc81ce9b1bc7f06237850c
        Image:          mcr.microsoft.com/windows/servercore/iis:windowsservercore
        Image ID:       mcr.microsoft.com/windows/servercore/iis@sha256:0f0114d0f6c6ee569e1494953efdecb76465998df5eba951dc760ac5812c7409
        Port:           80/TCP
        Host Port:      0/TCP
        State:          Running
          Started:      Fri, 05 May 2023 09:34:55 -0400
        Ready:          True
        Restart Count:  0
        Limits:
          cpu:     1
          memory:  800M
        Requests:
          cpu:        1
          memory:     800M
        Environment:  <none>
        Mounts:
          /inetpub/wwwroot from smb (rw)
          /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-mbnv8 (ro)
    ...
    
  4. 使用 kubectl exec 命令连接到 Pod 来验证卷是否已装载到 Pod 上,然后在正确目录下使用 dir 命令检查该卷是否已装载,并且大小是否与你预配的卷的大小匹配。

    kubectl exec -it iis-pod –- cmd.exe
    

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

    Microsoft Windows [Version 10.0.20348.1668]
    (c) Microsoft Corporation. All rights reserved.
    
    C:\>cd /inetpub/wwwroot
    
    C:\inetpub\wwwroot>dir
     Volume in drive C has no label.
     Volume Serial Number is 86BB-AA55
    
     Directory of C:\inetpub\wwwroot
    
    05/04/2023  08:15 PM    <DIR>          .
    05/04/2023  08:15 PM    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  107,373,838,336 bytes free
    

动态配置使用 SMB 卷的应用程序

本部分介绍如何使用 Astra Trident 在 Azure NetApp 文件上动态创建 SMB 卷,并将其自动装载到容器化 Windows 应用程序。

安装 Astra Trident

若要动态预配 SMB 卷,需要安装 Astra Trident 版本 22.10 或更高版本。 动态预配 SMB 卷需要使用 Windows 工作器节点。

Astra Trident 是 NetApp 的动态存储预配程序,专为 Kubernetes 而构建。 使用 Astra Trident 的行业标准容器存储接口 (CSI) 驱动程序简化 Kubernetes 应用程序的存储消耗。 Astra Trident 在 Kubernetes 群集上部署为 Pod,并为 Kubernetes 工作负荷提供动态存储业务流程服务。

可以使用 Trident 运算符(通过手动方式或通过 Helm)或 tridentctl 来安装 Trident。 若要详细了解这些安装方法及其工作原理,请参阅安装指南

使用 Helm 安装 Astra Trident

必须在工作站上安装 Helm,才能使用此方法安装 Astra Trident。 有关安装 Astra Trident 的其他方法,请参阅 Astra Trident 安装指南。 如果群集中有 Windows 工作器节点,请确保使用任何安装方法启用 Windows。

  1. 若要使用 Helm 为具有 Windows 工作器节点的群集安装 Astra Trident,请运行以下命令:

    helm repo add netapp-trident https://netapp.github.io/trident-helm-chart
    
    helm install trident netapp-trident/trident-operator --version 23.04.0  --create-namespace --namespace trident –-set windows=true
    

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

    NAME: trident
    LAST DEPLOYED: Fri May  5 14:23:05 2023
    NAMESPACE: trident
    STATUS: deployed
    REVISION: 1
    TEST SUITE: None
    NOTES:
    Thank you for installing trident-operator, which will deploy and manage NetApp's Trident CSI
    storage provisioner for Kubernetes.
    
    Your release is named 'trident' and is installed into the 'trident' namespace.
    Please note that there must be only one instance of Trident (and trident-operator) in a Kubernetes cluster.
    
    To configure Trident to manage storage resources, you will need a copy of tridentctl, which is available in pre-packaged Trident releases.  You may find all Trident releases and source code online at https://github.com/NetApp/trident.
    
    To learn more about the release, try:
    
      $ helm status trident
      $ helm get all trident
    
  2. 若要确认 Astra Trident 已成功安装,请运行以下 kubectl describe 命令:

    kubectl describe torc trident
    

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

    Name:         trident
    Namespace:    
    Labels:       app.kubernetes.io/managed-by=Helm
    Annotations:  meta.helm.sh/release-name: trident
                  meta.helm.sh/release-namespace: trident
    API Version:  trident.netapp.io/v1
    Kind:         TridentOrchestrator
    Metadata:
        ...
    Spec:
      IPv6:                  false
      Autosupport Image:     docker.io/netapp/trident-autosupport:23.04
      Autosupport Proxy:     <nil>
      Disable Audit Log:     true
      Enable Force Detach:   false
      Http Request Timeout:  90s
      Image Pull Policy:     IfNotPresent
      k8sTimeout:            0
      Kubelet Dir:           <nil>
      Log Format:            text
      Log Layers:            <nil>
      Log Workflows:         <nil>
      Namespace:             trident
      Probe Port:            17546
      Silence Autosupport:   false
      Trident Image:         docker.io/netapp/trident:23.04.0
      Windows:               true
    Status:
      Current Installation Params:
        IPv6:                       false
        Autosupport Hostname:       
        Autosupport Image:          docker.io/netapp/trident-autosupport:23.04
        Autosupport Proxy:          
        Autosupport Serial Number:  
        Debug:                      false
        Disable Audit Log:          true
        Enable Force Detach:        false
        Http Request Timeout:       90s
        Image Pull Policy:          IfNotPresent
        Image Pull Secrets:
        Image Registry:       
        k8sTimeout:           30
        Kubelet Dir:          /var/lib/kubelet
        Log Format:           text
        Log Layers:           
        Log Level:            info
        Log Workflows:        
        Probe Port:           17546
        Silence Autosupport:  false
        Trident Image:        docker.io/netapp/trident:23.04.0
      Message:                Trident installed
      Namespace:              trident
      Status:                 Installed
      Version:                v23.04.0
    Events:
      Type    Reason      Age   From                        Message
      ----    ------      ----  ----                        -------
      Normal  Installing  74s   trident-operator.netapp.io  Installing Trident
      Normal  Installed   46s   trident-operator.netapp.io  Trident installed
    

创建后端

要指示 Astra Trident 有关 Azure NetApp 文件订阅及其需要在何处创建卷,必须创建后端。 有关后端的详细信息,请参阅 Azure NetApp 文件后端配置选项和示例

  1. 创建名为 backend-secret-smb.yaml 的文件,并将其复制到以下 YAML 中。 将 Client IDclientSecret 更改为环境的正确值。

    apiVersion: v1
    kind: Secret
    metadata:
      name: backend-tbc-anf-secret
    type: Opaque
    stringData:
      clientID: abcde356-bf8e-fake-c111-abcde35613aa
      clientSecret: rR0rUmWXfNioN1KhtHisiSAnoTherboGuskey6pU
    
  2. 创建名为 backend-anf-smb.yaml 的文件,并将其复制到以下 YAML 中。 将 ClientIDclientSecretsubscriptionIDtenantIDlocation, 和 serviceLevel 更改为环境的正确值。 可以从 Microsoft Entra ID 中的应用程序注册中找到 tenantIDclientIDclientSecret,且该应用注册对 Azure NetApp 文件服务具有足够权限。 应用程序注册包括 Azure 预定义的“所有者”或“参与者”角色。 Azure 位置必须包含至少一个委托子网。 serviceLevel 必须匹配针对为 AKS 工作负载配置 Azure NetApp 文件中的容量池配置的 serviceLevel

    apiVersion: trident.netapp.io/v1
    kind: TridentBackendConfig
    metadata:
      name: backend-tbc-anf-smb
    spec:
      version: 1
      storageDriverName: azure-netapp-files
      subscriptionID: 12abc678-4774-fake-a1b2-a7abcde39312
      tenantID: a7abcde3-edc1-fake-b111-a7abcde356cf
      location: eastus
      serviceLevel: Premium
      credentials:
        name: backend-tbc-anf-secret
      nasType: smb
    
  3. 使用 kubectl apply 命令创建机密和后端。

    创建机密:

    kubectl apply -f backend-secret.yaml -n trident
    

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

    secret/backend-tbc-anf-secret created
    

    创建后端:

    kubectl apply -f backend-anf.yaml -n trident
    

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

    tridentbackendconfig.trident.netapp.io/backend-tbc-anf created
    
  4. 通过运行以下命令验证后端是否创建:

    kubectl get tridentbackends -n trident
    

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

    NAME        BACKEND               BACKEND UUID
    tbe-9shfq   backend-tbc-anf-smb   09cc2d43-8197-475f-8356-da7707bae203
    

为 SMB 使用域凭据创建机密

  1. 使用 kubectl create secret 命令在 AKS 群集上创建用于访问 AD 服务器的机密。 Kubernetes 永久性卷将使用此信息来访问 Azure NetApp 文件 SMB 卷。 使用以下命令,将 DOMAIN_NAME\USERNAME 替换为域名和用户名,并将 PASSWORD 替换为密码。

    kubectl create secret generic smbcreds --from-literal=username=DOMAIN_NAME\USERNAME –from-literal=password="PASSWORD" 
    
  2. 验证是否已创建机密。

    kubectl get secret
    

    输出如以下示例所示:

    NAME       TYPE     DATA   AGE
    smbcreds   Opaque   2      2h
    

创建存储类

存储类用于定义使用永久性卷动态创建存储单位的方式。 要使用 Azure NetApp 文件卷,必须创建存储类。

  1. 创建名为 anf-storageclass-smb.yaml 的文件,并将其复制到以下 YAML 中。

    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      name: anf-sc-smb
    provisioner: csi.trident.netapp.io
    allowVolumeExpansion: true
    parameters:
      backendType: "azure-netapp-files"
      trident.netapp.io/nasType: "smb"
      csi.storage.k8s.io/node-stage-secret-name: "smbcreds"
      csi.storage.k8s.io/node-stage-secret-namespace: "default"
    
  2. 使用 kubectl apply 命令创建存储类:

    kubectl apply -f anf-storageclass-smb.yaml
    

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

    storageclass/anf-sc-smb created
    
  3. 运行 kubectl get 命令以查看存储类的状态:

    kubectl get sc anf-sc-smb
    NAME         PROVISIONER             RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
    anf-sc-smb   csi.trident.netapp.io   Delete          Immediate           true                   13s
    

创建 PVC

永久性卷声明 (PVC) 是指用户对存储空间的请求。 创建永久性卷声明后,Astra Trident 会自动创建 Azure NetApp 文件 SMB 共享,使其可供 Kubernetes 工作负荷使用。

  1. 创建一个名为 anf-pvc-smb.yaml 的文件,并复制以下 YAML。 在此示例中,创建具有 ReadWriteMany 访问权限的 100-GiB 卷,并使用在创建存储类中创建的存储类。

    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
      name: anf-pvc-smb
    spec:
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 100Gi
      storageClassName: anf-sc-smb
    
  2. 使用 kubectl apply 命令创建持久卷声明:

    kubectl apply -f anf-pvc-smb.yaml
    

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

    persistentvolumeclaim/anf-pvc-smb created
    
  3. 若要查看有关永久性卷声明的信息,请运行 kubectl get 命令:

    kubectl get pvc
    

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

    NAME          STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
    anf-pvc-smb   Bound    pvc-209268f5-c175-4a23-b61b-e34faf5b6239   100Gi      RWX            anf-sc-smb     5m38s
    
  4. 若要查看 Astra Trident 创建的永久性卷,请运行以下 kubectl get 命令:

    kubectl get pv
    NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                 STORAGECLASS   REASON   AGE
    pvc-209268f5-c175-4a23-b61b-e34faf5b6239   100Gi      RWX            Delete           Bound    default/anf-pvc-smb   anf-sc-smb              5m52s
    

使用永久性卷

创建 PVC 后,可以启动 Pod 访问 Azure NetApp 文件卷。 以下清单可用于定义 Internet Information Services (IIS) Pod,其可装载上一步创建的 Azure NetApp 文件 SMB 共享。 在本示例中,卷将装载到 /inetpub/wwwroot

  1. 创建名为 anf-iis-pod.yaml 的文件,并将其复制到以下 YAML 中:

    apiVersion: v1
    kind: Pod 
    metadata:
      name: iis-pod
      labels:
         app: web
    spec:
      nodeSelector:
        "kubernetes.io/os": windows
      volumes:
      - name: smb
        persistentVolumeClaim:
          claimName: anf-pvc-smb 
      containers:
      - name: web
        image: mcr.microsoft.com/windows/servercore/iis:windowsservercore 
        resources:
          limits:
            cpu: 1
            memory: 800M
        ports:
          - containerPort: 80
        volumeMounts:
        - name: smb
          mountPath: "/inetpub/wwwroot"
          readOnly: false
    
  2. 使用 kubectl apply 命令创建部署:

    kubectl apply -f anf-iis-deploy-pod.yaml
    

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

    pod/iis-pod created
    

    使用 kubectl describe 命令验证 pod 正在运行且通过 SMB 装载到 /inetpub/wwwroot

    kubectl describe pod iis-pod
    

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

    Name:         iis-pod
    Namespace:    default
    Priority:     0
    Node:         akswin000001/10.225.5.246
    Start Time:   Fri, 05 May 2023 15:16:36 -0400
    Labels:       app=web
    Annotations:  <none>
    Status:       Running
    IP:           10.225.5.252
    IPs:
      IP:  10.225.5.252
    Containers:
      web:
        Container ID:   containerd://1e4959f2b49e7ad842b0ec774488a6142ac9152ca380c7ba4d814ae739d5ed3e
        Image:          mcr.microsoft.com/windows/servercore/iis:windowsservercore
        Image ID:       mcr.microsoft.com/windows/servercore/iis@sha256:0f0114d0f6c6ee569e1494953efdecb76465998df5eba951dc760ac5812c7409
        Port:           80/TCP
        Host Port:      0/TCP
        State:          Running
          Started:      Fri, 05 May 2023 15:16:44 -0400
        Ready:          True
        Restart Count:  0
        Limits:
          cpu:     1
          memory:  800M
        Requests:
          cpu:        1
          memory:     800M
        Environment:  <none>
        Mounts:
          /inetpub/wwwroot from smb (rw)
          /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-zznzs (ro)
    
  3. 验证卷是否已装载到 Pod 上:使用 kubectl exec 连接到 Pod。 然后使用正确目录中的 dir 命令检查是否已装载卷,并且大小是否与预配卷的大小匹配。

    kubectl exec -it iis-pod –- cmd.exe
    

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

    Microsoft Windows [Version 10.0.20348.1668]
    (c) Microsoft Corporation. All rights reserved.
    
    C:\>cd /inetpub/wwwroot
    
    C:\inetpub\wwwroot>dir
     Volume in drive C has no label.
     Volume Serial Number is 86BB-AA55
    
     Directory of C:\inetpub\wwwroot
    
    05/05/2023  01:38 AM    <DIR>          .
    05/05/2023  01:38 AM    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  107,373,862,912 bytes free
    
    C:\inetpub\wwwroot>exit
    

后续步骤

Astra Trident 支持 Azure NetApp 文件的许多功能。 有关详细信息,请参阅: