部署批次機器學習工作負載


本教學課程說明如何使用 Google Kubernetes Engine (GKE) 管理容錯批次工作負載,同時維持低成本。請參閱本教學課程,瞭解如何使用 Spot Pod 節省成本,以及如何在 GKE 上設定叢內 Redis 工作佇列。

背景

批次工作負載通常是指已定義了開始點和完成點的處理程序。如果您的架構涉及擷取、處理及輸出資料,而非使用原始資料,建議您考慮在 GKE 上使用批次工作負載。機器學習、人工智慧和高效能運算 (HPC) 等領域有不同類型的批次工作負載,例如離線模型訓練、批次預測、資料分析、實體系統模擬和影片處理。

設計容器化批次工作負載時,您可以善用下列 GKE 優勢:

  • 開放標準、廣大社群和代管服務。
  • 有效調度工作負載和基礎架構,並運用專用運算資源,提高成本效益。
  • 容器化技術可提供隔離和可攜性,讓您將雲端當做溢出容量使用,同時維持資料安全。
  • 爆量容量可用性,隨後快速縮減 GKE 叢集。

目標

本教學課程適合想要使用具成本效益且可擴充的架構,在 GKE 中執行批次機器學習工作負載的機器學習工程師或數據資料學家:

本教學課程包含下列步驟:

  1. 建立 GKE Autopilot 叢集。您也可以在本教學課程中使用 GKE Standard 叢集。
  2. 建立 Filestore NFS 磁碟區。
  3. 建立叢集中的 Redis 工作佇列。
  4. 將資料集轉移至 NFS 磁碟區,並排入佇列以供工作負載使用。
  5. 在 GKE 叢集中執行範例批次機器學習工作負載。

費用

在本文件中,您會使用 Google Cloud的下列計費元件:

如要根據預測用量估算費用,請使用 Pricing Calculator

初次使用 Google Cloud 的使用者可能符合免費試用資格。


如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導)

逐步引導


事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Compute Engine, GKE, and Filestore APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Compute Engine, GKE, and Filestore APIs.

    Enable the APIs

  8. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

準備環境

  1. 複製本教學課程中使用的範例存放區:

    git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples
    cd kubernetes-engine-samples/batch/aiml-workloads
    
  2. 建立 GKE Autopilot 叢集:

    gcloud container clusters create-auto batch-aiml \
        --region=us-central1
    

    這個步驟最多可能需要 5 分鐘才能完成。

使用網路檔案系統 (NFS) 設定資料集儲存空間

機器學習工作負載需要儲存資料集和輸出檔案的解決方案。在本節中,您將建立 Filestore 執行個體,並使用 PersistentVolumePersistentVolumeClaim 提供執行個體存取權。

詳情請參閱如何設計最佳儲存空間策略,以及如何從 GKE 叢集存取 Filestore 執行個體

建立 Filestore 執行個體

  1. 建立 Filestore 執行個體:

    gcloud filestore instances create batch-aiml-filestore \
        --zone=us-central1-b \
        --tier=BASIC_HDD \
        --file-share=name="NFSVol",capacity=1TB \
        --network=name="default"
    

    此指令會指定下列選項:

    • tier:Filestore 執行個體的服務層級。 本範例使用基本層級。如要瞭解其他選項,請參閱「服務等級」。

    • network=name:Filestore 執行個體的虛擬私有雲 (VPC) 網路名稱。GKE 叢集必須與 Filestore 執行個體位於相同的虛擬私有雲網路。

    • capacity:磁碟區的所需大小。使用資源數量一節所述的其中一個支援單位指定儲存空間值。

  2. 確認 Filestore 執行個體已部署:

    gcloud filestore instances list \
        --project=PROJECT_ID \
        --zone=us-central1-b
    

    PROJECT_ID 替換為專案 ID。 Google Cloud

    輸出結果會與下列內容相似:

    INSTANCE_NAME: batch-aiml-filestore
    LOCATION: us-central1-b
    TIER: BASIC_HDD
    CAPACITY_GB: 1024
    FILE_SHARE_NAME: NFSVol
    IP_ADDRESS: 203.0.113.54
    STATE: READY
    CREATE_TIME: 2022-03-15T18:23:51
    
  3. 請記下「IP_ADDRESS」欄位中的值,以供下一節使用。

建立 PersistentVolume

Kubernetes PersistentVolume 規格可讓 GKE 叢集連結至 Filestore 執行個體。

  1. 使用 Filestore 執行個體 IP 位址更新 kubernetes-manifests/persistent-volume.yaml 檔案:

    sed -i "\
      s/<FILESTORE_IP_ADDRESS>/IP_ADDRESS/g" \
      kubernetes-manifests/persistent-volume.yaml
    

    IP_ADDRESS 替換為您在上一節建立 Filestore 執行個體時記下的 IP 位址。

  2. 部署 PersistentVolume:

    kubectl apply -f kubernetes-manifests/persistent-volume.yaml
    

建立 PersistentVolumeClaim

Kubernetes PersistentVolumeClaim 可讓 Kubernetes Pod 和工作存取 PersistentVolume 的儲存空間資源。

部署 PersistentVolumeClaim:

kubectl apply -f kubernetes-manifests/persistent-volume-claim.yaml

使用 PersistentVolumeClaim

在 GKE 叢集上設定 PersistentVolume 和 PersistentVolumeClaim 後,您就可以設定 Redis 伺服器和批次作業,以使用 PersistentVolumeClaim。這會顯示為可掛接的儲存空間磁碟區。

檢查 kubernetes-manifests/redis-pod.yamlkubernetes-manifests/workload.yaml 檔案。資訊清單設定類似於下列內容:

  spec:
  
  containers:
  - name: workload
    image: "us-central1-docker.pkg.dev/gke-batch-aiml/batch-aiml-docker-repo/workload"
    volumeMounts:
    - mountPath: /mnt/fileserver
      name: workload-pvc
  volumes:
  - name: workload-pvc
    persistentVolumeClaim:
      claimName: fileserver-claim
      readOnly: false

在這個資訊清單中:

  • spec.volumes 指定要使用的 PersistentVolumeClaim。
  • spec.containers.volumeMounts 會指定 Pod 可存取 Filestore 檔案共用區的本機檔案路徑。

設定 Redis 工作佇列

工作負載會批次處理資料,以便反覆訓練詐欺偵測模型。如要管理目前正在處理或仍在佇列中的資料集,請將 Redis 伺服器部署至 GKE 叢集。

在本教學課程中,您會啟動單一 Redis 執行個體。如要以可擴充且備援的方式部署 Redis,請參閱「使用 Redis 和 PHP 建立多層級網頁應用程式」。

  1. 部署 Redis 伺服器規格。

    kubectl apply -f kubernetes-manifests/redis-pod.yaml
    
  2. 請確認 Pod 正在執行中:

    kubectl get pods
    

    輸出結果會與下列內容相似:

    NAME           READY   STATUS    RESTARTS   AGE
    redis-leader   1/1     Running   0          118s
    

    Pod 最多可能需要兩分鐘才會開始運作。

  3. 將包含訓練和測試資料集的檔案轉移至 NFS 磁碟區。

    sh scripts/transfer-datasets.sh
    

    這個指令碼會將程式碼範例存放區中的檔案複製到 redis-leader Pod 上的 /mnt/fileserver/datasets/ 目錄。

  4. 填入 Redis 佇列。

    sh scripts/queue-jobs.sh
    

    這個指令碼會將訓練資料集的檔案路徑推送到 Redis 資料庫中名為 datasets 的清單。工作負載會使用這個佇列來找出下一個要處理的資料集。

  5. 部署服務,讓 Redis 伺服器在 GKE 叢集中可供偵測。

    kubectl apply -f ./kubernetes-manifests/redis-service.yaml
    

執行批次工作負載

到目前為止,您已準備好 GKE 叢集、Redis 工作佇列和檔案共用。現在可以執行批次工作負載。

在本節中,您將使用範例工作負載的容器映像檔,透過批次金融交易資料訓練詐欺偵測模型。訓練流程可摘要如下:

  1. Redis 用戶端會在 Redis 佇列中領取工作 (資料集的檔案路徑),並在完成後將其從佇列中移除。

  2. 模型訓練管理程式類別 FraudDetectionModelTrainer 會載入新的一批資料,並視需要載入機器學習模型的儲存狀態。資料集可用來修正模型 (稱為「暖啟動」訓練的程序)。

  3. 模型的新狀態,以及批次詳細資料與效能分數的報告,會儲存在 Filestore NFS 磁碟區中,您可以透過 PersistentVolumeClaim 在 GKE 叢集中存取。

如要瞭解詳情,請探索原始碼

定義 Job

下列資訊清單說明批次工作負載映像檔的 Kubernetes 工作。Kubernetes 中的 Job 控制器會建立一或多個 Pod,並確保這些 Pod 成功執行特定工作。

apiVersion: batch/v1
kind: Job
metadata:
  name: workload
spec:
  parallelism: 1
  template:
    metadata:
      name: workload
    spec:
      nodeSelector:
        cloud.google.com/gke-spot: "true"
      containers:
      - name: workload
        image: "us-docker.pkg.dev/google-samples/containers/gke/batch-ml-workload"
        volumeMounts:
        - mountPath: /mnt/fileserver
          name: workload-pvc
      volumes:
      - name: workload-pvc
        persistentVolumeClaim:
          claimName: fileserver-claim
          readOnly: false
      restartPolicy: OnFailure

部署工作負載

  1. 部署 Job:

    kubectl apply -f ./kubernetes-manifests/workload.yaml
    
  2. 檢查 workload-XXX Pod 的狀態是否為 Completed

    watch kubectl get pods
    

    這可能需要幾秒鐘。按下 Ctrl+C 即可返回指令列。

    輸出結果會與下列內容相似:

    NAME             READY   STATUS      RESTARTS   AGE
    redis-leader     1/1     Running     0          16m
    workload-4p55d   0/1     Completed   0          83s
    
  3. 查看 workload 工作的記錄檔:

    kubectl logs job/workload
    

    輸出結果會與下列內容相似:

    Worker with sessionID: b50f9459-ce7f-4da8-9f84-0ab5c3233a72
    Initial queue state: empty=False
    Processing dataset: datasets/training/2018-04-04.pkl
    Processing dataset: datasets/training/2018-04-03.pkl
    Processing dataset: datasets/training/2018-04-02.pkl
    Processing dataset: datasets/training/2018-04-01.pkl
    Queue empty, exiting
    

    .pkl 檔案是資料集的序列化結果,內含一批標示為有效或詐欺的信用卡交易。workload 工作會逐一處理這些檔案,解壓縮資料集並用於訓練機器學習模型,然後再從 Redis 佇列中將其移除。工作負載會繼續分批處理資料,直到 Redis 佇列清空為止,然後成功結束。

探索 NFS 磁碟區

工作負載運作期間,會在已掛接的 NFS 磁碟區中建立檔案,供叢集中的其他批次工作或線上應用程式存取。

  1. 列出工作負載建立的檔案:

    kubectl exec --stdin --tty redis-leader -- /bin/sh -c "ls -1 /mnt/fileserver/output"
    

    輸出內容應如下所示:

    model_cpt_2018-04-01.pkl
    model_cpt_2018-04-02.pkl
    model_cpt_2018-04-03.pkl
    model_cpt_2018-04-04.pkl
    report.txt
    

    訓練模型的查核點 (如 model_cpt_XXX.pkl 等檔案名稱) 和模型效能報告 (report.txt) 是建立於 NFS 磁碟區的 /mnt/fileserver/output 目錄中。

  2. 檢查模型效能報告:

    kubectl exec --stdin --tty redis-leader -- /bin/sh -c "cat /mnt/fileserver/output/report.txt"
    

    以下是輸出內容的片段:

    Report generated on: 2022-02-09 14:19:42.303619
    Training dataset: 2018-04-04.pkl
    Model checkpoint: model_cpt_2018-04-04.pkl
    ---
    Accuracy on training data: 0.9981112277019937
    Accuracy on testing data: 0.9977204434773599
    

    檔案中詳述許多資訊,包括訓練時間、所用資料集、所達到的準確率,以及與訓練有關的模型查核點的檔案名稱。

如要進一步瞭解 NFS 磁碟區,請參閱 Filestore 指南

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。

移除個別資源

如要移除為本教學課程建立的個別資源,請執行下列指令。

  1. 刪除叢集:

    gcloud container clusters delete batch-aiml \
        --region=us-central1
    
  2. 刪除 Filestore 執行個體:

    gcloud filestore instances delete batch-aiml-filestore \
        --zone=us-central1-b
    

刪除專案

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

後續步驟