Share via


佈建適用於 Azure Kubernetes Service 的 Azure NetApp Files NFS 磁碟區

設定適用於 Azure Kubernetes Service 的 Azure NetApp Files 後,即可佈建適用於 Azure Kubernetes Service 的 Azure NetApp Files 磁碟區。

Azure NetApp Files 支援使用 NFS (NFSv3 或 NFSv4.1)、SMB雙重通訊協定 (NFSv3 和 SMB,或 NFSv4.1 和 SMB) 的磁碟區。

靜態設定使用 NFS 磁碟區的應用程式

本節說明如何在 Azure NetApp Files 上建立 NFS 磁碟區,並以靜態方式向 Kubernetes 公開磁碟區。 本節還描述如何搭配容器化應用程式使用磁碟區。

建立 NFS 磁碟區

  1. 定義變數以供日後使用。 以適合帳戶和環境的值取代 myresourcegroupmylocationmyaccountnamemypool1premiummyfilepathmyvolsizemyvolnamevnetidanfSubnetIDfilepath 在所有 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 命令建立磁碟區。 如需詳細資訊,請參閱建立適用於 Azure NetApp Files 的 NFS 磁碟區

    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 NFSv3
    

建立永續性磁碟區

  1. 使用 az netappfiles volume show 命令列出磁碟區的詳細資料。 如果未在上一個步驟中定義,請將這些變數取代為適合 Azure NetApp Files 帳戶和環境的值。

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

    下列輸出是使用實際值執行上述命令的範例。

    {
      ...
      "creationToken": "myfilepath2",
      ...
      "mountTargets": [
        {
          ...
          "ipAddress": "10.0.0.4",
          ...
        }
      ],
      ...
    }
    
  2. 建立名為 pv-nfs.yaml 的檔案,然後將下列 YAML 複製進來。 請確定伺服器符合步驟 1 中的輸出 IP 位址,且路徑符合上述 creationToken 的輸出。 容量也必須符合上述步驟中的磁碟區大小。

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv-nfs
    spec:
      capacity:
        storage: 100Gi
      accessModes:
        - ReadWriteMany
      mountOptions:
        - vers=3
      nfs:
        server: 10.0.0.4
        path: /myfilepath2
    
  3. 使用 kubectl apply 命令建立永續性磁碟區:

    kubectl apply -f pv-nfs.yaml
    
  4. 使用 kubectl describe 命令,驗證永續性磁碟區的狀態為 [可用]

    kubectl describe pv pv-nfs
    

建立永續性磁碟區宣告

  1. 建立名為 pvc-nfs.yaml 的檔案,然後將下列 YAML 複製進來。 此資訊清單會針對 100Gi 儲存體和 ReadWriteMany 存取模式建立名為 pvc-nfs 的PVC,並符合您所建立的 PV。

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: pvc-nfs
    spec:
      accessModes:
        - ReadWriteMany
      storageClassName: ""
      resources:
        requests:
          storage: 100Gi
    
  2. 使用 kubectl apply 命令建立永續性磁碟區宣告:

    kubectl apply -f pvc-nfs.yaml
    
  3. 使用 kubectl describe 命令,驗證永續性磁碟區宣告的 [狀態]為 [繫結]

    kubectl describe pvc pvc-nfs
    

使用 Pod 裝載

  1. 建立名為 nginx-nfs.yaml 的檔案,然後將下列 YAML 複製進來。 此資訊清單會定義使用永續性磁碟區宣告的 nginx Pod。

    kind: Pod
    apiVersion: v1
    metadata:
      name: nginx-nfs
    spec:
      containers:
      - image: mcr.microsoft.com/oss/nginx/nginx:1.15.5-alpine
        name: nginx-nfs
        command:
        - "/bin/sh"
        - "-c"
        - while true; do echo $(date) >> /mnt/azure/outfile; sleep 1; done
        volumeMounts:
        - name: disk01
          mountPath: /mnt/azure
      volumes:
      - name: disk01
        persistentVolumeClaim:
          claimName: pvc-nfs
    
  2. 使用 kubectl apply 命令建立 Pod:

    kubectl apply -f nginx-nfs.yaml
    
  3. 使用 kubectl describe 命令,驗證 Pod 為 [執行中]

    kubectl describe pod nginx-nfs
    
  4. 使用 kubectl exec 連線到 Pod,然後使用 df -h 檢查是否已掛接磁碟區,以驗證已在 Pod 中裝載您的磁碟區。

    kubectl exec -it nginx-nfs -- sh
    
    / # df -h
    Filesystem             Size  Used Avail Use% Mounted on
    ...
    10.0.0.4:/myfilepath2  100T  384K  100T   1% /mnt/azure
    ...
    

動態設定使用 NFS 磁碟區的應用程式

您可以使用 Astra Trident 在 Azure NetApp Files 上動態佈建 NFS 或 SMB 檔案。 只有 Windows 背景工作角色節點才支援動態佈建的 SMB 磁碟區。

本節說明如何使用 Astra Trident 在 Azure NetApp Files 上動態建立 NFS 磁碟區,並自動掛接至容器化應用程式。

安裝 Astra Trident

若要動態佈建 NFS 磁碟區,您必須安裝 Astra Trident。 Astra Trident 是 NetApp 的動態儲存體佈建工具,專為 Kubernetes 所建置。 使用 Astra Trident 的業界標準容器儲存體介面 (CSI) 驅動程式,簡化 Kubernetes 應用程式的儲存體使用。 Astra Trident 會在 Kubernetes 叢集上部署為 Pod,並為 Kubernetes 工作負載提供動態儲存體協調流程服務。

您可以使用 Trident 運算子 (手動或使用 Helm) 或 tridentctl 來安裝 Trident。 若要深入了解這些安裝方法及其運作方式,請參閱 Astra Trident 安裝指南

使用 Helm 安裝 Astra Trident

必須在工作站上安裝 Helm,才能使用此方法安裝 Astra Trident。 如需安裝 Astra Trident 的其他方法,請參閱 Astra Trident 安裝指南

  1. 若要在僅具有 Linux 背景工作角色節點的叢集中使用 Helm 安裝 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
    

    此命令的輸出與下列範例類似:

    NAME: trident
    LAST DEPLOYED: Fri May  5 13:55:36 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:               false
    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  2m59s  trident-operator.netapp.io  Installing Trident
      Normal  Installed   2m31s  trident-operator.netapp.io  Trident installed
    

建立後端

若要指示 Astra Trident 關於 Azure NetApp Files 訂閱以及需要建立磁碟區的位置,則會建立後端。 此步驟需要與在上一個步驟中所建立帳戶相關的詳細資料。

  1. 建立名為 backend-secret.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.yaml 的檔案,然後將下列 YAML 複製進來。 將 subscriptionIDtenantIDlocationserviceLevel 變更為環境適用的正確值。 針對已啟用 Azure NetApp Files 的 Azure 訂用帳戶使用 subscriptionID。 如果您具有 Azure NetApp Files 服務的足夠權限,即可在 Microsoft Entra ID 的應用程式註冊中取得 tenantIDclientIDclientSecret。 應用程式註冊包含 Azure 預先定義的擁有者或參與者角色。 此位置必須是 Azure 位置,其中至少包含一個在上個步驟中建立的委派子網路。 serviceLevel 必須符合設定適用於 AKS 工作負載的 Azure NetApp Files 中容量集區所設定的 serviceLevel

    apiVersion: trident.netapp.io/v1
    kind: TridentBackendConfig
    metadata:
      name: backend-tbc-anf
    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
    

    如需後端的詳細資訊,請參閱 Azure NetApp Files 後端組態選項和範例

  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 命令確認後端已建立:

     kubectl get tridentbackends -n trident
    

    此命令的輸出與下列範例類似:

    NAME        BACKEND               BACKEND UUID
    tbe-kfrdh   backend-tbc-anf   8da4e926-9dd4-4a40-8d6a-375aab28c566
    

建立儲存體類別

儲存體類別可用來定義如何搭配永續性磁碟區動態建立儲存體單位。 若要取用 Azure NetApp Files 磁碟區,必須建立儲存體類別。

  1. 建立名為 anf-storageclass.yaml 的檔案,然後將下列 YAML 複製進來:

    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      name: azure-netapp-files
    provisioner: csi.trident.netapp.io
    parameters:
      backendType: "azure-netapp-files"
      fsType: "nfs"
    
  2. 使用 kubectl apply 命令建立儲存類別:

    kubectl apply -f anf-storageclass.yaml
    

    此命令的輸出與下列範例類似:

    storageclass/azure-netapp-files created
    
  3. 執行 kubectl get 命令以檢視儲存類別的狀態:

    kubectl get sc
    NAME                 PROVISIONER             RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
    azure-netapp-files   csi.trident.netapp.io   Delete          Immediate           false                  
    

建立 PVC

永續性磁碟區宣告 (PVC) 是使用者提出的儲存體要求。 建立永續性磁碟區宣告時,Astra Trident 會自動建立 Azure NetApp Files 磁碟區,且可供 Kubernetes 工作負載取用。

  1. 建立名為 anf-pvc.yaml 的檔案,然後將下列 YAML 複製進來。 在此範例中,需要使用具有 ReadWriteMany 存取權的 1 TiB 磁碟區。

    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
      name: anf-pvc
    spec:
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 1Ti
      storageClassName: azure-netapp-files
    
  2. 使用 kubectl apply 命令建立永續性磁碟區宣告:

    kubectl apply -f anf-pvc.yaml
    

    此命令的輸出與下列範例類似:

    persistentvolumeclaim/anf-pvc created
    
  3. 若要檢視永續性磁碟區宣告的相關資訊,請執行 kubectl get 命令:

    kubectl get pvc
    

    此命令的輸出與下列範例類似:

    kubectl get pvc -n trident
    NAME      STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS         AGE
    anf-pvc   Bound    pvc-bffa315d-3f44-4770-86eb-c922f567a075   1Ti        RWO            azure-netapp-files   62s
    

使用永續性磁碟區

建立 PVC 之後,Astra Trident 會建立永續性磁碟區。 您可以啟動 Pod 以掛接並存取 Azure NetApp Files 磁碟區。

您可以使用下列資訊清單來定義 NGINX Pod,以掛接在上一個步驟中建立的 Azure NetApp Files 磁碟區。 在此範例中,磁碟區會裝載在 /mnt/data

  1. 建立名為 anf-nginx-pod.yaml 的檔案,然後將下列 YAML 複製進來:

    kind: Pod
    apiVersion: v1
    metadata:
      name: nginx-pod
    spec:
      containers:
      - name: nginx
        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/data"
          name: volume
      volumes:
        - name: volume
          persistentVolumeClaim:
            claimName: anf-pvc
    
  2. 使用 kubectl apply 命令建立 Pod:

    kubectl apply -f anf-nginx-pod.yaml
    

    此命令的輸出與下列範例類似:

    pod/nginx-pod created
    

    Kubernetes 已建立 Pod,其中包含在 /mnt/datanginx 容器內掛接且可存取的磁碟區。 使用 kubectl describe 命令檢查 Pod 的事件記錄檔,即可確認:

    kubectl describe pod nginx-pod
    

    此命令的輸出與下列範例類似:

    [...]
    Volumes:
      volume:
        Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
        ClaimName:  anf-pvc
        ReadOnly:   false
      default-token-k7952:
        Type:        Secret (a volume populated by a Secret)
        SecretName:  default-token-k7952
        Optional:    false
    [...]
    Events:
      Type    Reason                  Age   From                     Message
      ----    ------                  ----  ----                     -------
      Normal  Scheduled               15s   default-scheduler        Successfully assigned trident/nginx-pod to brameshb-non-root-test
      Normal  SuccessfulAttachVolume  15s   attachdetach-controller  AttachVolume.Attach succeeded for volume "pvc-bffa315d-3f44-4770-86eb-c922f567a075"
      Normal  Pulled                  12s   kubelet                  Container image "mcr.microsoft.com/oss/nginx/nginx:1.15.5-alpine" already present on machine
      Normal  Created                 11s   kubelet                  Created container nginx
      Normal  Started                 10s   kubelet                  Started container nginx
    

下一步

Astra Trident 可透過 Azure NetApp Files 支援許多功能。 如需詳細資訊,請參閱