使用 Terraform 在 Compute Engine VM 中建立 Google Distributed Cloud Hybrid 叢集

本文說明如何使用 Terraform 在 Compute Engine 上設定 VM,以便安裝及試用高可用性 (HA) 模式的 Google Distributed Cloud。如要瞭解如何使用 Google Cloud CLI 執行這項操作,請參閱「在 Compute Engine VM 上試用 Google Distributed Cloud」。

您可以快速試用 Google Distributed Cloud,無須準備任何硬體。提供的 Terraform 指令碼會在 Compute Engine 上建立 VM 網路,可用於執行 Google Distributed Cloud。在本教學課程中,我們使用混合叢集部署模型。

請完成下列步驟,執行範例叢集:

  1. 執行 Terraform 指令碼,在 Compute Engine 上設定 VM 網路
  2. 部署混合式叢集
  3. 驗證叢集

事前準備

部署作業需要下列資源:

在 Compute Engine 上設定 VM 網路

在本節中,您將使用 anthos-samples 存放區中的 Terraform 指令碼。指令碼會使用下列資源設定 Compute Engine:

  • 部署混合式叢集所需的六部 VM:
    • 一個管理員 VM,用於將混合式叢集部署到其他機器。
    • 三個 VM,分別對應執行混合式叢集控制層所需的三個控制層節點。
    • 兩個 VM,分別對應兩個工作站節點,用於在混合式叢集上執行工作負載。
  • 所有節點之間的 VxLAN 疊加網路,可模擬 L2 連線。
  • 管理 VM 透過 SSH 存取控制平面工作站節點。

使用 Compute Engine VM 在 Google Cloud 上建立裸機基礎架構

如要變更叢集中的節點數量,請將新的節點名稱新增至 instance_count Terraform 變數:

###################################################################################
# The recommended instance count for High Availability (HA) is 3 for Control plane
# and 2 for Worker nodes.
###################################################################################
variable "instance_count" {
  description = "Number of instances to provision per layer (Control plane and Worker nodes) of the cluster"
  type        = map(any)
  default = {
    "controlplane" : 3
    "worker" : 2
  }
}

  1. 下載 anthos-bm-gcp-terraform 範例的 Terraform 指令碼:

    git clone https://github.com/GoogleCloudPlatform/anthos-samples
    cd anthos-samples/anthos-bm-gcp-terraform
    
  2. 更新 terraform.tfvars.sample 檔案,加入環境專屬變數:

    project_id       = "PROJECT_ID"
    region           = "GOOGLE_CLOUD_REGION"
    zone             = "GOOGLE_CLOUD_ZONE"
    credentials_file = "PATH_TO_GOOGLE_CLOUD_SERVICE_ACCOUNT_KEY_FILE"
    
  3. terraform.tfvars.sample 檔案重新命名為 Terraform 用於變數檔案的預設名稱:

    mv terraform.tfvars.sample terraform.tfvars
    
  4. 將範例目錄初始化為 Terraform 工作目錄。 這會設定必要的 Terraform 狀態管理設定,類似於 git init

    terraform init
    
  5. 建立 Terraform 執行計畫。這個步驟會比較資源的狀態、驗證指令碼,並建立執行計畫:

    terraform plan
    
  6. 套用 Terraform 指令碼中說明的變更。這個步驟會在指定供應商(本例中為 Google Cloud) 上執行計畫,以達到資源的目標狀態:

    terraform apply  # when prompted to confirm the Terraform plan, type 'Yes' and enter
    

部署混合式叢集

Terraform 執行完成後,即可部署混合式叢集。

  1. 使用 SSH 連線至管理主機:

    gcloud compute ssh tfadmin@cluster1-abm-ws0-001 --project=PROJECT_ID --zone=GOOGLE_CLOUD_ZONE
    

    您可以忽略任何有關更新 VM 的訊息,並完成本教學課程。如果您打算將 VM 做為測試環境,建議您按照 Ubuntu 說明文件所述,更新 OS 或升級至下一個版本。

  2. 執行下列程式碼區塊,在設定的 Compute Engine VM 上建立 cluster1 混合式叢集:

    sudo ./run_initialization_checks.sh && \
    sudo bmctl create config -c cluster1 && \
    sudo cp ~/cluster1.yaml bmctl-workspace/cluster1 && \
    sudo bmctl create cluster -c cluster1
    

執行 bmctl 指令會開始設定新的混合式叢集。包括對節點執行前置檢查、建立管理員和使用者叢集,以及使用 Connect Agent 向 Google Cloud 註冊叢集。整個設定程序最多可能需要 15 分鐘。建立叢集時,您會看到下列輸出內容:

    Created config: bmctl-workspace/cluster1/cluster1.yaml
    Creating bootstrap cluster... OK
    Installing dependency components... OK
    Waiting for preflight check job to finish... OK
    - Validation Category: machines and network
            - [PASSED] 10.200.0.3
            - [PASSED] 10.200.0.4
            - [PASSED] 10.200.0.5
            - [PASSED] 10.200.0.6
            - [PASSED] 10.200.0.7
            - [PASSED] gcp
            - [PASSED] node-network
    Flushing logs... OK
    Applying resources for new cluster
    Waiting for cluster to become ready OK
    Writing kubeconfig file
    kubeconfig of created cluster is at bmctl-workspace/cluster1/cluster1-kubeconfig, please run
    kubectl --kubeconfig bmctl-workspace/cluster1/cluster1-kubeconfig get nodes
    to get cluster node status.
    Please restrict access to this file as it contains authentication credentials of your cluster.
    Waiting for node pools to become ready OK
    Moving admin cluster resources to the created admin cluster
    Flushing logs... OK
    Deleting bootstrap cluster... OK

驗證及與叢集互動

您可以在管理員機器的 bmctl-workspace 目錄中找到叢集的 kubeconfig 檔案。如要驗證部署作業,請完成下列步驟。

  1. 如果與管理主機中斷連線,請使用 SSH 連線至主機:

    # You can copy the command from the output of the Terraform execution above
    gcloud compute ssh tfadmin@cluster1-abm-ws0-001 --project=PROJECT_ID --zone=GOOGLE_CLOUD_ZONE
    
  2. 使用叢集設定檔的路徑設定 KUBECONFIG 環境變數,以便在叢集上執行 kubectl 指令:

    export CLUSTER_ID=cluster1
    export KUBECONFIG=$HOME/bmctl-workspace/$CLUSTER_ID/$CLUSTER_ID-kubeconfig
    kubectl get nodes
    

    您應該會看到叢集的節點已列印出來,類似下列輸出內容:

    NAME          STATUS   ROLES    AGE   VERSION
    cluster1-abm-cp1-001   Ready    master   17m   v1.18.6-gke.6600
    cluster1-abm-cp2-001   Ready    master   16m   v1.18.6-gke.6600
    cluster1-abm-cp3-001   Ready    master   16m   v1.18.6-gke.6600
    cluster1-abm-w1-001    Ready    <none>   14m   v1.18.6-gke.6600
    cluster1-abm-w2-001    Ready    <none>   14m   v1.18.6-gke.6600
    

從 Google Cloud 控制台登入叢集

如要在 Google Cloud 控制台中觀察工作負載,您必須登入叢集。

如需登入叢集的操作說明和詳細資訊,請參閱「 Google Cloud 透過控制台使用叢集

清除所用資源

你可以透過兩種方式清除叢集設定。

主控台

Terraform

  • 請先取消註冊叢集,再刪除 Terraform 建立的所有資源。
# Use SSH to connect to the admin host
gcloud compute ssh tfadmin@cluster1-abm-ws0-001 --project=PROJECT_ID --zone=GOOGLE_CLOUD_ZONE

# Reset the cluster
export CLUSTER_ID=cluster1
export KUBECONFIG=$HOME/bmctl-workspace/$CLUSTER_ID/$CLUSTER_ID-kubeconfig
sudo bmctl reset --cluster $CLUSTER_ID

# log out of the admin host
exit
  • 使用 Terraform 刪除所有資源。
terraform destroy --auto-approve