我目前在我的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"]在运行gcloud deployment-manager deployments create vm-deploy --config vm.yaml之后,我在访问虚拟机的外部ip时得到预期的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只在实例创建时执行,而不是在实例更新时执行。有人知道当我更新虚拟机时,如何使用部署管理器在虚拟机上执行脚本吗?
发布于 2020-08-28 15:21:32
我发现的一个“黑客”是,我可以在vm-update.yaml中更改VM的machineType,我认为这会强制VM重新启动/重新初始化,并运行startup-script。
发布于 2020-08-20 16:03:19
要使用部署管理器更新index.html (在运行的服务器中)并使更改“生效”,只需在更新的yaml文件中紧跟在echo部件之后添加一行service apache2 reload;如下所示:
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
service apache2 reload这将重新加载服务(比restart更安全)并提供新的index.html文件。
或者,您可以将添加的行更改为reboot -n,但仅当它对您方便时。只重新启动服务要快得多。
https://stackoverflow.com/questions/63479123
复制相似问题