如何使用来自 Node.JS 客户端的容器映像启动 Google 云实例?
How do I start a Google Cloud instance with a container image from a Node.JS client?
我想从 Node.JS 中的 Google 云功能中启动一个带有容器映像的虚拟机实例。
我不知道如何使用指定的容器映像调用 createVM 函数。
const [vm, operation] = await zone.createVM(vmName, {os: 'ubuntu'});
我在文档中的任何地方都没有看到它
https://googleapis.dev/nodejs/compute/latest/index.html
据我了解,您最终想要的是一个新的 GCP Compute Engine 实例 运行 与 Container Optimized OS (COS) 运行s Docker 从存储库托管的容器映像创建容器实例。要以编程方式实现此目的,您还需要使用 Node.JS API.
诀窍(对我而言)是通过 GCP 云控制台手动创建计算引擎实例。完成后,我们就可以登录实例并通过 运行ning:
检索原始元数据
wget --output-document=- --header="Metadata-Flavor: Google" --quiet http://metadata.google.internal/computeMetadata/v1/?recursive=true
我们得到的是该元数据的 JSON 表示。从这里,我们发现我们通过 API 创建所需的计算引擎的实际目标是使用标准 API 创建该计算引擎,然后还定义所需的元数据。容器优化 OS 似乎只有一个 script/program 读取元数据并将其用于 运行 Docker.
当我在 Compute Engine 中检查容器 运行ning 的数据时,我发现了一个名为:
的属性
attributes.gce-容器声明
包含:
"spec:\n containers:\n - name: instance-1\n image: nodered/node-red\n stdin: false\n tty: false\n restartPolicy: Always\n\n# This container declaration format is not public API and may change without notice. Please\n# use gcloud command-line tool or Google Cloud Console to run Containers on Google Compute Engine."
这是 YAML,如果我们将其格式化,我们会发现:
spec:
containers:
- name: instance-1
image: nodered/node-red
stdin: false
tty: false
restartPolicy: Always
# This container declaration format is not public API and may change without notice. Please
# use gcloud command-line tool or Google Cloud Console to run Containers on Google Compute Engine.
我们已经做到了。要创建托管容器映像的 GCP 计算引擎,我们将创建容器映像 运行 优化容器 OS(例如 "image":"projects/cos-cloud/global/images/cos-stable-77-12371-114-0")并设置元数据将容器定义为 运行.
在 Google 云控制台中创建实例时,我能够复制等效的 REST 命令,获取 JSON 并将其粘贴到 Google 云计算 Node.js SDK 配置。
const Compute = require('@google-cloud/compute');
// Creates a client
const compute = new Compute();
// Create a new VM using the latest OS image of your choice.
const zone = compute.zone('us-east1-d');
// The above object will auto-expand behind the scenes to something like the
// following. The Debian version may be different when you run the command.
//-
const config =
{
"kind": "compute#instance",
"name": "server",
"zone": "projects/projectName/zones/us-east1-d",
"machineType": "projects/projectName/zones/us-east1-d/machineTypes/f1-micro",
"displayDevice": {
"enableDisplay": false
},
"metadata": {
"kind": "compute#metadata",
"items": [
{
"key": "gce-container-declaration",
"value": "spec:\n containers:\n - name: game-server\n image: gcr.io/projectName/imageName\n stdin: false\n tty: false\n restartPolicy: Never\n\n# This container declaration format is not public API and may change without notice. Please\n# use gcloud command-line tool or Google Cloud Console to run Containers on Google Compute Engine."
},
{
"key": "google-logging-enabled",
"value": "true"
}
]
},
"tags": {
"items": [
"https-server"
]
},
"disks": [
{
... //Copied from Google Cloud console -> Compute Engine -> Create VM Instance -> copy equivalent REST command (at the bottom of the page)
]
};
//-
// If the callback is omitted, we'll return a Promise.
//-
zone.createVM('new-vm-name', config).then(function(data) {
const vm = data[0];
const operation = data[1];
const apiResponse = data[2];
res.status(200).send(apiResponse);
});
我想从 Node.JS 中的 Google 云功能中启动一个带有容器映像的虚拟机实例。
我不知道如何使用指定的容器映像调用 createVM 函数。
const [vm, operation] = await zone.createVM(vmName, {os: 'ubuntu'});
我在文档中的任何地方都没有看到它 https://googleapis.dev/nodejs/compute/latest/index.html
据我了解,您最终想要的是一个新的 GCP Compute Engine 实例 运行 与 Container Optimized OS (COS) 运行s Docker 从存储库托管的容器映像创建容器实例。要以编程方式实现此目的,您还需要使用 Node.JS API.
诀窍(对我而言)是通过 GCP 云控制台手动创建计算引擎实例。完成后,我们就可以登录实例并通过 运行ning:
检索原始元数据wget --output-document=- --header="Metadata-Flavor: Google" --quiet http://metadata.google.internal/computeMetadata/v1/?recursive=true
我们得到的是该元数据的 JSON 表示。从这里,我们发现我们通过 API 创建所需的计算引擎的实际目标是使用标准 API 创建该计算引擎,然后还定义所需的元数据。容器优化 OS 似乎只有一个 script/program 读取元数据并将其用于 运行 Docker.
当我在 Compute Engine 中检查容器 运行ning 的数据时,我发现了一个名为:
的属性attributes.gce-容器声明
包含:
"spec:\n containers:\n - name: instance-1\n image: nodered/node-red\n stdin: false\n tty: false\n restartPolicy: Always\n\n# This container declaration format is not public API and may change without notice. Please\n# use gcloud command-line tool or Google Cloud Console to run Containers on Google Compute Engine."
这是 YAML,如果我们将其格式化,我们会发现:
spec:
containers:
- name: instance-1
image: nodered/node-red
stdin: false
tty: false
restartPolicy: Always
# This container declaration format is not public API and may change without notice. Please
# use gcloud command-line tool or Google Cloud Console to run Containers on Google Compute Engine.
我们已经做到了。要创建托管容器映像的 GCP 计算引擎,我们将创建容器映像 运行 优化容器 OS(例如 "image":"projects/cos-cloud/global/images/cos-stable-77-12371-114-0")并设置元数据将容器定义为 运行.
在 Google 云控制台中创建实例时,我能够复制等效的 REST 命令,获取 JSON 并将其粘贴到 Google 云计算 Node.js SDK 配置。
const Compute = require('@google-cloud/compute');
// Creates a client
const compute = new Compute();
// Create a new VM using the latest OS image of your choice.
const zone = compute.zone('us-east1-d');
// The above object will auto-expand behind the scenes to something like the
// following. The Debian version may be different when you run the command.
//-
const config =
{
"kind": "compute#instance",
"name": "server",
"zone": "projects/projectName/zones/us-east1-d",
"machineType": "projects/projectName/zones/us-east1-d/machineTypes/f1-micro",
"displayDevice": {
"enableDisplay": false
},
"metadata": {
"kind": "compute#metadata",
"items": [
{
"key": "gce-container-declaration",
"value": "spec:\n containers:\n - name: game-server\n image: gcr.io/projectName/imageName\n stdin: false\n tty: false\n restartPolicy: Never\n\n# This container declaration format is not public API and may change without notice. Please\n# use gcloud command-line tool or Google Cloud Console to run Containers on Google Compute Engine."
},
{
"key": "google-logging-enabled",
"value": "true"
}
]
},
"tags": {
"items": [
"https-server"
]
},
"disks": [
{
... //Copied from Google Cloud console -> Compute Engine -> Create VM Instance -> copy equivalent REST command (at the bottom of the page)
]
};
//-
// If the callback is omitted, we'll return a Promise.
//-
zone.createVM('new-vm-name', config).then(function(data) {
const vm = data[0];
const operation = data[1];
const apiResponse = data[2];
res.status(200).send(apiResponse);
});