简体   繁体   English

Google Cloud Compute Engine API:直接使用setMetadata创建createVM

[英]Google Cloud Compute Engine API: createVM directly with setMetadata

I use @google-cloud/compute to create VM instances automatically. 我使用@ google-cloud / compute自动创建VM实例。

Also I use startup scripts in those instances. 我还在那些实例中使用启动脚本。

So, firstly I call Zone.createVM and then VM.setMetadata . 因此,我首先调用Zone.createVM ,然后调用VM.setMetadata

But in some regions startup script is not running. 但是在某些地区,启动脚本未运行。 And it is running after VM reset, so looks like my VM.setMetadata call is just too late. 而且它在VM重置后正在运行,因此看来我的VM.setMetadata调用为时已晚。

In the web-interface we can create VM directly with metadata. 在Web界面中,我们可以直接使用元数据创建VM。 But I do not see this ability in API. 但我在API中看不到此功能。

Can it be done with API? 可以用API完成吗?

To set up a startup script during instance deployment you can provide it as part of the metadata property in the API call: 要在实例部署期间设置启动脚本,您可以在API调用中将其作为元数据属性的一部分提供:

POST https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances

{
  ...
  "metadata": {
    "items": [
      {
       "key": "startup-script",
       "value": "#! /bin/bash\n\n# Installs apache and a custom homepage\napt-get update\napt-get install -y apache2\ncat <<EOF > /var/www/html/index.html\n<html><body><h1>Hello World</h1>\n<p>This page was created from a simple start up script!</p>\n</body></html>"
      }
    ]
  }
  ...
}

See the full reference for the resource "compute.instances" of the Compute Engine API here . 请在此处参阅Compute Engine API的资源“ compute.instances”的完整参考。

Basically, if you are using a Nodejs library to create the instance you are already calling this, so you will only need to add the metadata keys as documented. 基本上,如果您正在使用Nodejs库创建实例,那么您已经在调用它,因此您只需要添加记录的元数据键即可。

Also, if you are doing this frequently I guess it would be more practical if you stored the script in a bucket in GCP and simply add the URI to the metadata like this: 另外,如果您经常这样做,我想如果将脚本存储在GCP的存储桶中,然后将URI像这样简单地添加到元数据中,将更加实用:

  "metadata": {
    "items": [
      {
       "key": "startup-script-url",
       "value": "gs://bucket/myfile"
      }
    ]
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Gcloud 计算 api createVM 未创建公共 ip - Gcloud compute api createVM not creating public ip 为Google Compute Engine设置Cloud 9 - Setup Cloud 9 for Google Compute Engine 使用Firebase Cloud Functions中的Google Cloud Compute Engine REST API - Use the Google Cloud Compute Engine REST API from Firebase Cloud Functions Google Cloud Compute Engine-Node.js无法正常工作 - Google Cloud Compute Engine - Nodejs not working 无法在 Compute Engine 中克隆 Google Cloud Repository - Can't clone Google Cloud Repository in Compute Engine 在 Google Cloud Standard App Engine 中直接 push.env 文件的缺点 - Cons of directly pushing .env file in Google Cloud Standard App Engine 来自 google 官方文档的 Google Cloud Speech to Text NodeJs 在 Google Compute Engine Linux 服务器上不起作用 - Google Cloud Speech to Text NodeJs that from google official document does not work on Google Compute Engine Linux Server 在Compute Engine上连接Redis时,Google Cloud App Engine标准环境节点JS连接超时 - Google Cloud App Engine Standard Environment Node JS connection timeout when connecting Redis on Compute Engine 使用Google App Engine或Google Cloud Compute VM来测试“运行我的App”? - Use Google App Engine or Google Cloud Compute VM to Test Run My App? 带有nodejs的Google Compute Engine上的SSL - SSL on Google Compute Engine with nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM