背景
批次工作負載通常是指已定義了開始點和完成點的處理程序。如果您的架構涉及擷取、處理及輸出資料,而非使用原始資料,建議您考慮在 GKE 上使用批次工作負載。機器學習、人工智慧和高效能運算 (HPC) 等領域有不同類型的批次工作負載,例如離線模型訓練、批次預測、資料分析、實體系統模擬和影片處理。
設計容器化批次工作負載時,您可以善用下列 GKE 優勢:
- 開放標準、廣大社群和代管服務。
- 有效調度工作負載和基礎架構,並運用專屬運算資源,提高成本效益。
- 容器化技術可提供隔離和可攜性,讓您將雲端當做溢出容量使用,同時維持資料安全性。
- 爆量容量可用性,隨後快速縮減 GKE 叢集。
目標
本教學課程適合想要使用具成本效益且可擴充的架構,在 GKE 中執行批次機器學習工作負載的機器學習工程師或數據資料學家:
本教學課程包含下列步驟:
費用
在本文件中,您會使用下列 Google Cloud的計費元件:
您可以使用 Pricing Calculator,根據預測用量估算費用。
如要直接在 Google Cloud 控制台中,按照這項工作的逐步指南操作,請按一下「Guide me」(逐步引導):
事前準備
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Compute Engine, GKE, and Filestore APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Compute Engine, GKE, and Filestore APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
In the Google Cloud console, 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.
複製本教學課程中使用的範例存放區:
git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples cd kubernetes-engine-samples/batch/aiml-workloads建立 GKE Autopilot 叢集:
gcloud container clusters create-auto batch-aiml \ --location=us-central1這個步驟最多可能需要 5 分鐘才能完成。
建立 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:所需磁碟區大小。請使用資源數量一節所述的其中一個支援單位指定儲存空間值。
確認 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請記下
IP_ADDRESS欄位中的值,以供下一節使用。使用 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 位址。部署 PersistentVolume:
kubectl apply -f kubernetes-manifests/persistent-volume.yamlspec.volumes會指定要使用的 PersistentVolumeClaim。spec.containers.volumeMounts會指定 Pod 可存取 Filestore 檔案共用區的本機檔案路徑。部署 Redis 伺服器規格。
kubectl apply -f kubernetes-manifests/redis-pod.yaml請確認 Pod 正在執行中:
kubectl get pods輸出結果會與下列內容相似:
NAME READY STATUS RESTARTS AGE redis-leader 1/1 Running 0 118sPod 最多可能需要兩分鐘才會開始運作。
將包含訓練和測試資料集的檔案轉移至 NFS 磁碟區。
sh scripts/transfer-datasets.sh這個指令碼會將程式碼範例存放區中的檔案複製到
redis-leaderPod 上的/mnt/fileserver/datasets/目錄。填入 Redis 佇列。
sh scripts/queue-jobs.sh這個指令碼會將訓練資料集的檔案路徑推送到 Redis 資料庫中名為
datasets的清單。工作負載會使用這個佇列來找出下一個要處理的資料集。部署服務,讓 Redis 伺服器在 GKE 叢集中可供偵測。
kubectl apply -f ./kubernetes-manifests/redis-service.yamlRedis 用戶端會在 Redis 佇列中領取工作 (資料集的檔案路徑),並在完成後將其從佇列中移除。
模型訓練管理程式類別
FraudDetectionModelTrainer會載入新的一批資料,並視需要載入機器學習模型的儲存狀態。資料集可用來修正模型 (稱為「暖啟動」訓練的程序)。模型的新狀態,以及批次詳細資料與效能分數的報告,會儲存在 Filestore NFS 磁碟區中,您可以透過 PersistentVolumeClaim 在 GKE 叢集中存取。
部署 Job:
kubectl apply -f ./kubernetes-manifests/workload.yaml檢查
workload-XXXPod 的狀態是否為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查看
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 佇列清空為止,然後成功結束。列出工作負載建立的檔案:
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目錄中。檢查模型效能報告:
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檔案中詳述許多資訊,包括訓練時間、所用資料集、所達到的準確率,以及與訓練有關的模型查核點的檔案名稱。
刪除叢集:
gcloud container clusters delete batch-aiml \ --location=us-central1刪除 Filestore 執行個體:
gcloud filestore instances delete batch-aiml-filestore \ --zone=us-central1-b- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
查看 GKE 說明文件。
進一步探索永久磁碟區。
進一步瞭解 GKE 中的工作。
探索其他 Kubernetes Engine 教學課程。
探索 Google Cloud 的參考架構、圖表和最佳做法。歡迎瀏覽我們的雲端架構中心。
準備環境
使用網路檔案系統 (NFS) 設定資料集儲存空間
機器學習工作負載需要儲存資料集和輸出檔案的解決方案。在本節中,您將建立 Filestore 執行個體,並使用 PersistentVolume 和 PersistentVolumeClaim 提供執行個體存取權。
詳情請參閱如何設計最佳儲存空間策略,以及如何從 GKE 叢集存取 Filestore 執行個體。
建立 Filestore 執行個體
建立 PersistentVolume
Kubernetes PersistentVolume 規格可讓 GKE 叢集連結至 Filestore 執行個體。
建立 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.yaml 和 kubernetes-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
在這個資訊清單中:
設定 Redis 工作佇列
工作負載會批次處理資料,以便反覆訓練詐欺偵測模型。如要管理目前正在處理或仍在佇列中的資料集,請將 Redis 伺服器部署至 GKE 叢集。
在本教學課程中,您會啟動單一 Redis 執行個體。 如要以可擴充且備援的方式部署 Redis,請參閱「使用 Redis 和 PHP 建立多層級網頁應用程式」。
執行批次工作負載
到目前為止,您已準備好 GKE 叢集、Redis 工作佇列和檔案共用。現在可以執行批次工作負載。
在本節中,您會使用範例工作負載的容器映像檔,透過批次金融交易資料訓練詐欺偵測模型。訓練流程可摘要如下:
如要瞭解詳情,請探索原始碼。
定義工作
以下資訊清單說明批次工作負載映像檔的 Kubernetes 工作。Kubernetes 中的 Job 控制器會建立一或多個 Pod,並確保這些 Pod 成功執行特定工作。
部署工作負載
探索 NFS 磁碟區
工作負載運作期間會在已掛接的 NFS 磁碟區中建立檔案,叢集中的其他批次工作或線上應用程式都能存取這些檔案。
如要進一步瞭解 NFS 磁碟區,請參閱 Filestore 指南。
清除所用資源
為避免因為本教學課程所用資源,導致系統向 Google Cloud 收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
移除個別資源
如要移除為本教學課程建立的個別資源,請執行下列指令。