微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Google Cloud - Compute Engine,使用实例模板插入实例

如何解决Google Cloud - Compute Engine,使用实例模板插入实例

我想通过 java google-api-client 使用 InstanceTemplate 创建一个实例。执行操作后,新实例将在 GCP 的 Compute Engine 前端显示,并已创建。 10-15 秒后,实例消失。

GCP-Compute Engine VM-Instance Overview

按照参考手册,我无法理解为什么我的代码不起作用。
https://cloud.google.com/compute/docs/reference/rest/v1/instances/insert

Compute.Instances.Insert insert = compute
                    .instances()
                    .insert("{{project-id}}","europe-west1-c",instance)
                    .setSourceInstanceTemplate("/compute/v1/projects/{{project-id}}/global/instanceTemplates/instance-template-1")
                    .setZone("europe-west1-c")
                    .setProject("{{project-id}}");
            Operation op = insert.execute();

实例对象如下所示:

Instance instance = new Instance();
instance.setName(instanceName);
instance.setMachineType("zones/europe-west1-c/machineTypes/g1-small");

Gradle 依赖

compile 'com.google.api-client:google-api-client:1.31.2'  
compile group: 'com.google.apis',name: 'google-api-services-compute',version: 'v1-rev235-1.25.0'

从 GCP 登录

{
  "protoPayload": {
    "@type": "type.googleapis.com/google.cloud.audit.AuditLog","status": {
      "code": 3,"message": "INVALID_ParaMETER"
    },"authenticationInfo": {
      "principalEmail": "compute-dev-me@{{project-id}}.iam.gserviceaccount.com"
    },"requestMetadata": {
      "callerIp": "12.34.56.78","callerSuppliedUserAgent": "redacted/0.1 Google-API-Java-Client/1.31.2 Google-HTTP-Java-Client/1.37.0 (gzip),gzip(gfe)"
    },"serviceName": "compute.googleapis.com","methodName": "v1.compute.instances.insert","resourceName": "projects/{{project-id}}/zones/europe-west1-c/instances/test-mit-richtig","request": {
      "@type": "type.googleapis.com/compute.instances.insert"
    }
  },"insertId": "-duwg4fde7a2","resource": {
    "type": "gce_instance","labels": {
      "instance_id": "redacted-number","zone": "europe-west1-c","project_id": "{{project-id}}"
    }
  },"timestamp": "2021-02-xxTxx:xx:xx.565227Z","severity": "ERROR","logName": "projects/{{project-id}}/logs/cloudaudit.googleapis.com%2Factivity","operation": {
    "id": "operation-1613xxxx41-xxxxxxxx-xxxxxxxx-xxxxxxx","producer": "compute.googleapis.com","last": true
  },"receiveTimestamp": "2021-02-xxTxx:xx:xx.569578819Z"
}

解决方法

在查看日志后,如果您收到一个无效参数错误代码。在您的参数中,您按如下方式设置实例名称:

instance.setName(instanceName);

GCE 实例有一个 naming convention,其中名称必须以小写字母开头,后跟一串字符或连字符,并且不能以连字符结尾。在您的情况下,您的代码中有一个大写的 n (instanceName)。将其设置如下应该可以解决问题:

instance.setName(instancename);

我在您的日志中注意到的另一件事是,项目 ID 显示为 {{project-id}},如果您没有对此进行编辑(如果有的话,这是一个好习惯) ,但您应该指定代码的编辑部分以帮助理解日志)。

-----评论后编辑-----

检查 Google Cloud Platform 上的活动选项卡后,我们发现它是由服务帐户权限引起的。

在以下链接中,您可以找到有关创建和启用 Service Accounts 的信息。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。