使用 Cloud Deploy 將應用程式部署至 Cloud Run
本頁面說明如何使用 Cloud Deploy,將名為 hello 的範例應用程式映像檔,依序交付給兩個 Cloud Run 服務、兩個 Cloud Run 工作,或兩個 Cloud Run 工作人員集區。
在本快速入門導覽課程中,您將執行下列操作:
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
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.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Deploy, Cloud Build, Cloud Run, and Cloud Storage APIs.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
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.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Deploy, Cloud Build, Cloud Run, and Cloud Storage APIs.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init
必要的角色
為確保 Cloud Deploy 服務帳戶具備執行 Cloud Deploy 作業及部署至 Cloud Run 的必要權限,請管理員在專案中將下列 IAM 角色授予 Cloud Deploy 服務帳戶:
- Cloud Run 開發人員 (
roles/run.developer) - IAM 服務帳戶使用者 (
roles/iam.serviceAccountUser) - Cloud Deploy Job Runner (
roles/clouddeploy.jobRunner)
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
準備 Skaffold 設定
Cloud Deploy 會使用 Skaffold 提供詳細資料,說明要部署的內容,以及如何為個別目標正確部署。
在本快速入門導覽中,您會建立 skaffold.yaml 檔案,識別要用於部署範例應用程式的 Cloud Run 服務或工作定義。
開啟終端機視窗。
建立名為
deploy-run-quickstart的新目錄,然後前往該目錄。mkdir deploy-run-quickstart cd deploy-run-quickstart建立名為
skaffold.yaml的檔案,並在當中加入下列內容:服務
apiVersion: skaffold/v4beta7 kind: Config metadata: name: deploy-run-quickstart profiles: - name: dev manifests: rawYaml: - run-service-dev.yaml - name: prod manifests: rawYaml: - run-service-prod.yaml deploy: cloudrun: {}工作
apiVersion: skaffold/v4beta7 kind: Config metadata: name: deploy-run-quickstart profiles: - name: dev manifests: rawYaml: - run-job-dev.yaml - name: prod manifests: rawYaml: - run-job-prod.yaml deploy: cloudrun: {}工作站集區
apiVersion: skaffold/v4beta7 kind: Config metadata: name: deploy-run-quickstart profiles: - name: dev manifests: rawYaml: - run-worker-pool-dev.yaml - name: prod manifests: rawYaml: - run-worker-pool-prod.yaml deploy: cloudrun: {}這個檔案是基本的 Skaffold 設定,用於識別 Cloud Run 服務、工作或工作站集區。如要進一步瞭解這個檔案,請參閱
skaffold.yaml參考資料。
準備 Cloud Run 服務、工作或 worker 集區
在本快速入門導覽課程中,您將在同一個專案中建立兩個不同的 Cloud Run 服務、兩個 Cloud Run 工作,或兩個 Cloud Run worker 集區。Cloud Deploy 也支援跨多個專案部署。此外,我們使用 Skaffold 設定檔,讓您在同一個專案中擁有兩個服務、工作或工作站集區。使用不同專案時,您可能不需要使用 Skaffold 設定檔。
服務
建立名為
run-service-dev.yaml的檔案,並在當中加入下列內容:apiVersion: serving.knative.dev/v1 kind: Service metadata: name: deploy-run-service-dev spec: template: spec: containers: - image: my-app-image這個檔案定義了 Cloud Run 服務。如名稱
deploy-run-service-dev所示,這是您的dev服務,對應於推送管道進度中的第一個目標。建立名為
run-service-prod.yaml的檔案,並在當中加入下列內容:apiVersion: serving.knative.dev/v1 kind: Service metadata: name: deploy-run-service-prod spec: template: spec: containers: - image: my-app-image這個檔案定義了另一個 Cloud Run 服務,且如名稱
deploy-run-service-prod所示,這是您的prod服務,對應於推送管道進度中的第二個目標。
這些檔案是 Cloud Run 服務定義,用於部署應用程式。要部署的容器映像檔會在此處設為預留位置 my-app-image,當您建立版本時,系統會將其替換為特定映像檔。
工作
建立名為
run-job-dev.yaml的檔案,並在當中加入下列內容:apiVersion: run.googleapis.com/v1 kind: Job metadata: name: deploy-run-job-dev spec: template: spec: template: spec: containers: - image: my-app-image這個檔案定義了 Cloud Run 工作。如名稱
deploy-run-job-dev所示,這是您的dev工作,對應於推送管道進度中的第一個目標。建立名為
run-job-prod.yaml的檔案,並在當中加入下列內容:apiVersion: run.googleapis.com/v1 kind: Job metadata: name: deploy-run-job-prod spec: template: spec: template: spec: containers: - image: my-app-image這個檔案定義了另一個 Cloud Run 工作。顧名思義,這是您的
prod工作,對應於推送管道階段中的第二個目標。deploy-run-job-prod
這些檔案是 Cloud Run 工作定義,用於部署應用程式。要部署的容器映像檔在此設為預留位置 my-app-image,您建立版本時,系統會將其替換為特定映像檔。
工作站集區
建立名為
run-worker-pool-dev.yaml的檔案,並在當中加入下列內容:apiVersion: run.googleapis.com/v1 kind: WorkerPool metadata: name: deploy-run-worker-pool-dev annotations: run.googleapis.com/launch-stage: BETA spec: template: spec: containers: - image: my-app-image這個檔案定義了 Cloud Run worker 集區。顧名思義,這是您的
dev工作站集區,對應於推送管道進度中的第一個目標。deploy-run-worker-pool-dev建立名為
run-worker-pool-prod.yaml的檔案,並在當中加入下列內容:apiVersion: run.googleapis.com/v1 kind: WorkerPool metadata: name: deploy-run-worker-pool-prod annotations: run.googleapis.com/launch-stage: BETA spec: template: spec: containers: - image: my-app-image這個檔案定義了另一個 Cloud Run worker 集區。如名稱
deploy-run-worker-pool-prod所示,這是您的prod工作站集區,對應於傳送管道進度中的第二個目標。
這些檔案是 Cloud Run worker 集區定義,用於部署應用程式。要部署的容器映像檔在此設為預留位置 my-app-image,您建立版本時,系統會將其替換為特定映像檔。
建立推送管道和目標
您可以在一個或多個檔案中定義管道和目標。在本快速入門導覽課程中,您將建立單一檔案。
在
deploy-run-quickstart目錄中建立新檔案:clouddeploy.yaml,並加入下列內容:apiVersion: deploy.cloud.google.com/v1 kind: DeliveryPipeline metadata: name: my-run-demo-app-1 description: main application pipeline serialPipeline: stages: