使用 EFS 檔案系統

AWS 上的 GKE 1.6 以上版本支援透過 EFS CSI 驅動程式使用 AWS Elastic File System (EFS)。本主題說明如何在使用者叢集上,將現有的 EFS 檔案系統掛接為 PersistentVolume。

事前準備

如要執行本主題中的步驟,您需要下列項目:

  • 與 GKE on AWS 安裝作業位於相同 AWS VPC 中的現有 EFS 檔案系統
  • 在與 GKE on AWS 安裝相同的 AWS VPC 中,至少有一個 EFS 掛接目標。
  • 所有 EFS 掛接目標都必須屬於下列項目:
    • AWS 上的 GKE 安裝作業適用的私人子網路。根據預設,AWS 上的 GKE 會建立名為 gke-CLUSTER_ID-private-AWS_ZONE 的子網路,其中 CLUSTER_ID 是使用者叢集 ID,AWS_ZONE 則是 AWS 可用區。
    • 節點集區安全性群組。根據預設,AWS 上的 GKE 會建立名為 gke-CLUSTER_ID-nodepool 的節點集區,其中 CLUSTER_ID 是使用者叢集 ID。
  • anthos-aws 目錄中,使用 anthos-gke 將環境切換至使用者叢集。
    cd anthos-aws
    env HTTPS_PROXY=http://localhost:8118 \
      anthos-gke aws clusters get-credentials CLUSTER_NAME
    CLUSTER_NAME 替換為使用者叢集名稱。

使用 EFS PersistentVolume

如要將 EFS 檔案系統做為使用者叢集的 PersistentVolume,請先建立 PersistentVolume,然後建立 PersistentVolumeClaim,並在工作負載中參照該項目。

建立 PersistentVolume

如要使用 EFS CSI 驅動程式建立 PersistentVolume,請執行下列步驟。

  1. anthos-aws 目錄中,使用 anthos-gke 將環境切換至使用者叢集。

    cd anthos-aws
    env HTTPS_PROXY=http://localhost:8118 \
      anthos-gke aws clusters get-credentials CLUSTER_NAME
    CLUSTER_NAME 替換為使用者叢集名稱。

  2. 您使用的 PersistentVolume 設定,取決於是否直接連線至 Elastic File System,或是透過存取點連線。選取要直接連線至 Elastic File System,還是透過存取點連線。

    直接連結

    將下列 YAML 資訊清單複製到名為 efs-volume.yaml 的檔案。 資訊清單會參照您先前建立的 EFS 儲存空間類別。

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: VOLUME_NAME
    spec:
      capacity:
        # Note: storage capacity is not used by the EFS CSI driver.
        # It is required by the PersistentVolume spec.
        storage: 5Gi
      volumeMode: Filesystem
      accessModes:
        - ReadWriteMany
      persistentVolumeReclaimPolicy: Retain
      storageClassName: "" # storageClassName is not required, see note in the following section.
      claimRef:
        name: CLAIM_NAME
        namespace: default
      csi:
        driver: efs.csi.aws.com
        volumeHandle: EFS_FILE_SYSTEM_ID
    

    更改下列內容:

    • VOLUME_NAME 替換為永久磁碟區的名稱。
    • CLAIM_NAME 替換為您要用於 PersistentVolumeClaim 的名稱。
    • EFS_FILE_SYSTEM_ID 替換為 EFS 檔案系統 ID。例如:fs-12345678a

    存取點

    將下列 YAML 資訊清單複製到名為 efs-volume.yaml 的檔案。 資訊清單會參照您先前建立的 EFS 儲存空間類別。

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: VOLUME_NAME
    spec:
      capacity:
        # Note: storage capacity is not used by the EFS CSI driver.
        # It is required by the PersistentVolume spec.
        storage: 5Gi
      volumeMode: Filesystem
      accessModes:
        - ReadWriteMany
      persistentVolumeReclaimPolicy: Retain
      storageClassName: "" # storageClassName is not required, see note in the following section.
      claimRef:
        name: CLAIM_NAME
        namespace: default
      csi:
        driver: efs.csi.aws.com
        volumeHandle: EFS_FILE_SYSTEM_ID::ACCESS_POINT_ID
    

    更改下列內容:

    • VOLUME_NAME 替換為永久磁碟區的名稱。
    • CLAIM_NAME 替換為您要用於 PersistentVolumeClaim 的名稱。
    • EFS_FILE_SYSTEM_ID 替換為 EFS 檔案系統 ID。例如:fs-12345678a
    • ACCESS_POINT_ID 改成存取點的 ID。例如:fsap-1234567890abcde
  3. 將 YAML 套用至使用者叢集。

    env HTTPS_PROXY=http://localhost:8118 \
      kubectl apply -f efs-volume.yaml
    

    輸出內容會確認 PersistentVolume 的建立作業。

    persistentvolume/VOLUME_NAME created
    

建立 PersistentVolumeClaim

如要將 EFS 檔案系統用於工作負載,請建立 PersistentVolumeClaim。

  1. 如要建立 PersistentVolumeClaim,請將下列 YAML 資訊清單複製到名為 efs-claim.yaml 的檔案。

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: CLAIM_NAME
    spec:
      accessModes:
        - ReadWriteMany
      storageClassName: ""
      resources:
        requests:
          storage: 5Gi
    

    CLAIM_NAME 替換為 PersistentVolumeClaim 的名稱。例如:efs-claim1

  2. 將 YAML 套用至使用者叢集。

    env HTTPS_PROXY=http://localhost:8118 \
      kubectl apply -f efs-claim.yaml
    

    輸出內容會確認 PersistentVolumeClaim 的建立作業。

    persistentvolumeclaim/CLAIM_NAME created
    

建立 StatefulSet

建立 PersistentVolumeClaim 後,即可在工作負載中使用。本節中的步驟會建立範例 StatefulSet,並掛接 EFS 檔案系統。您也可以在 spec.volumes 中參照要求,將 PersistentVolumeClaim 用於其他工作負載類型,例如 Pod 和 Deployment。

如要建立 StatefulSet 並掛接 PersistentVolumeClaim 中參照的 EFS 檔案系統,請按照下列步驟操作。

  1. 將下列 YAML 資訊清單複製到名為 efs-statefulset.yaml 的檔案。 這個資訊清單範例會啟動 Ubuntu Linux 容器,並將 EFS 檔案系統掛接至 /efs-data。容器每五秒會寫入 EFS 檔案系統上名為 out.txt 的檔案。

    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: efs-shell
    spec:
      selector:
        matchLabels:
          app: test-efs
      serviceName: efs-app
      replicas: 1
      template:
        metadata:
          labels:
            app: test-efs
        spec:
          terminationGracePeriodSeconds: 10
          containers:
          - name: linux
            image: ubuntu:bionic
            command: ["/bin/sh"]
            args: ["-c", "while true; do echo $(date -u) >> /efs-data/out.txt; sleep 5; done"]
            volumeMounts:
            - name: efs-volume
              mountPath: /efs-data
          volumes:
          - name: efs-volume
            persistentVolumeClaim:
              claimName: CLAIM_NAME
    

    更改下列內容:

    • CLAIM_NAME 換成您先前指定的 PersistentVolumeClaim 名稱。例如:efs-claim1
  2. 將 YAML 套用至使用者叢集。

    env HTTPS_PROXY=http://localhost:8118 \
     kubectl apply -f efs-statefulset.yaml
    

    輸出內容會確認 StatefulSet 的建立作業。

    statefulset.apps/efs-shell created
    

    StatefulSet 可能需要幾分鐘才能下載容器映像檔並啟動。

  3. 使用 kubectl get pods 確認 StatefulSet 的 Pod 處於 Running 狀態。

    env HTTPS_PROXY=http://localhost:8118 \
      kubectl get pods -l app=test-efs
    

    輸出內容會顯示 Pod 名稱和狀態。在以下回應中,Pod 的名稱為 efs-shell-0

    NAME          READY   STATUS    RESTARTS   AGE
    efs-shell-0   1/1     Running   0          1m
    
  4. Pod 處於「Running」狀態後,請使用 kubectl exec 連線至代管 StatefulSet 的 Pod。

    env HTTPS_PROXY=http://localhost:8118 \
      kubectl exec -it efs-shell-0 -- bash
    

    kubectl 指令會在 Pod 上啟動殼層。

  5. 如要確認 EFS 檔案系統是否已掛接,請使用 tail 指令檢查 out.txt 檔案的內容。

    tail /efs-data/out.txt
    

    輸出內容會顯示最近的時間 (以世界標準時間為準)。

  6. 使用 exit 指令與 Pod 中斷連線。

    exit
    

    殼層會返回本機電腦。

  7. 如要移除 StatefulSet,請使用 kubectl delete

    env HTTPS_PROXY=http://localhost:8118 \
      kubectl delete -f efs-statefulset.yaml
    

正在清除所用資源

如要移除您在先前章節中建立的資源,請執行下列指令:

env HTTPS_PROXY=http://localhost:8118 \
  kubectl delete -f efs-statefulset.yaml
env HTTPS_PROXY=http://localhost:8118 \
  kubectl delete -f efs-claim.yaml
env HTTPS_PROXY=http://localhost:8118 \
  kubectl delete -f efs-volume.yaml

後續步驟