為單一執行個體設定較大的網路遮罩
本頁說明如何設定自訂映像檔,建立具有較大網路遮罩 (與子網路的網路遮罩大小相同) 的 Compute Engine 執行個體。
或者,您也可以建立子網路,為連線至該子網路的所有運算執行個體設定較大的網路遮罩。為方便維護及取得次要範圍的支援服務,建議您透過子網路設定較大的執行個體網路遮罩,而不是建立自訂映像檔。
如果建立子網路不符合您的用途,可以建立自訂映像檔,並使用這個映像檔建立具有較大網路遮罩的運算執行個體。DHCP 伺服器會使用子網路的網路遮罩 (而非 /32) 設定執行個體介面,執行個體可以直接與連線至子網路主要 IPv4 範圍的其他執行個體通訊。次要 IPv4 範圍不受影響。
如要進一步瞭解運算執行個體網路遮罩,請參閱「運算執行個體網路遮罩」。
事前準備
- 請參閱下列 Compute Engine 映像檔指南:
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Compute Engine API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Compute Engine API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.
必要的角色
如要取得建立自訂映像檔和執行個體所需的權限,請要求管理員授予您專案的 Compute 執行個體管理員 (v1) (role/compute.instanceAdmin.v1) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。
建立設定 MULTI_IP_SUBNET 選項的自訂映像檔
建立自訂映像檔時,您可以透過多種方式指定來源映像檔。以下範例說明如何根據映像檔系列建立自訂映像檔。如要進一步瞭解如何建立自訂映像檔,請參閱「建立自訂映像檔」。
gcloud CLI
gcloud compute images create IMAGE_NAME \
--source-image-family=SOURCE_IMAGE_FAMILY \
--source-image-project=PROJECT \
--guest-os-features=MULTI_IP_SUBNET
更改下列內容:
IMAGE_NAME:自訂映像檔的名稱。SOURCE_IMAGE_FAMILY:用於建立這個映像檔的來源映像檔系列。例如:debian-13。PROJECT:映像檔系列所在的來源專案,例如debian-cloud或專案 URI。
從MULTI_IP_SUBNET自訂映像檔建立 Compute 執行個體
如要建立使用自訂映像檔的運算執行個體,請指定您建立的映像檔。如要進一步瞭解如何建立執行個體,請參閱「透過自訂映像檔建立執行個體」。
gcloud CLI
gcloud compute instances create INSTANCE_NAME \
--image=IMAGE_NAME \
--zone=ZONE \
--subnet=SUBNET_NAME
更改下列內容:
INSTANCE_NAME:執行個體的名稱。IMAGE_NAME:您建立的自訂映像檔名稱。ZONE:要建立執行個體的可用區。SUBNET_NAME:執行個體要連線的子網路名稱。
驗證設定
建立運算執行個體後,您可以確認 DHCP 伺服器是否在執行個體上設定正確的網路遮罩,以及執行個體是否收到 ARP 回覆。您可以使用單一執行個體驗證 ARP 回覆,因為 Google Cloud 一律會針對子網路範圍內的 IP 位址傳送回覆。
下列步驟說明如何在 Linux 執行個體上驗證設定。
- 使用 SSH 連線至執行個體。
檢查網路介面設定,確認網路遮罩與子網路的網路遮罩相符 (不是
/32)。ip addr show
在輸出內容中,找出網路介面的
inet行,例如ens4。確認執行個體可以接收子網路中其他 IP 位址的 ARP 回覆。您可以使用
ping指令觸發 ARP 要求。您可以選擇未使用的 IP 位址,即使沒有對ping要求的回覆,Google Cloud 仍會傳送 ARP 回覆。ping -c 3 OTHER_IP_ADDRESS_IN_SUBNET
將
OTHER_IP_ADDRESS_IN_SUBNET替換為相同子網路的 IP 位址。這個 IP 位址不一定要在使用中。檢查 ARP 表格,查看與其他 IP 位址相關聯的 MAC 位址。
ip neighbor
輸出內容會顯示其他 IP 位址的 MAC 位址。