將 Connect Gateway 與 Cloud Build 整合
本基本教學課程說明如何將 Cloud Build 與 Connect 閘道整合,為在多個不同環境中執行的 GKE 叢集建立 CI/CD 管道。
本教學課程假設您已熟悉連線閘道指南中的先前章節,也熟悉 Cloud Build。這些操作說明會使用 cloud-sdk
建構工具映像檔,因此需要進行一些簡單的指令碼編寫 (如下所示)。
事前準備
確認您已安裝下列指令列工具:
- 最新版 Google Cloud CLI,其中包含
gcloud
,這是與 Google Cloud互動的指令列工具。 kubectl
,這是與 Kubernetes 互動的指令列工具。
如果您使用 Cloud Shell 做為與 Google Cloud互動的 Shell 環境,系統會為您安裝這些工具。
- 最新版 Google Cloud CLI,其中包含
確認您已初始化 gcloud CLI,以便搭配專案使用。
如設定指南所述,確認已為專案啟用 Connect 閘道和其他必要 API。
1. 將 IAM 角色授予 Cloud Build 服務帳戶
根據預設,Cloud Build 會使用服務帳戶執行所有必要工作,地址格式為 Google Cloud MY_PROJECT_NUMBER @cloudbuild.gserviceaccount.com
。您可以在 Google Cloud 主控台的「Cloud Build」-「設定」下方,找到專案的服務帳戶電子郵件地址。
請按照閘道設定指南中的「授予 IAM 權限」一節操作,在專案中授予這個帳戶必要角色。
2. 為 Cloud Build 服務帳戶指定 RBAC 政策
請按照閘道設定指南中的「設定 RBAC 政策」一節操作,在要使用的所有叢集上,授予 Cloud Build 服務帳戶適當權限。
強烈建議使用 Policy Controller,在多個叢集上部署及維護 RBAC 政策。
3. 建立 Cloud Build 管道
Cloud Build 工作流程需要 cloudbuild.yaml
檔案來設定管道。以下是簡單範例,可將靜態資訊清單部署至兩個不同的叢集 (一個位於 Google Cloud的 GKE 叢集,另一個位於 VMware)。如要進一步瞭解如何設定 Cloud Build 管道,請參閱 Cloud Build 說明文件。
steps:
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: bash
id: Deploy to cluster on Google Cloud
args:
- '-c'
- |
set -x && \
export KUBECONFIG="$(pwd)/gateway-kubeconfig" && \
gcloud container fleet memberships get-credentials my-gke-cluster && \
kubectl --kubeconfig gateway-kubeconfig apply -f myapp.yaml
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: bash
id: Deploy to cluster on VMware
args:
- '-c'
- |
set -x && \
export KUBECONFIG="$(pwd)/gateway-kubeconfig" && \
gcloud container fleet memberships get-credentials my-vmware-cluster && \
kubectl --kubeconfig gateway-kubeconfig apply -f myapp.yaml
您可以將任何所需的工作流程放入 myapp.yaml
,設定叢集。範例如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
將設定推送至 Git 存放區後,Cloud Build 工作流程會將必要應用程式部署至指定叢集。您也可以設定 Cloud Build 偵測連結的 Git 存放區中的變更,以觸發自動更新或安裝應用程式。
進階用法
由於範例使用標準 Cloud Build 概念,您可以進一步調整及自訂範例,以符合特定 CI/CD 需求。具體來說,如果您想從頭建構映像檔並部署到管道中,可以使用 gke-deploy
建構工具的準備模式。舉例來說,下列 Cloud Build 設定:
- 從 Git 存放區根目錄中的 Dockerfile 建構 Docker 映像檔,並以 Git SHA 標記。
- 將加上標記的映像檔推送至專案的 Container Registry。
- 設定正確的映像檔標記,將輸出資訊清單放在
output/expanded
中,藉此準備manifest
目錄中的 Kubernetes 資訊清單。 - 使用 Connect 閘道部署至內部部署 GKE 叢集。
steps:
- name: 'gcr.io/cloud-builders/docker'
id: "Build Container"
args: ['build', '--tag=gcr.io/$PROJECT_ID/demo-app:$SHORT_SHA', '.']
- name: 'gcr.io/cloud-builders/docker'
id: "Push to GCR"
args: ['push', 'gcr.io/$PROJECT_ID/demo-app:$SHORT_SHA']
- name: "gcr.io/cloud-builders/gke-deploy"
id: "Prepare Manifests"
args:
- prepare
- --filename=manifests/
- --image=gcr.io/$PROJECT_ID/demo-app:$SHORT_SHA
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: bash
id: "Deploy to cluster on VMware
args:
- '-c'
- |
set -x && \
export KUBECONFIG="$(pwd)/gateway-kubeconfig" && \
gcloud container fleet memberships get-credentials my-vmware-cluster && \
kubectl --kubeconfig=gateway-kubeconfig apply -f output/expanded
請注意,在本範例中,我們必須建立映像檔提取密鑰,授權 GKE On-Prem 叢集從 Container Registry 提取映像檔。
如需更多 Cloud Build 用法,請參閱 Cloud Build 說明文件。