在 Azure Stack HCI 叢集的 AKS 中使用持續性磁碟區Use persistent volume in an AKS on Azure Stack HCI cluster

適用于: AKS on Azure Stack HCI、Windows Server 2019 Datacenter 上的 AKS runtimeApplies to: AKS on Azure Stack HCI, AKS runtime on Windows Server 2019 Datacenter

永續性磁碟區代表一塊已佈建來與 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 more pods and is meant for long-term storage. 它也與 pod 或節點生命週期無關。It's also independent of pod or node lifecycle. 雖然您可以為 Windows 和 Linux 節點布建 PVC,但在本節中,您將瞭解如何建立持續性磁片區,以及如何在您的 Windows 應用程式中使用此磁片區。While you can provision a PVC for both Windows and Linux nodes, in this section, you'll see how to create a persistent volume and how to use this volume in your Windows application. 如需詳細資訊,請參閱 Kubernetes 中的持續性磁片區。For more information, see Persistent volumes in Kubernetes.

開始之前Before you begin

以下是您需要的入門入門:Here's what you need to get started:

  • 至少有一個 Windows 背景工作節點的 Kubernetes 叢集。A Kubernetes cluster with at least one Windows worker node.
  • 用來存取 Kubernetes 叢集的 kubeconfig 檔。A kubeconfig file to access the Kubernetes cluster.

建立永續性磁碟區宣告Create a persistent volume claim

持續性磁片區宣告可用來根據儲存類別自動布建儲存體。A persistent volume claim is used to automatically provision storage based on a storage class. 若要建立磁片區宣告,請先建立名為的檔案 pvc-akshci-csi.yaml ,並複製到下列 YAML 定義中。To create a volume claim, first create a file named pvc-akshci-csi.yaml and copy in the following YAML definition. 宣告要求磁片的大小為 10 GB,且具有 >readwriteonce   存取權。The claim requests a disk that is 10 GB in size with ReadWriteOnce access.  *預設*   儲存類別會指定為 (vhdx) 的儲存類別。The default storage class is specified as the storage class (vhdx).

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
 name: pvc-akshci-csi
spec:
 accessModes:
 - ReadWriteOnce
 resources:
  requests:
   storage: 10Gi

在 Azure Stack HCI 叢集的其中一部伺服器上的系統管理 PowerShell 會話中執行下列命令,以建立磁片區 (使用像是 [ 輸入-PSSession ] 或 [遠端桌面] 連線到伺服器) 的方法:Create the volume by running the following commands in an administrative PowerShell session on one of the servers in the Azure Stack HCI cluster (using a method such as Enter-PSSession or Remote Desktop to connect to the server):

kubectl create -f pvc-akshci-csi.yaml 

下列輸出會顯示已成功建立您的永久磁片區宣告:The following output will show that your persistent volume claim has been created successfully:

輸出:Output:

persistentvolumeclaim/pvc-akshci-csi created

使用永久性磁片區Use persistent volume

若要使用持續性磁片區,請建立名為 winwebserver 的檔案,並複製到下列 YAML 定義中。To use a persistent volume, create a file named winwebserver.yaml and copy in the following YAML definition. 然後,您將建立可存取永久性磁片區宣告和 vhdx 的 pod。You will then create a pod with access to the persistent volume claim and vhdx.

在以下的 yaml 定義中, mountPath 是在容器內掛接磁片區的路徑。In the yaml definition below, mountPath is the path to mount a volume inside a container. 建立 pod 成功之後,您會看到 mnt 建立于 C \ : 的子目錄,以及在 mnt 內建立的子目錄 akshciscsiAfter a successful pod creation, you will see the subdirectory mnt created in C:\ and the subdirectory akshciscsi created inside mnt.

apiVersion: apps/v1 
kind: Deployment 
metadata: 
  labels: 
    app: win-webserver 
  name: win-webserver 
spec: 
  replicas: 1 
  selector: 
    matchLabels: 
      app: win-webserver 
  template: 
    metadata: 
      labels: 
        app: win-webserver 
      name: win-webserver 
    spec: 
     containers: 
      - name: windowswebserver 
        image: mcr.microsoft.com/windows/servercore/iis:windowsservercore-ltsc2019 
        ports:  
          - containerPort: 80    
        volumeMounts: 
            - name: akshciscsi 
              mountPath: "/mnt/akshciscsi" 
     volumes: 
        - name: akshciscsi 
          persistentVolumeClaim: 
            claimName:  pvc-akshci-csi 
     nodeSelector: 
      kubernetes.io/os: windows 

若要使用上述 yaml 定義建立 pod,請執行:To create a pod with the above yaml definition, run:

kubectl create -f winwebserver.yaml 

若要確認 pod 正在執行,請執行下列命令。To make sure the pod is running, run the following command. 等候幾分鐘,直到 pod 處於執行中狀態,因為提取映射需要一些時間。Wait a few minutes until the pod is in a running state, since pulling the image takes time.

kubectl get pods -o wide 

當您的 pod 正在執行之後,請執行下列命令來查看 pod 狀態:Once your pod is running, view the pod status by running the following command:

kubectl.exe describe pod %podName% 

若要確認已將磁片區掛接到 pod,請執行下列命令:To verify your volume has been mounted in the pod, run the following command:

kubectl exec -it %podname% cmd.exe 

刪除永久性磁片區宣告Delete a persistent volume claim

在刪除永久性磁片區宣告之前,您必須執行下列動作來刪除應用程式部署:Before deleting a persistent volume claim, you must delete the app deployment by running:

kubectl delete deployments win-webserver

然後,您可以藉由執行下列動作來刪除永久性磁片區宣告:You can then delete a persistent volume claim by running:

kubectl delete PersistentVolumeClaim pvc-akshci-csi

後續步驟Next steps