本文件說明如何使用服務帳戶,透過 SSH 連線至 Compute Engine 虛擬機器 (VM) 執行個體。為服務帳戶設定 SSH 後,您就能設定應用程式使用 SSH,進而自動執行工作負載。
事前準備
- 建立服務帳戶。
-
如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 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.
-
以服務帳戶身分手動連線至 VM
如要以服務帳戶身分連線至 VM,請使用下列其中一種方法:
直接模擬服務帳戶
使用 gcloud CLI --impersonate-service-account
旗標,即可透過服務帳戶身分直接連線至 VM。執行下列指令,以服務帳戶身分連線至 VM:
gcloud compute ssh VM_NAME \ --impersonate-service-account=SERVICE_ACCOUNT_EMAIL
更改下列內容:
VM_NAME
:您要以哪個 VM 名稱連線至服務帳戶。SERVICE_ACCOUNT_EMAIL
:與服務帳戶相關聯的電子郵件地址。
從 VM 模擬服務帳戶
如要模擬其他 VM 中的服務帳戶,請執行下列操作:
- 連線至以服務帳戶身分執行的 VM。
從以服務帳戶身分執行的 VM 中,使用相同方法連線至其他 VM。
後續步驟
- 瞭解如何設定應用程式以使用 SSH。
- 進一步瞭解 SSH 連線在 Compute Engine 中的運作方式,包括 SSH 金鑰設定和儲存空間。