位於同一個虛擬私人雲端網路中的 VM 可以使用內部 DNS 名稱 (而非 IP 位址) 互相存取。
事前準備
-
如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 Google Cloud 服務和 API 的程序。如要在本機開發環境中執行程式碼或範例,您可以選取下列任一選項,向 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
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.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
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.
For more information, see Authenticate for using REST in the Google Cloud authentication documentation.
-
判斷 VM 的內部 DNS 名稱
使用以下程序,讀取指派給 VM 執行個體的內部 DNS 名稱。您可以查詢 hostname
中繼資料項目,取得內部 DNS 名稱。
- 連線至 VM。
查詢
hostname
中繼資料:Linux VM
curl "http://metadata.google.internal/computeMetadata/v1/instance/hostname" \ -H "Metadata-Flavor: Google"
Windows VM
Invoke-RestMethod ` -Headers @{"Metadata-Flavor" = "Google"} ` -Uri "http://metadata.google.internal/computeMetadata/v1/instance/hostname"
中繼資料伺服器會以下列格式之一傳回 VM 的主機名稱,顯示 VM 使用的內部 DNS 名稱類型:
- 區域 DNS:
VM_NAME
.ZONE
.c
.PROJECT_ID
.internal
- 全球 DNS:
VM_NAME
.c
.PROJECT_ID
.internal
輸出內容:
VM_NAME
:VM 名稱ZONE
:VM 所在區域PROJECT_ID
:VM 所屬的專案
透過內部 DNS 名稱存取 VM
如要存取 VM,請使用內部 DNS 名稱取代 IP 位址。
以下範例使用 ping
聯絡使用區域 DNS 的 VM。如要使用這個方法,您必須先建立允許執行個體接收 ICMP 流量的防火牆規則。
$ ping VM_NAME.ZONE.c.PROJECT_ID.internal -c 1 PING VM_NAME.ZONE.c.PROJECT_ID.internal (10.240.0.17) 56(84) bytes of data. 64 bytes from VM_NAME.ZONE.c.PROJECT_ID.internal (10.240.0.17): icmp_seq=1 ttl=64 time=0.136 ms
更改下列內容:
VM_NAME
:VM 名稱ZONE
:VM 所在區域PROJECT_ID
:VM 所屬的專案
後續步驟
- 遷移至區域 DNS
- 進一步瞭解 Compute Engine 的內部 DNS 名稱。
- 為 VM 設定靜態 IP 位址。