首页 > 解决方案 > 更新 Deployment Manager 部署时如何在 VM 实例上执行命令行脚本

问题描述

我目前在我的vm.yaml文件中有此配置,它设置了一个允许 http 请求并提供自定义Hello World!页面的 vm 实例,如以下startup-scripts部分所述:

resources:
- type: compute.v1.instance
  name: quickstart-deployment-vm
  properties:
    zone: us-central1-f
    machineType: https://www.googleapis.com/compute/v1/projects/my_project/zones/us-central1-f/machineTypes/f1-micro
    disks:
      ...
    networkInterfaces:
      ...
    tags:
        items: ["http"]

    metadata:
      items:
      - key: startup-script
        value: |
          #!/bin/bash
          apt-get update
          apt-get install -y apache2
          echo '<!doctype html><html><body><h1>Hello World!</h1></body></html>' | sudo tee /var/www/html/index.html

- type: compute.v1.firewall
  name: tcp-firewall-rule
  properties:
    targetTags: ["http"]
    network: https://www.googleapis.com/compute/v1/projects/my_project/global/networks/default
    sourceRanges: ["0.0.0.0/0"]
    targetTags: ["http","http-server"]
    allowed:
     - IPProtocol: TCP
       ports: ["80"]

运行后,我在访问 vm 的外部 ip 时gcloud deployment-manager deployments create vm-deploy --config vm.yaml得到了预期的响应。Hello World!但是,我现在想更新此部署以更改响应的内容,比如说,What's up, everyone!但是,似乎我不能仅通过更改该startup-scripts部分中的内容来做到这一点。我试着把它放在一个vm-update.yaml文件中:

resources:
- type: compute.v1.instance
  name: quickstart-deployment-vm
  properties:
    ...
    metadata:
      items:
      - key: startup-script
        value: |
          #!/bin/bash
          apt-get update
          apt-get install -y apache2
          echo '<!doctype html><html><body><h1>What's up, everyone!</h1></body></html>' | sudo tee /var/www/html/index.html

- type: compute.v1.firewall
  name: tcp-firewall-rule
  properties:
    ...

并跑了gcloud deployment-manager deployments update vm-deploy --config vm-update.yaml。但是,响应和index.html文件并没有改变,它们仍然是Hello World!. 我猜这是因为startup-script仅在实例创建时执行,而不是在实例更新时执行。有谁知道当我更新虚拟机时如何使用部署管理器在我的虚拟机上执行脚本?

标签: google-cloud-platformgoogle-deployment-manager

解决方案


我发现的一个“黑客”是我可以更改machineType虚拟vm-update.yaml机的startup-script.


推荐阅读