为虚拟机创建 Terraform 部署模块
本页面介绍了如何为虚拟机 (VM) 创建虚拟机和 Terraform 部署模块。
创建许可的虚拟机映像
- 为确保开发虚拟机不受影响,请运行以下 gcloud 命令,创建虚拟机中所用磁盘的副本:
gcloud compute disks create CLONE_DISK \ --description="cloned disk" \ --source-disk=projects/PROJECT/zones/ZONE/disks/SOURCE_VM_DISK
- 创建虚拟机的克隆。如需了解相关信息,请参阅创建一个与现有虚拟机类似的虚拟机。
- 在高级选项部分中,挂接克隆的磁盘。
- 创建虚拟机。
- 验证服务器是否正在为相应虚拟机运行。启动脚本可确保虚拟机正常运行。
- 停止并移除 Docker 中的连接器服务。
- 移除主目录以及运行服务器不需要的任何其他文件。同一磁盘将用于创建虚拟机映像,并且客户稍后可以访问该磁盘。此虚拟机必须仅包含运行服务所需的库。
- 修改虚拟机,然后选择将删除规则设为保留磁盘
- 删除此虚拟机。
- 如需创建许可映像,请从 Producer Portal 获取许可。访问 Producer Portal。
- 创建新的虚拟机产品。前往部署包部分,然后复制虚拟机许可。
- 如需创建许可虚拟机映像,请运行以下命令:
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
如需了解详情,请参阅构建虚拟机。
创建 Terraform 部署模块
Marketplace 提供 Autogen 工具,用于生成在 Marketplace 上部署虚拟机映像所需的 Terraform 模块。
此工具使用 Autogen proto 定义来生成部署。如需了解详情,请参阅配置示例。
- 添加解决方案的详细信息:
- 对于连接器,请创建一个包含虚拟机的模块。在公共项目中,输入映像的项目名称、虚拟机映像名称和映像的标签。
- 使用 n2d-standard-2 作为默认机器类型,最小 CPU 为 2,RAM 为 8 Gb。N2d-standard 最适合中等流量的 Web 服务器。默认虚拟机的核心数可以设置为 2。如果客户预计流量会增加,可以在从 Marketplace 创建虚拟机期间更改此设置。如需了解不同机器类型的价格,请参阅虚拟机实例价格。
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
- 使用 pd-standard 作为默认磁盘类型。
bootDisk: diskType: defaultType: pd-standard
- 将启动脚本添加到自动生成配置中。将启动脚本字符串化,并将其添加到 bashScriptContent 中。
gceStartupScript: bashScriptContent:
- 定义在创建虚拟机期间将从客户那里获取的输入字段。
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 连接器的客户显示。
- 定义 Compute Engine 元数据项。定义虚拟机元数据时。将输入字段的名称映射到元数据键。
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
- 添加部署后将向客户显示的步骤详细信息。
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.
- 如需设置自动生成功能,请运行以下命令:
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
- 如需生成 Terraform 模块,请运行以下命令:
mkdir solution_folder autogen \ --input_type YAML \ --single_input example-config/solution.yaml \ --output_type PACKAGE \ --output solution_folder
solution_folder 包含 Terraform 模块。此模块将在 Marketplace 上发布产品时使用。
solutionInfo: name:packagedSoftwareGroups: - type: SOFTWARE_GROUP_OS components: - name: Ubuntu version: 16.04 LTS - components: - name: Apache version: 2.4.23
后续步骤
- 了解如何提交虚拟机映像以供审批。