[英]How to create a new service using the Google Cloud Run API for node.js?
我正在 Google Cloud Platform 上开展一个 node.js 项目,我想在用户想要启动服务时启动新的 Cloud Run 实例。 基本上,我想在用户选择可视化他们的结果时自动启动可视化服务器。 这就是为什么我决定在需要时使用 GCP Cloud Run API 创建新服务的原因。 但是,API 的记录很差,我不断收到错误消息:
Service has no template
我的问题是:如何使用 Google Cloud Run 客户端服务 API 创建新服务?
我尝试使用 API,目前我以这种方式编写代码:
// create a new service
async function callCreateService(){
// construct request
const request = {
parent,
template,
serviceId,
};
// Run request
const [operation] = await runClient.createService(request);
const [response] = await operation.promise();
console.log(`The response is ${response}`);
}
console.log('initiated...');
//callListServices();
callCreateService();
我知道父级是正确的,因为我使用listServices()
方法连接并列出了所有当前正在运行的服务,但是我无法使用createService()
设法创建新服务,因为我不知道模板的结构和Google Cloud Run 的 API 对于 node.js 的记录非常少。
是的,您错过了创建云运行服务的模板,以下是它的 arguments:
const template = {
spec: {
container: {
image: '<your image path>',
},
containerPort: 8080,
},
scale: {
replicas: 1,
},
};
const request = {
parent: 'projects/my-project',
serviceId: 'my-service',
template,
};
const [operation] = await runClient.createService(request);
const [response] = await operation.promise();
console.log(`The response is ${response}`);
您可以在此处获取此方法的官方文档
如果以上方法不起作用,您还可以在上面的文档中将与模板相关的内容传递给服务,它被指定为空,但您可以在规范 object 中填写这些内容,如下所示:
const request = {
parent: 'projects/my-project',
service: {
spec: {
container: {
image: 'gcr.io/my-project/my-image:latest',
},
containerPort: 8080,
},
scale: {
replicas: 1,
},
},
serviceId: '<your_service_id>',
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.