【问题标题】:how to get error if starting compute engine instance fails如果启动计算引擎实例失败,如何获取错误
【发布时间】:2019-06-22 00:22:26
【问题描述】:

我正在使用以下代码使用 PHP 启动一个实例:

function startInstance($g_project,$g_instance, $g_zone){

    $client = new Google_Client();
    $client->setApplicationName('Google-ComputeSample/0.1');
    $client->useApplicationDefaultCredentials();
    $client->addScope('https://www.googleapis.com/auth/cloud-platform');

    $service = new Google_Service_Compute($client);
    $response = $service->instances->start($g_project, $g_zone, $g_instance);
    echo json_encode($response);

}

今天我很幸运地意识到,由于未知原因,我想要启动的实例未能启动。我尝试使用 GUI 启动它,但通过 GUI 出现错误:Zone "some-zone" does not have enough resources available to fulfill the request. Try a different zone, or try again later.

我回显了 PHP 响应,并将其与实例成功启动时得到的响应进行了比较。我的发现令人震惊。响应完全相同(不包括时间戳和 ID)。如果响应相同,我到底如何区分失败的实例启动和成功?

https://cloud.google.com/compute/docs/reference/rest/v1/instances/start 表明在出现错误时将存在一个error 对象。我可以确认没有。

两者的响应都失败了成功启动:

{
    "clientOperationId": null,
    "creationTimestamp": null,
    "description": null,
    "endTime": null,
    "httpErrorMessage": null,
    "httpErrorStatusCode": null,
    "id": "id",
    "insertTime": "2019-01-28T14:22:36.664-08:00",
    "kind": "compute#operation",
    "name": "operation-name",
    "operationType": "start",
    "progress": 0,
    "region": null,
    "selfLink": "link/operation-name",
    "startTime": null,
    "status": "PENDING",
    "statusMessage": null,
    "targetId": "targetIdHere",
    "targetLink": "linkhere",
    "user": "user",
    "zone": "zone-in-question"
}

你建议我做什么?切换到不同的区域可能是最好的解决方案。但是有一个问题,我什至不知道实例没有成功启动,所以我无法对此做出反应。这是预期的行为吗?您采取了哪些措施来缓解这个问题?

【问题讨论】:

  • 我也有同样的问题,API调用响应"status": "RUNNING"。
  • GCE 不会报告失败,因为它会等到给定区域中的一些资源可用,这就是为什么"status": "PENDING"
  • 您可以设置 stackdriver 运行状况检查,或者您可以包含一个启动脚本,该脚本调用一个单独的 webhook 关于创建状态。

标签: php google-cloud-platform google-compute-engine gcloud


【解决方案1】:

您从 start 调用得到的响应只是一个与您的异步 API 调用相对应的 (zone) operation resource。

为了确定 API 调用的最终状态,您需要使用 get 调用轮询该操作,直到其状态为 DONE。然后,如果error 字段不为空,它将包含出错的详细信息,例如:

{
  ...
  "error": {
    "errors": [
      {
        "code": "ZONE_RESOURCE_POOL_EXHAUSTED",
        "message": "The zone 'projects/you-project/zones/us-central1-c' does not have enough resources available to fulfill the request."
      }
    ]
  }
  ...
}

否则操作已成功完成。

【讨论】:

    【解决方案2】:

    我实际上还没有观察到您使用 GCE 描述的错误,但是要获取 GCE 实例的“错误状态”, 您可以使用 Method: instances.get 查询 Compute API 并评估“status”和“statusMessage”的响应

    HTTP request
    GET https://www.googleapis.com/compute/v1/projects/{project}/zones/{zone}/instances/{resourceId}
    

    状态的返回值可能是以下之一: 配置、暂存、运行、停止、停止、暂停、暂停和终止。

    另请参阅此 API 调用的参考手册: https://cloud.google.com/compute/docs/reference/rest/v1/instances/get

    因此,如果您查询新创建的 GCE 实例的状态一段时间,并且仅当实例的状态从“PROVISIONING”或“STAGING”切换到“RUNNING”时返回“success”,您应该是安全的. 如果实例状态设置为“RUNNING”,我从未观察到实例创建过程中出现任何错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      • 2018-11-01
      • 1970-01-01
      相关资源
      最近更新 更多