Mat*_*ttK 6 php google-compute-engine google-cloud-platform gcloud
我使用以下代码使用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);
}
Run Code Online (Sandbox Code Playgroud)
今天,我很幸运地意识到,由于未知原因,我想启动的实例未能成功。我尝试使用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"
}
Run Code Online (Sandbox Code Playgroud)
你建议我做什么?切换到其他区域可能是最好的解决方案。但是有一个问题,我什至没有实例没有成功启动,所以我无法对此做出反应。这是预期的行为吗?您做了什么缓解这个问题?
实际上,我还没有观察到您使用 GCE 描述的错误,但要获取 GCE 实例的“错误状态”,您可以使用 Method:instances.get 查询计算 API 并评估“status”和“statusMessage”的响应”
HTTP request
GET https://www.googleapis.com/compute/v1/projects/{project}/zones/{zone}/instances/{resourceId}
Run Code Online (Sandbox Code Playgroud)
状态的返回值可以是以下之一:PROVISIONING、STAGING、RUNNING、STOPPING、STOPPED、SUSPENDING、SUSPENDED 和 TERMINATED。
另请参阅此 API 调用的参考手册: https://cloud.google.com/compute/docs/reference/rest/v1/instances/get
因此,如果您查询新创建的 GCE 实例的状态一段时间,并且只有在实例状态从“PROVISIONING”或“STAGING”切换到“RUNNING”时才返回“成功”,那么您应该是安全的。如果实例状态设置为“正在运行”,我从未观察到在实例创建过程中出现任何错误。
| 归档时间: |
|
| 查看次数: |
384 次 |
| 最近记录: |