需要在與網際網路中斷連線的情況下設計和執行 Terraform 整合的 Cloud Assembly 使用者,可以遵循此範例來設定其執行階段環境。

備註: 若要取得映像建立的來源,設定過程涉及短暫連線到網際網路。如果無法建立臨時連線,可能需要在中斷連線的站台之外執行這些步驟。

此程序假設您已有自己的 Docker 登錄,並且可以在沒有網際網路連線的情況下存取其存放庫。

建立自訂容器映像

  1. 建置包含 Terraform 提供者外掛程式二進位檔的自訂容器映像。

    下列 Dockerfile 顯示了使用 Terraform GCP 提供者建立自訂映像的範例。

    在 Dockerfile 中下載基礎映像 projects.registry.vmware.com/vra/terraform:latest 時,需要透過網際網路存取 projects.registry.vmware.com 上的 VMware Harbor 登錄。

    防火牆設定或 Proxy 設定可能會導致映像建置失敗。您可能需要啟用對 releases.hashicorp.com 的存取,以下載 Terraform 提供者外掛程式二進位檔。不過,您可以選擇使用私人登錄提供外掛程式二進位檔。

    FROM projects.registry.vmware.com/vra/terraform:latest as final
     
    # Create provider plug-in directory
    ARG plugins=/tmp/terraform.d/plugin-cache/linux_amd64
    RUN mkdir -m 777 -p $plugins
      
    # Download and unzip all required provider plug-ins from hashicorp to provider directory
    RUN cd $plugins \
        && wget -q https://releases.hashicorp.com/terraform-provider-google/3.58.0/terraform-provider-google_3.58.0_linux_amd64.zip \
        && unzip *.zip \
        && rm *.zip
      
    # For "terraform init" configure terraform CLI to use provider plug-in directory and not download from internet
    ENV TF_CLI_ARGS_init="-plugin-dir=$plugins -get-plugins=false"
  2. 在中斷連線的站台上,建置、標記自訂容器映像並推送至您自己的 Docker 存放庫。
  3. 在中斷連線的站台的 Cloud Assembly 中,在基礎結構 > 連線 > 整合下,移至 Terraform 執行階段整合。
  4. 建立或編輯執行階段容器設定,以為自訂容器映像新增存放庫。此範例建立的自訂容器映像名稱為 registry.ourcompany.com/project1/image1:latest
    Terraform 容器映像

在本機託管 Terraform CLI

  1. 下載 Terraform CLI 二進位檔。
  2. 將 Terraform CLI 二進位檔上傳到您的本機 Web 或 FTP 伺服器。
  3. Cloud Assembly 中,移至基礎結構 > 設定 > Terraform 版本
  4. 建立或編輯 Terraform 版本,以便其包含在您本機伺服器上託管的 Terraform CLI 二進位檔 URL。
  5. 如果本機 Web 或 FTP 伺服器需要登入驗證,請選取基本驗證,然後輸入可存取伺服器的使用者名稱和密碼認證。

    若要變更驗證類型,您必須在 Cloud Assembly 中具有雲端管理員角色。

    Terraform CLI URL

設計和部署 Terraform 組態

當執行階段就位時,您可以將 Terraform 組態檔新增到 Git,為這些檔案設計雲端範本,然後進行部署。

若要開始,請參閱在 Cloud Assembly 中準備 Terraform 組態

疑難排解

部署時,請在 Cloud Assembly 中開啟部署。在 [歷程記錄] 索引標籤下,尋找 Terraform 事件,然後按一下右側的顯示記錄。當本機 Terraform 提供者正在運作時,記錄中會出現下列訊息。

Initializing provider plugins

Terraform has been successfully initialized

為了實現更強大的記錄,您可以手動編輯雲端範本代碼以新增 TF_LOG: DEBUG,如下列範例所示。

resources:
  terraform:
    type: Cloud.Terraform.Configuration
    properties:
      providers:
        - name: google
          # List of available cloud zones: gcp/us-west1
          cloudZone: gcp/us-west1
      environment:
        # Configure terraform CLI debug log settings
        TF_LOG: DEBUG
      terraformVersion: 0.12.29
      configurationSource:
        repositoryId: fc569ef7-f013-4489-9673-6909a2791071
        commitId: 3e00279a843a6711f7857929144164ef399c7421
        sourceDirectory: gcp-simple

建立您自己的基礎映像

儘管 VMware 偶爾會更新 projects.registry.vmware.com/vra/terraform:latest 上的基礎映像,但該映像可能已過期並包含漏洞。

若要建置您自己的基礎映像,請改用以下 Dockerfile。

FROM alpine:latest as final
RUN apk add --no-cache git wget curl openssh