为子网中的所有实例配置更大的实例网络掩码
本页面介绍了如何为子网中的所有 Compute Engine 实例配置大于默认值的网络掩码。如需使用此配置,请创建一个子网并配置解析子网掩码 (--resolve-subnet-mask) 设置。将计算实例连接到子网时,系统会为实例配置与子网的网络掩码大小相同的网络掩码,而不是
/32。如需详细了解计算实例网络掩码,请参阅计算
实例网络掩码。
准备工作
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
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 Network Admin (role/compute.networkAdmin) IAM 角色。如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
您也可以通过自定义 角色或其他预定义 角色来获取所需的权限。
创建子网以配置更大的实例网络掩码
控制台
在 Google Cloud 控制台中,前往 VPC 网络页面。
选择一个 VPC 网络。
点击子网 标签页。
点击添加子网 。
在名称字段中,输入子网的名称。
在区域 部分中,选择一个区域。
在用途 部分中,选择无 。
在 IP 栈类型 字段中,选择 IPv4(单栈) 。
在主要 IPv4 范围字段中,输入有效的 IPv4 地址 范围。
如需添加次要 IPv4 范围,请执行以下操作:
- 在子网范围名称字段中,输入次要 IPv4 范围的名称。
- 在次要 IPv4 范围 字段中,输入有效的 IPv4 地址范围。
在解析子网掩码 中,选择以下选项之一:
- 所有范围
- 仅主要范围
点击 Add (添加)。
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
在输出中,找到网络接口(例如
ens4)的inet行。验证实例是否可以接收子网中不同 IP 地址的 ARP 回复。您可以使用
ping命令触发 ARP 请求。您可以选择未使用的 IP 地址,Google Cloud 即使没有对ping请求的回复,也会发送 ARP 回复。ping -c 3 OTHER_IP_ADDRESS_IN_SUBNET
将
OTHER_IP_ADDRESS_IN_SUBNET替换为同一子网中的 IP 地址。此 IP 地址不需要在使用中。检查 ARP 表,查看与另一个 IP 地址关联的 MAC 地址。
ip neighbor
输出会显示另一个 IP 地址的 MAC 地址。