在 GKE 和 Cloud SQL 上部署 Apache Guacamole

Last reviewed 2025-01-09 UTC

本文說明如何在 GKE 和 Cloud SQL 上部署 Apache Guacamole

以下操作說明適用於想在 GKE 和 Cloud SQL 上代管 Guacamole 的伺服器管理員和工程師。本文假設您熟悉如何將工作負載部署至 Kubernetes 和 MySQL 適用的 Cloud SQL。建議您也熟悉 Identity and Access Management 和 Google Compute Engine。

架構

下圖顯示如何使用 IAP 設定負載平衡器,保護在 GKE 中執行的 Guacamole 用戶端執行個體: Google Cloud

已設定 IAP 的 Google Cloud 負載平衡器架構。

Guacamole 用戶端會連線至 guacd 後端服務,該服務會將遠端桌面連線中介至一或多個 Compute Engine VM。指令碼也會部署 Cloud SQL 執行個體,管理 Guacamole 的設定資料。

詳情請參閱「在 GKE 和 Cloud SQL 上使用 Apache Guacamole」。

目標

  • 使用 Terraform 部署基礎架構。
  • 在 Cloud SQL 中建立 Guacamole 資料庫。
  • 使用 Skaffold 將 Guacamole 部署至 GKE 叢集。
  • 透過 Guacamole 測試與 VM 的連線。

費用

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

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

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

完成本文所述工作後,您可以刪除已建立的資源,避免繼續計費。詳情請參閱清除所用資源一節。

事前準備

  1. 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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  2. Verify that billing is enabled for your Google Cloud project.

  3. Enable the Resource Manager, Service Usage, Artifact Registry, and Compute Engine APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

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

    Activate Cloud Shell

    部署基礎架構

    在本節中,您將使用 Terraform 部署下列資源:

    • 虛擬私有雲
    • 防火牆規則
    • GKE 叢集
    • Artifact Registry 存放區
    • MySQL 適用的 Cloud SQL
    • 用於管理 MySQL 資料庫的 VM
    • 服務帳戶

    Terraform 設定也會啟用專案中的 IAP。

    1. 在 Cloud Shell 中,複製 GitHub 存放區。

      git clone https://github.com/GoogleCloudPlatform/guacamole-on-gcp.git
      
    2. 使用 Terraform 部署必要基礎架構:

      cd guacamole-on-gcp/tf-infra
      unset GOOGLE_CLOUD_QUOTA_PROJECT
      terraform init -upgrade
      terraform apply
      
    3. 按照操作說明輸入 Google Cloud 專案 ID。

    4. 如要核准 Terraform 將資源部署至專案的要求,請輸入 yes

      部署所有資源需要幾分鐘才能完成。

    部署 Guacamole 資料庫

    在本節中,您會在 MySQL 適用的 Cloud SQL 中建立 Guacamole 資料庫和資料表,並填入管理員使用者資訊。

    1. 在 Cloud Shell 中設定環境變數,並找出資料庫根密碼:

      cd ..
      source bin/read-tf-output.sh
      

      請記下資料庫根密碼,後續步驟會用到。

      指令碼會從 Terraform 執行作業讀取輸出變數,並設定下列環境變數,這些變數會在整個程序中使用:

      CLOUD_SQL_INSTANCE
      ZONE
      REGION
      DB_MGMT_VM
      PROJECT_ID
      GKE_CLUSTER
      GUACAMOLE_URL
      SUBNET
      
    2. create-schema.sqlinsert-admin-user.sql 指令碼檔案複製到資料庫管理 VM,然後連線至 VM:

      gcloud compute scp \
          --tunnel-through-iap \
          --zone=$ZONE \
          create-schema.sql \
          insert-admin-user.sql \
          $DB_MGMT_VM:
      
      gcloud compute ssh $DB_MGMT_VM \
          --zone=$ZONE \
          --tunnel-through-iap
      

      現在已透過 Cloud Shell 建立資料庫管理 VM 的控制台工作階段。

    3. 安裝 MySQL 用戶端工具:

      sudo apt-get update
      sudo apt-get install -y mariadb-client
      
    4. 連線至 Cloud SQL 並建立資料庫。系統提示輸入密碼時,請使用您在本節稍早記下的根密碼。

      export CLOUD_SQL_PRIVATE_IP=$(curl http://metadata.google.internal/computeMetadata/v1/instance/attributes/cloud_sql_ip -H "Metadata-Flavor: Google")
      mysql -h $CLOUD_SQL_PRIVATE_IP -u root -p
      
    5. 授予資料庫使用者對新建立資料庫的權限:

      CREATE DATABASE guacamole;
      USE guacamole;
      GRANT SELECT,INSERT,UPDATE,DELETE ON guacamole.* TO 'guac-db-user';
      FLUSH PRIVILEGES;
      SOURCE create-schema.sql;
      SOURCE insert-admin-user.sql;
      quit
      
    6. MySQL 指令執行完畢後,請結束 VM 的 SSH 工作階段:

      exit
      

    使用 Skaffold 將 Guacamole 部署至 GKE

    在本節中,您將使用 Skaffold,將 Guacamole 應用程式部署至 GKE 叢集。Skaffold 會處理將 Guacamole 映像檔建構、推送及部署至 GKE 叢集的工作流程。

    1. 在 Cloud Shell 中,使用 Terraform 部署 GKE 設定:

      cd tf-k8s
      terraform init -upgrade
      terraform apply -parallelism=1
      
    2. 取得 GKE 叢集的憑證:

      gcloud container clusters get-credentials \
          --region $REGION $GKE_CLUSTER
      
    3. 從複製的 Git 存放區根目錄執行 Skaffold:

      cd ..
      skaffold --default-repo $REGION-docker.pkg.dev/$PROJECT_ID/guac-repo run
      

      Skaffold 工具會透過 Google Cloud Build 建構 Guacamole 的容器映像檔 (指令列包含指定要將映像檔推送至哪個存放區的標記)。這項工具也會執行 kustomize 步驟,根據 Terraform 執行的輸出內容產生 Kubernetes ConfigMap 和 Secret。

    4. 確認憑證已佈建完成:

      kubectl get -w managedcertificates/guacamole-client-cert \
      -n guacamole \
      -o jsonpath="{.spec.domains[0]} is {.status.domainStatus[0].status}"
      

      憑證佈建作業最多可能需要 60 分鐘。

    5. 憑證佈建完成後,您可以在瀏覽器中造訪網址。

      1. 查看 Terraform 輸出內容中的網址:

        echo $GUACAMOLE_URL
        
      2. 在瀏覽器視窗中,輸入您在上一步取得的網址。

      3. 當 IAP 提示您時,請使用 Google 憑證登入。

        登入後,系統會根據您先前在本程序中執行的 insert-admin-user.sql 指令碼,以管理員權限登入 Guacamole。

    您現在可以透過 Guacamole 使用者介面,根據電子郵件地址新增其他使用者。詳情請參閱 Guacamole 說明文件中的「Administration」。這些額外使用者也需要透過 Google IAM 取得權限,並具備 IAP-secured Web App User 角色。

    測試與 VM 的連線

    部署、設定並成功登入 Guacamole 後,您就可以建立 Windows VM,並透過 Guacamole 連線至新建立的 VM。

    建立 VM

    1. 在 Cloud Shell 中建立 Windows VM,測試與下列項目的連線:

      export TEST_VM=windows-vm
      gcloud compute instances create $TEST_VM \
          --project=$PROJECT_ID \
          --zone=$ZONE \
          --machine-type=n1-standard-1 \
          --subnet=$SUBNET \
          --no-address \
          --image-family=windows-2019 \
          --image-project=windows-cloud \
          --boot-disk-size=50GB \
          --boot-disk-type=pd-standard \
          —-shielded-secure-boot
      

      執行指令後,您可能需要稍候幾分鐘,等待 Windows 完成初始化,再繼續下一個步驟。

    2. 重設您剛建立的 VM 的 Windows 密碼:

      gcloud compute reset-windows-password $TEST_VM \
          --user=admin \
          --zone=$ZONE
      

    新增與 VM 的連線

    1. 在瀏覽器視窗中,輸入「使用 Skaffold 將 Guacamole 部署至 GKE」中的 Guacamole 執行個體網址,然後透過 IAP 登入。
    2. 在 Guacamole UI 中,按一下使用者名稱,然後按一下「設定」
    3. 按一下「連線」分頁下方的「新增連線」
      1. 在「Name」(名稱) 欄位中,輸入連線名稱。
      2. 在「Location」(位置) 欄位中,輸入連線的位置。
      3. 在「通訊協定」下拉式清單中,選取「RDP」
    4. 在「Network」(網路) 下的「Hostname」(主機名稱) 欄位中,輸入您建立的 VM 名稱 windows-vm

      專案 DNS 會將這個主機名稱解析為執行個體的內部 IP 位址。

    5. 在「Authentication」部分,設定下列欄位:

      1. 使用者名稱: admin
      2. 密碼:重設 VM 密碼時取得的密碼
      3. 安全模式: NLA (網路層級驗證)
      4. 忽略伺服器憑證:勾選核取方塊

        Compute Engine Windows VM 佈建時會使用遠端桌面服務的自簽憑證,因此您需要指示 Guacamole 忽略憑證驗證問題。

    6. 按一下 [儲存]

    7. 按一下使用者名稱,然後選取「首頁」

    8. 按一下剛建立的連線,測試連線能力。 幾秒後,您應該會看到 VM 執行個體的桌面。

    如要進一步瞭解如何設定 Guacamole,請參閱 Apache Guacamole 手冊

    清除所用資源

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

    刪除專案

    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.

    刪除新資源

    您不必刪除完整專案,而是可以刪除在本程序中建立的個別資源。請注意,OAuth 同意畫面設定無法從專案中移除,只能修改。

    • 在 Cloud Shell 中,使用 Terraform 刪除資源:

      cd ~/guacamole-on-gcp/tf-k8s
      terraform destroy
      
      cd ~/guacamole-on-gcp/tf-infra
      terraform destroy
      
      gcloud compute instances delete $TEST_VM –-zone=$ZONE
      

    後續步驟

    貢獻者

    作者:Richard Grime | 英國公部門首席架構師

    其他貢獻者: