為 VM 建立 Terraform 部署模組

本頁說明如何建立虛擬機器 (VM) 和 VM 的 Terraform 部署模組。

建立授權的 VM 映像檔

  1. 為確保開發 VM 不受影響,請執行下列 gcloud 指令,建立 VM 所用磁碟的副本:
    gcloud compute disks create CLONE_DISK \ --description="cloned disk" \ --source-disk=projects/PROJECT/zones/ZONE/disks/SOURCE_VM_DISK
  2. 建立 VM 的副本。詳情請參閱「建立與現有 VM 類似的 VM」。
  3. 在「Advanced options」(進階選項) 區段中,附加複製的磁碟。
  4. 建立 VM。
  5. 確認伺服器正在這個 VM 上執行。開機指令碼可確保 VM 正常運作。
  6. 停止並移除 Docker 中的連接器服務。
  7. 移除主目錄和執行伺服器不需要的任何其他檔案。這個磁碟會用於建立 VM 映像檔,之後客戶也能存取。這個 VM 只能包含執行服務所需的程式庫。
  8. 編輯 VM,然後選取「保留磁碟」刪除規則
  9. 刪除這個 VM。
  10. 如要建立授權的映像檔,請從製作人入口網站取得授權。前往 Producer Portal
  11. 建立新的虛擬機器產品。前往「部署套件」部分,然後複製 VM 授權。
  12. 如要建立授權的 VM 映像檔,請執行下列指令:
  13. gcloud compute images create VM_IMAGE_NAME \
     --project PUBLIC_PROJECT_NAME \
     --source-disk projects/DEV_PROJECT_NAME/zones/ZONE/disks/SOURCE_VM_DISK \
     --licenses LICENSE \
     --description VM_IMAGE_DESCRIPTION

    詳情請參閱「建構 VM」。

建立 Terraform 部署模組

Marketplace 提供 Autogen 工具,可產生 Terraform 模組,在 Marketplace 上部署 VM 映像檔。

這項工具會使用 Autogen proto 定義產生部署作業。詳情請參閱設定範例

  1. 新增解決方案詳細資料:
  2. solutionInfo:
      name: 
      packagedSoftwareGroups:
        - type: SOFTWARE_GROUP_OS
          components:
            - name: Ubuntu
              version: 16.04 LTS
        - components:
            - name: Apache
              version: 2.4.23
  3. 如果是連接器,請建立含有 VM 的模組。在公開專案中輸入映像檔的專案名稱、VM 映像檔名稱和標籤
  4. 將 n2d-standard-2 設為預設機器類型,CPU 數量下限為 2,RAM 為 8 GB。N2d-standard 最適合中等流量的網路伺服器。預設 VM 可設定為 2 個核心。如果客戶預期流量會增加,可以在從 Marketplace 建立 VM 時變更這項設定。如要瞭解不同機器類型的定價資訊,請參閱 VM 執行個體定價
        machineType:
          # Check http://cloud.google.com/compute/docs/machine-types for all available types
          defaultMachineType:
            gceMachineType: n2d-standard-2
          # Minimum CPU and memory constraints for the machine type to run your solution properly
          minimum:
            cpu: 2
            ramGb: 8
  5. 使用 pd-standard 做為預設磁碟類型。
        bootDisk:
          diskType:
            defaultType: pd-standard
  6. 將開機指令碼新增至自動產生設定。將啟動指令碼字串化,並新增至 bashScriptContent。
    gceStartupScript:
       bashScriptContent: 
  7. 定義輸入欄位,這些欄位會在建立 VM 時做為客戶的輸入內容。
        deployInput:
          sections:
            # The place int he Deployment Manager config that this section will appear. More details in deployment_package_autogen_spec.proto
            - placement: CUSTOM_TOP
              name: CONNECTOR_SETTINGS
              title: Connectors settings
              description: Connectors settings
              tooltip: Setting with which connector VM will be running
              # List of input fields that this section has
              fields:
                - required: false
                  name: TEST_DATA
                  title: Test Data
                  description: Test data
                  tooltip: random data for testing
                  string_box: {}
                - required: false
                  name: PORT
                  title: Port to run service
                  tooltip: Port to run connector service
                  string_box: {}
                - required: false
                  name: LOG_LEVEL
                  title: Log level
                  description: Log level for the web service. Allowed values are DEBUG, INFO, WARNING, ERROR. Default is INFO
                  string_box: {}

    將刊登位置保留為 CUSTOM_TOP,並將名稱保留為 CONNECTOR_SETTINGS

    定義欄位和所需輸入內容類型。在本範例中,Marketplace 連接器的客戶會看到「測試資料」和「要執行的服務連接埠」欄位。

  8. 定義 Compute Engine 中繼資料項目。定義 VM 中繼資料時,將輸入欄位的名稱對應至中繼資料鍵。
        gce_metadata_items:
          - key: CONNECTOR_ENV_PORT
            value_from_deploy_input_field: PORT
          - key: CONNECTOR_ENV_TEST_DATA
            value_from_deploy_input_field: TEST_DATA
    - key: CONNECTOR_ENV_LOG_LEVEL
            value_from_deploy_input_field: LOG_LEVEL
  9. 新增步驟詳細資料,這些資訊會在部署後向顧客顯示。
        postDeploy:
          # List of suggested action items for users, after deployment is successful
          actionItems:
            - heading: Create the Private service attachment
              description: Now the connector service is running on the VM, please create the PSC service attachment to create the connection. Follow https://cloud.google.com/integration-connectors/docs/configure-psc for the detailed guide.
            - heading: Create the connection
              description: After creating the connectors endpoint attachment, use the endpoint attachment to create a connection to the partner connector.
  10. 如要設定 autogen,請執行下列指令:
    alias autogen='docker run \
      --rm \
      --workdir /mounted \
      --mount type=bind,source="$(pwd)",target=/mounted \
      --user $(id -u):$(id -g) \
      gcr.io/cloud-Marketplace-tools/dm/autogen'
    
    autogen --help
  11. 如要產生 Terraform 模組,請執行下列指令:
    mkdir solution_folder
    
    autogen \
      --input_type YAML \
      --single_input example-config/solution.yaml \
      --output_type PACKAGE \
      --output solution_folder

    solution_folder 包含 Terraform 模組。在 Marketplace 上發布產品時,會使用這個模組。

後續步驟