為子網路中的所有執行個體設定較大的執行個體網路遮罩
本頁說明如何使用大於預設網路遮罩的子網路,設定所有 Compute Engine 執行個體。如要使用這項設定,請建立子網路並設定「解析子網路遮罩」 (--resolve-subnet-mask) 設定。將運算執行個體連線至子網路時,系統會為執行個體設定與子網路網路遮罩大小相同的網路遮罩,而非 /32。如要進一步瞭解運算執行個體網路遮罩,請參閱「運算執行個體網路遮罩」。
事前準備
- 登入 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 網路管理員 (role/compute.networkAdmin) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。
建立子網路,設定較大的執行個體網路遮罩
控制台
前往 Google Cloud 控制台的「VPC Networks」(虛擬私有雲網路) 頁面。
選取虛擬私有雲網路。
點選「子網路」分頁標籤。
按一下 [新增子網路]。
在「Name」部分,輸入子網路名稱。
在「區域」部分,選取區域。
在「用途」部分,選取「無」。
在「IP stack type」(IP 堆疊類型),選取「IPv4 (single stack)」(IPv4 (單一堆疊))。
在「Primary IPv4 range」(主要 IPv4 範圍) 中,輸入有效的 IPv4 位址範圍。
如要新增次要 IPv4 範圍,請按照下列步驟操作:
- 在「子網路範圍名稱」中,輸入次要 IPv4 範圍的名稱。
- 在「Secondary IPv4 range」(次要 IPv4 範圍) 中,輸入有效的 IPv4 位址範圍。
在「解析子網路遮罩」中,選取下列其中一個選項:
- 所有範圍
- 僅限主要範圍
按一下「新增」。
gcloud
gcloud compute networks subnets create SUBNET_NAME \
--network=NETWORK_NAME \
--range=IP_RANGE \
--region=REGION \
--resolve-subnet-mask=ARP_RANGES
更改下列內容:
SUBNET_NAME:子網路名稱。NETWORK_NAME:要在其中建立子網路的網路。IP_RANGE:子網路的主要 IPv4 位址範圍。REGION:子網路的區域。ARP_RANGES:要接收 ARP 回覆的範圍。有效值為ARP_PRIMARY_RANGE和ARP_ALL_RANGES。
在子網路中建立運算執行個體
建立連線至子網路的運算執行個體。如要進一步瞭解如何建立執行個體,請參閱在特定子網路中建立執行個體。
gcloud CLI
gcloud compute instances create INSTANCE_NAME \
--zone=ZONE \
--subnet=SUBNET_NAME
更改下列內容:
INSTANCE_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 位址。