虛擬私有雲端(VPC)是雲端中的私有隔離網路,讓您擁有完全控制權。VPC 是區域級資源,您可以在其中建立和使用阿里雲資源,例如 ECS 和 RDS 執行個體。
vSwitch 是可用區級資源,可用於將 VPC 劃分為子網路。同一 VPC 內的 vSwitch 可以透過內部網路相互通訊。透過將雲端資源部署在不同可用區的 vSwitch 中,您可以保護應用程式免受單一可用區故障的影響。
網路規劃
正確的網路規劃對於避免 CIDR 區塊衝突和確保網路可擴展性至關重要。不當的規劃可能導致代價高昂的重建。因此,建議您在建立 VPC 之前規劃您的網路。
Create or delete VPC 和 vSwitch
主控台
建立 VPC 和 vSwitch
Go to the 建立 VPC page in the VPC console.
Configure the VPC:
Region: Select the region where you plan to create cloud resources.
IPv4 CIDR block: Select a suggested CIDR block provided by the console, or enter a custom one. When connecting multiple VPCs, we recommend using non-overlapping CIDR blocks to prevent conflicts. To avoid CIDR block conflicts and ensure network scalability, we recommend that you create a VPC by using IPAM.
1. We recommend that you use the private IPv4 addresses specified in RFC 1918 for the VPC's CIDR block, with a mask length between 16 and 28. Examples include 10.0.0.0/16, 172.16.0.0/16, and 192.168.0.0/16.
2. You cannot use 100.64.0.0/10, 224.0.0.0/4, 127.0.0.0/8, or 169.254.0.0/16 as the IPv4 CIDR block for a VPC.
Configure the vSwitch:
Zone: Select the zone where you will create cloud resources. Your choice should be based on resource availability, as some resources may be sold out in that zone.
IPv4 CIDR block: Select the default CIDR block provided by the console, or adjust the address range as needed.
Add more vSwitches: To protect your applications from single-zone failures, we recommend that you create multiple vSwitches in different zones. You can create vSwitches while you create the VPC, or add more later on the vSwitch 頁面 in the VPC console.
刪除 VPC 和 vSwitch
In the Actions column of the target VPC or vSwitch, or on its details page, click Delete. The system checks for any unreleased cloud resources or associated resources. If any dependencies exist, you must release them before you can delete the VPC or vSwitch.
1. 刪除 vSwitch 之前,請確保它未被共用、未繫結至自訂路由表或網路 ACL,且其中所有雲端資源均已釋放。
2. 刪除 VPC 之前,請確保其中所有資源均已釋放,且與其他網路服務(如 CEN)的連線已移除。
API
與主控台不同,呼叫CreateVpc操作僅建立一個空的 VPC。您還必須呼叫CreateVSwitch操作來建立 vSwitch。
Call CreateVpc and then CreateVSwitch to create a VPC and a vSwitch.
Call DeleteVSwitch and then DeleteVpc to delete a vSwitch and a VPC.
1. 刪除 vSwitch 之前,請確保它未被共用、未繫結至自訂路由表或網路 ACL,且其中所有雲端資源均已釋放。
2. 刪除 VPC 之前,請確保其中所有資源均已釋放,且與其他網路服務(如 CEN)的連線已移除。
Terraform
資源: alicloud_vpc, alicloud_vswitch
資料來源: alicloud_zones
# 指定要建立 VPC 的地區。
provider "alicloud" {
region = "cn-hangzhou"
}
# 使用資料來源自動擷取可建立 vSwitch 的可用區清單。
data "alicloud_zones" "available_zones" {
available_resource_creation = "VSwitch" # 查詢 VPC 中可建立 vSwitch 的可用區。
# available_instance_type = "ecs.g7.large" # 查詢 VPC 中可建立 ECS 執行個體的可用區。
# available_resource_creation = "slb" # 查詢 VPC 中可建立 SLB 執行個體的可用區。
}
# 建立 VPC。
resource "alicloud_vpc" "example_vpc" {
vpc_name = "example_vpc_name"
cidr_block = "10.0.0.0/16" # 指定 CIDR 區塊。
}
# 建立 vSwitch。
resource "alicloud_vswitch" "example_vswitch" {
vswitch_name = "example_vswitch_name"
cidr_block = "10.0.0.0/24" # 指定 CIDR 區塊。
vpc_id = alicloud_vpc.example_vpc.id # 指定 vSwitch 所屬的 VPC ID。
zone_id = data.alicloud_zones.available_zones.zones.0.id # 指定 vSwitch 所屬的可用區。
}
啟用或停用 IPv6
為 VPC 及其 vSwitch 啟用 IPv6 後,預設僅支援私有通訊。若要啟用公共網際網路存取,您可以啟用 IPv6 的公共頻寬.
支援 IPv4/IPv6 雙堆疊的地區.
主控台
啟用 IPv6
建立 VPC 和 vSwitch 時,您可以透過以下方式之一啟用 IPv6:
Select Allocated by system, and then select Assign BGP (Multi-ISP) from the drop-down list. The system automatically creates an IPv6 閘道 and assigns an IPv6 CIDR block.
To centrally manage IP addresses, select Allocated by IPAM. Choose an IPAM pool with a pre-provisioned IPv6 CIDR block, and then configure a mask or specify a CIDR block to allocate an IPv6 CIDR block from the pool.
For an existing VPC, click 啟用 IPv6 in the IPv6 CIDR column:
Select Allocated by system or Allocated by IPAM.
If you choose system allocation, you can select the Automatically 啟用 IPv6 for All vSwitches checkbox. If you do not select this checkbox or if you choose allocation by IPAM, you must navigate to the target vSwitch and click 啟用 IPv6 in its IPv6 CIDR column.
停用 IPv6
In the IPv6 CIDR column for the target VPC or vSwitch, click 停用 IPv6. To disable IPv6 for a VPC, you must first disable IPv6 for all its vSwitches and delete the associated IPv6 閘道.
API
與主控台不同,透過呼叫 API 為 VPC 和 vSwitch 啟用 IPv6 時,不會自動建立 IPv6 閘道。您必須呼叫 CreateIpv6Gateway 自行建立一個。
When you create a VPC and a vSwitch, set the
EnableIPv6parameter in CreateVpc and CreateVSwitch to enable or disable IPv6. When you create a VPC, specify theIpv6IpamPoolIdandIpv6CidrMaskparameters to allocate an IPv6 CIDR block from a specified IPv6 IPAM pool to the VPC.For existing VPC 和 vSwitch, modify the
EnableIPv6parameter of ModifyVpcAttribute and ModifyVSwitchAttribute to enable or disable IPv6. To assign an IPv6 CIDR block to a VPC from a specified IPv6 address pool, call AssociateVpcCidrBlock.
Terraform
Terraform 目前僅支援系統分配的 IPv6 CIDR 區塊。不支援從 IPAM 集區分配。
資源: alicloud_vpc, alicloud_vswitch
資料來源: alicloud_zones
# 指定要建立 VPC 的地區。
provider "alicloud" {
region = "cn-hangzhou"
}
# 使用資料來源自動擷取可建立 vSwitch 的可用區清單。
data "alicloud_zones" "available_zones" {
available_resource_creation = "VSwitch" # 查詢 VPC 中可建立 vSwitch 的可用區。
# available_instance_type = "ecs.g7.large" # 查詢 VPC 中可建立 ECS 執行個體的可用區。
# available_resource_creation = "slb" # 查詢 VPC 中可建立 SLB 執行個體的可用區。
}
# 建立雙堆疊 VPC。
resource "alicloud_vpc" "example_vpc" {
vpc_name = "example_vpc_name"
cidr_block = "10.0.0.0/16"
enable_ipv6 = true # 啟用 IPv6。設定為 false 以停用 IPv6。
ipv6_isp = "BGP" # 指定 IPv6 CIDR 區塊的類型。
}
# 建立雙堆疊 vSwitch。
resource "alicloud_vswitch" "example_vswitch" {
vswitch_name = "example_vswitch_name"
cidr_block = "10.0.0.0/24"
vpc_id = alicloud_vpc.example_vpc.id
zone_id = data.alicloud_zones.available_zones.zones.0.id
enable_ipv6 = true # 啟用 IPv6。設定為 false 以停用 IPv6。
ipv6_cidr_block_mask = 1 # 指定 vSwitch 的 IPv6 CIDR 區塊最後 8 位元。
}
修改 CIDR 區塊
建立 VPC 時指定的 IPv4 CIDR 區塊是其主要 CIDR 區塊。您無法在主控台中修改主要 CIDR 區塊,但可以呼叫 ModifyVpcAttribute 操作並指定 CidrBlock 參數來擴大或縮小 CIDR 區塊。如果縮小 CIDR 區塊,您必須確保新的 CIDR 區塊包含所有已在使用中的 IP 位址。
VPC 的 IPv6 CIDR 區塊和 vSwitch 的 IPv4 或 IPv6 CIDR 區塊無法修改。
次要 CIDR 區塊
如果 VPC 的可用 IP 位址不足以應對業務增長,或者初始網路規劃不充分,您可以新增次要 CIDR 區塊來擴展其位址空間。
次要 CIDR 區塊與主要 CIDR 區塊並行運作,可用於建立 vSwitch 和部署雲端資源(如 ECS 執行個體)。
1. 不能使用 100.64.0.0/10、224.0.0.0/4、127.0.0.0/8 或 169.254.0.0/16 作為次要 IPv4 CIDR 區塊。
2. 次要 CIDR 區塊不能與主要 CIDR 區塊重疊。
3. 預設情況下,您最多可以為每個 VPC 新增 五個次要 IPv4 CIDR 區塊和五個次要 IPv6 CIDR 區塊。
主控台
新增次要 CIDR 區塊
On the Basic Information page of the target VPC, click the CIDR Block Management tab to add a secondary IPv4 or IPv6 CIDR block.
You can add a secondary IPv4 CIDR block in one of three ways:
Default: Quickly add a CIDR block by selecting 10.0.0.0/16, 172.16.0.0/16, or 192.168.0.0/16.
Custom: Configure a custom secondary CIDR block.
Allocated by IPAM: Use IPAM to prevent CIDR block conflicts. We recommend that you select this option if you have an IPAM pool with a provisioned CIDR block. To configure the CIDR block, select an IPAM pool and then configure the IPv4 Mask.
To add a secondary IPv6 CIDR block:
If the VPC does not have IPv6 enabled, click 啟用 IPv6. You can select Allocated by system and then select Assign BGP (Multi-ISP) from the drop-down list. To centrally manage addresses, select Allocated by IPAM, choose an IPAM pool, and then select a mask or specify a CIDR block.
You can select the Automatically 啟用 IPv6 for All vSwitches checkbox. Alternatively, to enable IPv6 for a specific vSwitch, navigate to that vSwitch and click 啟用 IPv6 in the IPv6 CIDR column.
If the VPC already has IPv6 enabled, click Add IPv6 CIDR Block and select either Allocated by system or Allocated by IPAM.
刪除次要 CIDR 區塊
On the Basic Information page of the target VPC, go to the CIDR Block Management tab. On the IPv4 CIDR or IPv6 CIDR tab, locate the secondary CIDR block that you want to delete and click Delete in the Actions column.
API
Call AssociateVpcCidrBlock to add a secondary CIDR block.
Call UnassociateVpcCidrBlock to delete a secondary CIDR block.
Terraform
Terraform 目前僅支援新增次要 IPv4 CIDR 區塊。不支援新增次要 IPv6 CIDR 區塊。
資源: alicloud_vpc_ipv4_cidr_block
# 指定要建立 VPC 的地區。
provider "alicloud" {
region = "cn-hangzhou"
}
# 指定 VPC 的 ID。
variable "vpc_id" {
default = "vpc-xxx" # 替換為您實際的 VPC ID。
}
# 為 VPC 新增次要 CIDR 區塊。
resource "alicloud_vpc_ipv4_cidr_block" "example_secondary_cidr_block" {
vpc_id = var.vpc_id
secondary_cidr_block = "192.168.0.0/16" # 指定次要 CIDR 區塊。
}保留 CIDR 區塊
您可以在 vSwitch 中保留 CIDR 區塊,以防止其 IP 位址在建立其他資源時被自動指派。目前,保留的 CIDR 區塊僅可用於將 IP 前綴 指派給彈性網路介面(ENI)的次要私有 IP 位址。
1. 保留的 CIDR 區塊不能包含 vSwitch 的 系統保留 IP 位址。
2. 每個 VPC 支援最多 100 個保留的 IPv4 CIDR 區塊和 100 個保留的 IPv6 CIDR 區塊。
3. 保留的 IPv4 CIDR 區塊的最大遮罩長度為 /28,保留的 IPv6 CIDR 區塊的最大遮罩長度為 /80。
主控台
建立保留 CIDR 區塊
On the Basic Information page of the target vSwitch, click the Reserved CIDR Block tab to add a reserved IPv4 or IPv6 CIDR block. You can add a CIDR block in one of two ways:
Specify CIDR Block: Precisely control the address block that you want to reserve.
Specify Mask Length: The system automatically carves out a reserved CIDR block from the available address space.
For IPv6, if the vSwitch does not have IPv6 enabled, click 啟用 IPv6. In the 啟用 IPv6 dialog box, configure the IPv6 CIDR block for the vSwitch.
If IPv6 is not enabled for the VPC, in the pop-up 啟用 IPv6 dialog box, first set IPv6 CIDR Block Type to the default value Assign BGP (Multi-ISP), and then configure the IPv6 CIDR block for the vSwitch.
檢視已使用的 IP 位址
On the Reserved CIDR Block tab of the target vSwitch, click the IPv4 CIDR or IPv6 CIDR tab. Find the target reserved CIDR block and click View Used IP in the Actions column. You can view the used IP addresses and their corresponding ENIs.
刪除保留 CIDR 區塊
刪除保留的 CIDR 區塊之前,請確保其中沒有正在使用的 IP 位址。
On the Basic Information page of the target vSwitch, click the Reserved CIDR Block tab. On the IPv4 CIDR or IPv6 CIDR tab, find the reserved CIDR block that you want to delete and click Delete in the Actions column.
API
Call CreateVSwitchCidrReservation to create a reserved CIDR block.
Call GetVSwitchCidrReservationUsage to view used IP addresses.
Call DeleteVSwitchCidrReservation to delete a reserved CIDR block.
Terraform
資源: alicloud_vpc_vswitch_cidr_reservation
# 指定資源所在的地區。
provider "alicloud" {
region = "cn-hangzhou" # 資源所在的地區。
}
# 指定 vSwitch 的 ID。
variable "vsw_id" {
default = "vsw-xxx" # 替換為您實際的 vSwitch ID。
}
# 建立保留的 CIDR 區塊。
resource "alicloud_vpc_vswitch_cidr_reservation" "example_cidr_reservation" {
vswitch_id = var.vsw_id
ip_version = "IPv4"
cidr_reservation_cidr = "10.0.0.128/26" # 指定保留的 CIDR 區塊。
}使用 IPAM 建立 VPC
IPAM 是一種雲端工具,用於自動化 IP 位址的分配和管理,簡化網路管理並防止位址衝突。在您使用 IPAM 進行規劃(建立 IPAM 執行個體和 IPAM 集區)後,您可以從集區將 IPv4 和 IPv6 CIDR 區塊分配給您的 VPC。
主控台
建立 VPC 之前,請確保您已在 IPAM 主控台.
Go to the 建立 VPC page in the VPC console.
To allocate an IPv4 CIDR block, select Allocated by IPAM, choose an IPAM pool, and configure a mask. The system automatically allocates the first available CIDR block matching the specified mask length. You can also adjust the IPv4 CIDR block within the pre-provisioned range of the pool.
To enable IPv6, select Allocated by IPAM, choose an IPv6 IPAM pool, and then configure a mask or specify a CIDR block.
API
If you have an existing IPAM pool:
Allocate an IPv4 CIDR block: You can call CreateVpc and pass the
Ipv4IpamPoolIdparameter to specify an IPAM pool and theIpv4CidrMaskparameter to specify a mask. A CIDR block is then allocated from the IPAM pool and used as the IPv4 CIDR block for the new VPC. Alternatively, you can pass theCidrBlockparameter to specify the CIDR block for the VPC instead of automatically allocating a CIDR block by specifying a mask.Allocate an IPv6 CIDR block: If you pass both
Ipv6IpamPoolIdandIpv6CidrMask, an IPv6 CIDR block is allocated to the VPC from the specified IPv6 IPAM pool.
If you have not created an IPAM pool, call the following APIs in sequence to create one, and then create your VPC as described above.
Terraform
Terraform 目前僅支援從 IPAM 集區分配 IPv4 CIDR 區塊。不支援分配 IPv6 CIDR 區塊。
資源: vpc_ipam_ipam, alicloud_vpc_ipam_ipam_pool, alicloud_vpc_ipam_ipam_pool_cidr, alicloud_vpc
# 指定要建立 IPAM 執行個體、IPAM 集區和 VPC 的地區。
provider "alicloud" {
region = "cn-hangzhou"
}
# 建立 IPAM 執行個體。
resource "alicloud_vpc_ipam_ipam" "example_ipam" {
ipam_name = "example_ipam_name"
operating_region_list = ["cn-hangzhou"] # 指定 IPAM 執行個體的操作地區。
}
# 建立 IPAM 集區。
resource "alicloud_vpc_ipam_ipam_pool" "example_parentIpamPool" {
ipam_scope_id = alicloud_vpc_ipam_ipam.example_ipam.private_default_scope_id # 指定 IPAM 集區的範圍。
ipam_pool_name = "example_parentIpamPool_name"
pool_region_id = alicloud_vpc_ipam_ipam.example_ipam.region_id # 指定 IPAM 集區的地區。
ip_version = "IPv4" # 指定 IPAM 集區的 IP 版本。
}
# 為 IPAM 集區分配 CIDR。
resource "alicloud_vpc_ipam_ipam_pool_cidr" "example_ipamPoolCidr" {
cidr = "10.0.0.0/16" # 指定 CIDR。
ipam_pool_id = alicloud_vpc_ipam_ipam_pool.example_parentIpamPool.id # 指定 IPAM 集區的 ID。
}
# 建立 VPC。
resource "alicloud_vpc" "example_ipam_vpc" {
vpc_name = "example_ipam_vpc_name"
ipv4_ipam_pool_id = alicloud_vpc_ipam_ipam_pool.example_parentIpamPool.id # 指定 IPAM 集區的 ID。
ipv4_cidr_mask = 24 # IPv4 網路遮罩。
}
更多資訊
預設 VPC 和預設 vSwitch
預設 VPC 和預設 vSwitch help you verify and deploy services quickly. However, for production workloads or long-term services, we strongly recommend that you create custom VPC 和 vSwitch tailored to your business architecture. This provides granular control over network design, resource isolation, security policies, and scalability, ensuring your cloud environment is perfectly suited to your business needs.
每個地區只能建立一個預設 VPC,每個可用區只能建立一個預設 vSwitch。預設 VPC 和預設 vSwitch 不計入您的阿里雲資源配額。
當您在沒有 VPC 的地區建立 ECS、CLB 或 RDS 執行個體等資源時,您可以選擇讓阿里雲為您建立預設 VPC 和預設 vSwitch。以此方式建立的預設 VPC 具有固定的 CIDR 區塊 172.16.0.0/12。
在沒有預設 VPC 的地區,您可以呼叫 CreateDefaultVpc and CreateDefaultVSwitch 來建立預設 VPC 和預設 vSwitch。以此方式建立的預設 VPC 具有 172.xx.0.0/16 的 CIDR 區塊。
VPC 和 vSwitch created by any other method are considered non-default. You can delete default VPCs and default vSwitches, but you cannot convert them to non-default, or vice versa.
系統保留 IP 位址
每個 vSwitch 的 CIDR 區塊都包含系統保留的 IP 位址。您不能將這些位址指派給雲端資源,例如 ECS 執行個體。
對於 IPv4,每個 vSwitch 的 CIDR 區塊中的第一個 IP 位址和最後三個 IP 位址是保留的。
例如,如果 vSwitch 的 CIDR 區塊為 192.168.1.0/24,則位址 192.168.1.0、192.168.1.253、192.168.1.254 和 192.168.1.255 是保留的。
對於 IPv6,每個 vSwitch 的 CIDR 區塊中的第一個 IP 位址和最後九個 IP 位址是保留的。
For example, if the IPv6 CIDR block of a vSwitch is 2408:xxxx:xxxx:6eff::/64, the first address 2408:xxxx:xxxx:6eff:: and the last nine addresses 2408:xxxx:xxxx:6eff:ffff:ffff:ffff:fff7, 2408:xxxx:xxxx:6eff:ffff:ffff:ffff:fff8, 2408:xxxx:xxxx:6eff:ffff:ffff:ffff:fff9, 2408:xxxx:xxxx:6eff:ffff:ffff:ffff:fffa, 2408:xxxx:xxxx:6eff:ffff:ffff:ffff:fffb, 2408:xxxx:xxxx:6eff:ffff:ffff:ffff:fffc, 2408:xxxx:xxxx:6eff:ffff:ffff:ffff:fffd, 2408:xxxx:xxxx:6eff:ffff:ffff:ffff:fffe, and 2408:xxxx:xxxx:6eff:ffff:ffff:ffff:ffff are reserved.
跨帳號授權
在將 VPC 連接到跨帳號 CEN、VBR 或 ECR 執行個體之前,您必須先為 VPC 授予跨帳號授權。
有關授權說明,請參閱 授權跨帳號 CEN 執行個體, 授權跨帳號 VBR 執行個體, and 授權跨帳號 ECR 執行個體.
授權成功後,另一個帳號可以在以下操作時選擇您的 VPC 執行個體:建立 VPC 連線, 建立上行 VBR 連線, or 將 VPC 與 ECR 關聯.
1. 此處的「帳號」是指阿里雲帳號(主帳號),而非 RAM 使用者(子帳號)。
2. 跨帳號授權不支援跨站場景。換言之,不支援中國站(aliyun.com)與國際站(alibabacloud.com)之間的跨帳號授權。