【问题标题】:Google Cloud - Compute Engine, Insert Instance with Instance TemplateGoogle Cloud - Compute Engine,使用实例模板插入实例
【发布时间】:2021-05-15 19:19:47
【问题描述】:

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

按照参考手册,我无法理解为什么我的代码不起作用。
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"
}

【问题讨论】:

    标签: java google-api google-compute-engine google-api-java-client google-compute-api


    【解决方案1】:

    查看提供的日志后,您会收到 Invalid Parameter 错误代码。在您的参数中,您将实例名称设置如下:

    instance.setName(instanceName);
    

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

    instance.setName(instancename);
    

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

    ----- 在 cmets 之后编辑 -----

    在查看 Google Cloud Platform 上的活动标签后,我们发现它是由服务帐户权限引起的。

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

    【讨论】:

    • 谢谢迪伦。我正在修改日志中的项目 ID 和其他一些部分。我会尽我所能改进日志发布。检查命名后我的问题仍然存在。您提到的解决方法不起作用,因为 instanceName 是一个 java 字符串,其值为:“test-withbeer”。所以我以前不知道的命名约定已经实现了。日志消息中的错误与什么有关?
    • 另一种检查导致 GCE 实例失败的参数而不是日志的方法是检查 Google Cloud Platform 控制台上的“活动”选项卡,或在日志中将查询设置为:protoPayload.methodName = "v1.compute.instances.insert"
    • " Instanz erstellen test-withbeer compute-dev-me@anon-anon-######.iam.gserviceaccount.com 开始:12.02.2021, 11:06:41 结束:12.02 .2021, 11:07:48 SERVICE_ACCOUNT_ACCESS_DENIED " 我认为这将解决我的问题!我基本上从来没有找到一个地方可以查看 GCP 正在做什么。谢谢迪伦!
    • 如果它提供了任何有用的信息,请编辑您的问题以包含它,谢谢。
    • 这似乎是由权限问题引起的,我将编辑我的答案以提供创建和启用服务帐户所需的步骤。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 2014-11-22
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 2019-07-05
    相关资源
    最近更新 更多