由 az file 建立 pv,pvc - daniel-qa/Azure-Kubernetes-Service GitHub Wiki

https://docs.microsoft.com/zh-tw/azure/aks/azure-files-volume

  • 記得要先建立檔案共用( share file) , 並將名稱填入 shareName 欄位值:

使用 Azure 檔案服務作為 Kubernetes 磁碟區之前, 必須建立 Azure 儲存體帳戶與檔案共用。

必需: 資源群組、儲存體帳戶,以檔案共用 ( myshare )

  • 資源群組 : CoreServiceRG-Test

  • 儲存體帳戶 : corestorageservicetest

  • 檔案共用 (File shares) : myshare

  • 取得 storage account key (STORAGE_KEY)

  az storage account keys list --resource-group CoreServiceRG-Test --account-name corestorageservicetest --query "[0].value" -o tsv
zQSbJ/7CTamf1sbFFPeSIcZNMvlwuIgla+3HzWguuDMP97PIN3o6DYM9FzR5v+w8xcMJBhEw+TWlIepOMUYWOw==

建立 Kubernetes 祕密

Kubernetes 必須要有認證才能存取前面步驟中建立的檔案共用。 這些認證會儲存在 [Kubernetes 祕密],並在建立 Kubernetes Pod 時參考。

kubectl create secret generic azure-secret --from-literal=azurestorageaccountname=corestorageservicetest --from-literal=azurestorageaccountkey=zQSbJ/7CTamf1sbFFPeSIcZNMvlwuIgla+3HzWguuDMP97PIN3o6DYM9FzR5v+w8xcMJBhEw+TWlIepOMUYWOw==

將檔案共用掛接為內嵌磁片區 ( inline volume )

若要將Azure 檔案儲存體共用掛接至 Pod,請在容器規格中設定磁片區。

  • azure-files-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  nodeSelector:
    kubernetes.io/os: linux
  containers:
  - image: mcr.microsoft.com/oss/nginx/nginx:1.15.5-alpine
    name: mypod
    resources:
      requests:
        cpu: 100m
        memory: 128Mi
      limits:
        cpu: 250m
        memory: 256Mi
    volumeMounts:
      - name: azure
        mountPath: /mnt/azure
  volumes:
  - name: azure
    csi:
      driver: file.csi.azure.com
      readOnly: false
      volumeAttributes:
        secretName: azure-secret  # required
        shareName: myshare  # required
        mountOptions: "dir_mode=0777,file_mode=0777,cache=strict,actimeo=30"  # optional

  • 創建 azure file pod
kubectl apply -f azure-files-pod.yaml

現在已有一個 Azure 檔案共用掛接在 /mnt/azure 的執行中 Pod。

使用 kubectl describe pod mypod 來驗證是否已成功掛接共用。

 kubectl describe pod mypod
  • 將檔案共用掛接為永續性磁片區 (PV)

fileMode和dirMode的預設值為0777。

  • azurefile-mount-options-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: azurefile
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: azurefile-csi
  csi:
    driver: file.csi.azure.com
    readOnly: false
    volumeHandle: unique-volumeid  # make sure this volumeid is unique in the cluster
    volumeAttributes:
      resourceGroup: CoreServiceRG-Test  # optional, only set this when storage account is not in the same resource group as agent node
      shareName: myshare
    nodeStageSecretRef:
      name: azure-secret
      namespace: default
  mountOptions:
    - dir_mode=0777
    - file_mode=0777
    - uid=0
    - gid=0
    - mfsymlinks
    - cache=strict
    - nosharesock
    - nobrl

建立azurefile-mount-options-pvc.yaml檔案, 裡面的 PersistentVolumeClaim 使用 PersistentVolume 。 例如:

  • azurefile-mount-options-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: azurefile
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: azurefile-csi
  volumeName: azurefile
  resources:
    requests:
      storage: 5Gi
  • 建立 PersistentVolume 和 PersistentVolumeClaim。
kubectl apply -f azurefile-mount-options-pv.yaml
kubectl apply -f azurefile-mount-options-pvc.yaml

確認已建立 PersistentVolumeClaim 並系結至 PersistentVolume。

$ kubectl get pvc azurefile

NAME        STATUS   VOLUME      CAPACITY   ACCESS MODES   STORAGECLASS   AGE
azurefile   Bound    azurefile   5Gi        RWX            azurefile      5s
  • Update your container spec to reference your PersistentVolumeClaim and update your pod. For example:
...
  volumes:
  - name: azure
    persistentVolumeClaim:
      claimName: azurefile

p.s 在 spc: 之下,縮排兩個空白

  • 由於 Pod 規格無法就地更新,請使用 kubectl 命令來刪除,然後重新建立 Pod:
kubectl delete pod mypod

kubectl apply -f azure-files-pod.yaml