本頁面提供的秘訣有助於您使用 gcloud
指令列工具來管理 Compute Engine 資源。如需所有可用的 gcloud compute
旗標與指令完整清單,請使用內建指令說明 (--help
) 或已發布的參考說明文件或 gcloud core 說明文件。
事前準備
-
如果尚未設定,請先設定驗證機制。「驗證」是指驗證身分,以便存取 Google Cloud 服務和 API 的程序。如要從本機開發環境執行程式碼或範例,請選取下列任一選項,以便對 Compute Engine 進行驗證:
-
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
- Set a default region and zone.
-
擷取資源相關資訊
您可以透過兩種方式擷取 Compute Engine 資源的相關資訊:使用 list
指令傳回資源清單,以及使用 describe
指令傳回特定資源的相關詳細資訊。
使用 list
指令擷取資源
list
指令旨在針對所要求資源的最相關資料,傳回使用者可理解的表格。您可以選擇篩選結果,以傳回內含更多相關結果但較短的清單。
- 篩選名稱的規則運算式
- 您可以使用 RE2 語法來比對資源名稱 (例如執行個體或磁碟名稱)。請參閱
gcloud compute instances list
指令。 - 指令標記
--limit
要傳回的結果數上限。這個標記與使用 describe 指令擷取資源一節中所述的
--sort-by
標記搭配使用時,會顯得格外實用。--sort-by SORT_BY
用於排序的欄位 (如果適用)。如要執行遞減順序排序,請在值前面加上波浪號 (「~」)。這個旗標會與以下順序套用的其他旗標互動:
--flatten
、--sort-by
、--filter
、--limit
。
使用 describe
指令擷取資源
describe
指令是用於顯示一項資源的相關資料。您必須在 describe
指令中提供資源名稱。如果您不記得資源名稱,則可以執行 list
指令以取得資源清單。舉例來說,以下兩個指令說明如何列出映像檔以取得映像檔名稱及其相關專案,以便提供這些資料做為 describe
指令的輸入項目:
gcloud compute images list
NAME PROJECT FAMILY DEPRECATED STATUS ... centos-7-v20170620 centos-cloud centos-7 READY ... debian-9-stretch-v20170619 debian-cloud debian-9 READY ...
gcloud compute images describe debian-9-stretch-v20170619 --project debian-cloud
describe
指令的預設輸出為 YAML 格式,但您可以使用 --format
旗標來選擇要使用 JSON、YAML 或文字輸出格式。如果您要剖析輸出內容,JSON 輸出格式會十分實用。而文字輸出格式則會以每行列出一個屬性。
gcloud compute regions describe us-central1 --format json
{ "creationTimestamp": "2013-09-06T10:36:54.847-07:00", "description": "us-central1", "id": "6837843067389011605", "kind": "compute#region", "name": "us-central1", ... "status": "UP", "zones": [ "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a", "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-b", "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f" ] }
範例
list
指令範例
當您列出資源時,會獲得一目了然的摘要資料表。例如,如要傳回您專案中執行個體的摘要資料,請使用 instances list
指令:
gcloud compute instances list
NAME ZONE MACHINE_TYPE INTERNAL_IP EXTERNAL_IP STATUS example-instance asia-east1-b e2-standard-2 10.240.95.199 107.167.182.44 RUNNING example-instance2 us-central1-a e2-standard-2 10.240.173.254 23.251.148.121 RUNNING test-instance us-central1-a e2-standard-2 10.240.118.207 23.251.153.172 RUNNING
加入具有 key ~ value
運算子的 --filter
旗標,就可以使用規則運算式對 list
指令產生的結果進行篩選。例如,篩選執行個體清單以僅納入名稱中包含「test」的執行個體:
gcloud compute instances list --filter="name ~ .*test.*"
NAME ZONE MACHINE_TYPE INTERNAL_IP EXTERNAL_IP STATUS test-instance us-central1-a e2-standard-2 10.240.118.207 23.251.153.172 RUNNING
如要傳回含有 status
為 DONE
但不包含 httpStatus
為 200
的區域作業清單,請對 operations list
指令套用 zone
篩選器,然後 grep
結果:
gcloud compute operations list --filter="zone:(us-central1-a)" | grep DONE | grep -v 200
NAME HTTP_STATUS TYPE TARGET STATUS operation-1397752585735-4f73fa25b4b58-f0920fd5-254d709f 400 delete us-central1-a/disks/example-instance DONE operation-1398357613036-4f7cc80cb41e0-765bcba6-34bbd040 409 insert us-central1-a/instances/i-1 DONE operation-1398615481237-4f8088aefbe08-cc300dfa-2ce113cf 409 insert us-central1-a/instances/i-2 DONE
如要取得依名稱以遞減順序排序 (--sort-by ~NAME
) 的 us-central1-a
磁碟清單,請使用 disks list
指令:
gcloud compute disks list --sort-by ~NAME --filter="zone:(us-central1-a)"
在某些情況下,您可能希望擁有連結資源的完整 URI,例如要求將 list
指令的輸出傳遞到另一個指令或應用程式,以取得資源連結的清單。如要顯示完整的 URI 資源連結,請使用 list
指令搭配 --uri
旗標。
gcloud compute instances list --uri --filter="name~'^example-.*'"
https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/example-instance1 https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/example-instance2
如要在指令中使用先前的 list
指令輸出來刪除執行個體,請使用以下指令:
gcloud compute instances delete $(gcloud compute instances list --uri --filter="name~'^example-.*'")
describe
指令範例
如要只取得關於一個執行個體的詳細資料,請指定包括區域的執行個體。例如,如要傳回「asia-east1-b」區域中名為「example-instance」執行個體的相關資訊,您可以使用 instances describe
指令:
gcloud compute instances describe example-instance --zone asia-east1-b
在預設情況下,這會傳回 YAML 輸出內容。如要將輸出變更為 JSON 或文字格式 (每行一個屬性),請使用 --format
旗標。舉例來說,如要傳回相同執行個體的文字輸出,請使用:
gcloud compute instances describe example-instance --zone asia-east1-b --format text
--- canIpForward: False creationTimestamp: 2014-04-19T06:43:04.087-07:00 disks[0].autoDelete: False disks[0].boot: True disks[0].deviceName: example-instance ...
如要取得特定作業的詳細資訊,請使用 operations list
指令找出作業的完整 URI:
gcloud compute operations list --filter="zone:(us-central1-a)"
NAME TYPE TARGET HTTP_STATUS STATUS operation-1406155165815-4fee4032850d9-7b78077c-a170c5c0 delete us-central1-a/instances/example-instance 200 DONE operation-1406155180632-4fee4040a67c1-bf581ed8-ab5af2b8 delete us-central1-a/instances/example-instance-2 200 DONE ...
然後在 operations describe
指令中使用此 URI:
gcloud compute operations describe \
operation-1406155165815-4fee4032850d9-7b78077c-a170c5c0 --zone us-central1-a
endTime: '2014-07-23T15:40:02.463-07:00' id: '31755455923038965' insertTime: '2014-07-23T15:39:25.910-07:00' kind: compute#operation name: operation-1406155165815-4fee4032850d9-7b78077c-a170c5c0 operationType: delete progress: 100 ...
以下指令會以 JSON 格式 (--format json
) 取得執行個體設定。
gcloud compute instances describe example-instance \
--zone us-central1-a
--format json
{ ... "name": "example-instance", "networkInterfaces": [ { "accessConfigs": [ { "kind": "compute#accessConfig", "name": "external-nat", "natIP": "107.167.187.66", "type": "ONE_TO_ONE_NAT" } ], "name": "nic0", "network": "https://www.googleapis.com/compute/v1/projects/myproject/global/networks/default", "networkIP": "10.240.111.51" } ], ... "status": "RUNNING" ... }
查看授予給您的使用者身分
請使用下列指令來找出您被授予權限的帳戶:
gcloud auth list
撤銷更新憑證
如要從使用 Google Cloud CLI 的電腦上撤銷其帳戶憑證,請使用以下指令:
gcloud auth revoke
這會強制您使用 gcloud init
進行重新認證。
此外,您也可以撤銷 gcloud CLI 存取資源的權限。假設您的更新憑證遭駭時,就可以這麼做。如要撤銷 gcloud CLI 的權限,請按照下列步驟操作:
- 在「Google Account」(Google 帳戶) 頁面登入您的帳戶。
- 在「Account permissions」(帳戶權限) 部分,按一下 [Security] (安全性),然後點選 [View all] (查看全部)。
- 選取 [Google Cloud SDK],然後點選 [撤銷存取權]。
重新啟動執行個體
如要在「us-central1-a」區域中重設名為「example-instance」的執行個體,請使用 instances reset
指令:
gcloud compute instances reset example-instance --zone us-central1-a
如需重設注意事項的相關資訊,請參閱重設執行個體說明文件。