API 範例-使用 VPN 連接兩個 VPC
下列 API 範例說明如何透過在每一個 VPC 中建立 VPN 閘道來連接兩個 VPC。 VPN 閘道可以連接其部署所在區域中的所有子網路。 因此,在您要交互連接的每一個區域中具有 VPN 閘道,會使這兩個區域中的子網路充當單一網路。 兩個 VPC 中子網路的 IP 位址不得重疊。
您可以使用 VPN 閘道來連接兩個 VPC。 但是,建議使用 IBM Cloud Transit Gateway 進行 VPC 對 VPC 連線。 如需詳細資訊,請參閱 IBM Cloud Transit Gateway 入門。
下圖顯示如何交互連接三個 VPC; 下列範例顯示如何僅連接前兩個 VPC。 您可以重複步驟來連接更多 VPC。
下列範例假設:
- 您已建立 VPC、子網路及虛擬伺服器實例。 如需建立 VPC 資源的相關資訊,請參閱 開始使用 Virtual Private Cloud(VPC)。
- 您設定 API 環境 並起始設定所有需要的變數。
您也可以建立 VPN 閘道,並使用使用者介面來新增 VPN 連線。 如需指示,請參閱 使用使用者介面建立 VPN 閘道。
步驟 1. 在第一個 VPC 中建立 VPN 閘道
VPN 閘道會建立在與您所選取子網路相關聯的區域中。 若要取得最佳效能,請在不含任何其他 VPC 資源的子網路中建立 VPN 閘道,以確保閘道有足夠的專用 IP 可用。 VPN 閘道需要 8 個私有 IP 位址,以因應高可用性和滾動升級。
VPN 閘道只能連接至所部署區域中的虛擬伺服器實例。 其他區域中的實例無法使用此 VPN 閘道來與其他網路進行通訊。 針對區域容錯,每個區域部署一個 VPN 閘道。
使用 get subnet 指令尋找子網路 ID,然後移入變數:
export SubnetId1=<your_subnet_id>
下列指令會在 Default
資源群組中部署 VPN 閘道。
curl -X POST "$vpc_api_endpoint/v1/vpn_gateways?version=$api_version&generation=2" \
-H "Authorization: $iam_token" \
-d '{
"name": "vpn-gateway-1",
"subnet": {
"id": "'$SubnetId1'"
}
}'
輸出範例:
{
"id": "0738-7fd72524-6e2d-49a6-b975-0071efccd89a",
"crn": "crn:v1:bluemix:public:is:us-south:a/a1234567:vpn:0738-7fd72524-6e2d-49a6-b975-0071efccd89a",
"name": "vpn-gateway-1",
"href": "https://us-south.iaas.cloud.ibm.com/v1/vpn_gateways/0738-7fd72524-6e2d-49a6-b975-0071efccd89a",
"created_at": "2018-07-06T19:19:28.694388Z",
"status": "pending",
"public_ip": {
"address": "169.61.161.167"
},
"subnet": {
"id": "0738-f45ee0be-cf3f-41ca-a279-23139110aa58",
"name": "subnet-1",
"href": "https://us-south.iaas.cloud.ibm.com/v1/subnets/0738-f45ee0be-cf3f-41ca-a279-23139110aa58"
},
"resource_group": {
"id": "d28a2jsiw1pl2g22q8462tyr321416z2",
"href": "https://resource-manager.cloud.ibm.com/v1/resource_groups/d28a2jsiw1pl2g22q8462tyr321416z2"
}
}
儲存第一個 VPN 閘道的 id
,例如:
export gwid1=<your_vpngateway_id>
當 VPN 閘道正在建立時,閘道狀態會顯示為 pending
,當建立完成時,狀態會變成 available
。 創建可能需要幾分鐘。
您可以使用下列指令來檢查閘道的狀態:
curl -X GET "$vpc_api_endpoint/v1/vpn_gateways/$gwid1?version=$api_version&generation=2" \
-H "Authorization: $iam_token"
儲存第一個 VPN 閘道的 public_ip.address
,例如:
export gwaddress1=<your_vpngateway_public_ip>
步驟 2. 在第二個 VPC 中建立 VPN 閘道
使用 get subnet 指令尋找第二個 VPC 的子網路 ID,然後移入變數:
export SubnetId2=<your_subnet_id>
下列指令會在 Default
資源群組中部署 VPN 閘道。 如果第二個 VPC 屬於不同的區域,請確保更新變數 vpc_api_endpoint
。 請參閱 API 端點 清單。
curl -X POST "$vpc_api_endpoint/v1/vpn_gateways?version=$api_version&generation=2" \
-H "Authorization: $iam_token" \
-d '{
"name": "vpn-gateway-2",
"subnet": {
"id": "'$SubnetId2'"
}
}'
輸出範例:
{
"id": "0738-f72559a3-2fac-4958-b937-54474e6a8a8d",
"crn": "crn:v1:bluemix:public:is:us-south:a/a1234567::vpn:0738-f72559a3-2fac-4958-b937-54474e6a8a8d",
"name": "vpn-gateway-2",
"href": "https://us-south.iaas.cloud.ibm.com/v1/vpn_gateways/0738-f72559a3-2fac-4958-b937-54474e6a8a8d",
"created_at": "2018-07-06T19:33:23.789675Z",
"status": "pending",
"public_ip": {
"address": "169.61.161.150"
},
"subnet": {
"id": "0738-f72c7f7c-0fa5-42d1-9bdc-9e0acad53cb4",
"name": "subnet-2",
"href": "https://us-south.iaas.cloud.ibm.com/v1/subnets/0738-f72c7f7c-0fa5-42d1-9bdc-9e0acad53cb4"
},
"resource_group": {
"id": "d28a2jsiw1pl2g22q8462tyr321416z2",
"href": "https://resource-manager.cloud.ibm.com/v1/resource_groups/d28a2jsiw1pl2g22q8462tyr321416z2"
}
}
儲存第二個 VPN 閘道的 id
,例如:
export gwid2=<your_vpngateway_id>
當 VPN 閘道正在建立時,閘道狀態會顯示為 pending
,當建立完成時,狀態會變成 available
。 創建可能需要幾分鐘。
您可以使用下列指令來檢查閘道的狀態:
curl -X GET "$vpc_api_endpoint/v1/vpn_gateways/$gwid2?version=$api_version&generation=2" \
-H "Authorization: $iam_token"
儲存第二個 VPN 閘道的 public_ip.address
,例如:
export gwaddress2=<your_vpngateway_public_ip>
步驟 3. 從第一個 VPN 閘道建立 VPN 連線到第二個 VPN 閘道
當您建立 VPC 1 的 VPN 閘道連線時,請將 local_cidrs
設為 VPC 1 上的子網路,並將 peer_cidrs
設為 VPC 2 上的子網路。 以逗號分隔多個 CIDR。 請記得根據 VPN 閘道所在的地區來更新變數 vpc_api_endpoint
。
export Vpc1Subnets=<your_vpc1_subnets>
export Vpc2Subnets=<your_vpc2_subnets>
curl -X POST "$vpc_api_endpoint/v1/vpn_gateways/$gwid1/connections?version=$api_version&generation=2" \
-H "Authorization: $iam_token" \
-d '{
"name": "vpn-connection-to-vpn-gateway-2",
"peer_address": "'$gwaddress2'",
"psk": "VPNDemoPassword",
"local_cidrs": [ "'$Vpc1Subnets'" ],
"peer_cidrs": [ "'$Vpc2Subnets'" ]
}'
輸出範例:
{
"id": "0738-a252d380-0784-45ff-8fc0-c2b58e446b4d",
"name": "vpn-connection-to-vpn-gateway-2",
"href": "https://us-south.iaas.cloud.ibm.com/v1/vpn_gateways/0738-7fd72524-6e2d-49a6-b975-0071efccd89a/connections/0738-a252d380-0784-45ff-8fc0-c2b58e446b4d",
"local_cidrs": [
"192.168.100.0/24"
],
"peer_cidrs": [
"192.168.0.0/24"
],
"peer_address": "169.61.161.150",
"admin_state_up": true,
"psk": "VPNDemoPassword",
"dead_peer_detection": {
"action": "none",
"interval": 30,
"timeout": 120
},
"created_at": "2018-07-06T19:50:49.252072Z",
"route_mode": "policy",
"authentication_mode": "psk",
"status": "down"
}
步驟 4. 從第二個 VPN 閘道建立 VPN 連線到第一個 VPN 閘道
當您建立 VPC 2 的 VPN 閘道連線時,請將 local_cidrs
設為 VPC 2 上的子網路,並將 peer_cidrs
設為 VPC 1 上的子網路。 請記得根據 VPN 閘道所在的地區來更新變數 vpc_api_endpoint
。
curl -X POST "$vpc_api_endpoint/v1/vpn_gateways/$gwid2/connections?version=$api_version&generation=2" \
-H "Authorization: $iam_token" \
-d '{
"name": "vpn-connection-to-vpn-gateway-1",
"peer_address": "'$gwaddress1'",
"psk": "VPNDemoPassword",
"local_cidrs": [ "'$Vpc2Subnets'" ],
"peer_cidrs": [ "'$Vpc1Subnets'" ]
}'
輸出範例:
{
"id": "0738-1d4dbacq-673d-2qed-hf68-858961739gf0",
"name": "vpn-connection-to-vpn-gateway-1",
"href": "https://us-south.iaas.cloud.ibm.com/v1/vpn_gateways/0738-f72559a3-2fac-4958-b937-54474e6a8a8d/connections/0738-1d4dbacq-673d-2qed-hf68-858961739gf0",
"local_cidrs": [
"192.168.100.0/24"
],
"peer_cidrs": [
"192.168.100.0/24"
],
"peer_address": "169.61.161.167",
"admin_state_up": true,
"psk": "VPNDemoPassword",
"dead_peer_detection": {
"action": "none",
"interval": 30,
"timeout": 120
},
"created_at": "2018-07-06T19:54:14.961597Z",
"route_mode": "policy",
"authentication_mode": "psk",
"status": "down"
}
步驟 5. 驗證連線功能
您可以檢查 VPN 連線的狀態,如下所示:
curl -X GET "$vpc_api_endpoint/v1/vpn_gateways/$gwid1/connections?version=$api_version&generation=2" \
-H "Authorization: $iam_token"
輸出範例:
{
"first": {
"href": "https://us-south.iaas.cloud.ibm.com/v1/vpn_gateways/0738-7fd72524-6e2d-49a6-b975-0071efccd89a/connections?limit=10"
},
"limit": 10,
"connections": [
{
"id": "0738-a252d380-0784-45ff-8fc0-c2b58e446b4d",
"name": "vpn-connection-to-vpn-gateway-2",
"href": "https://us-south.iaas.cloud.ibm.com/v1/vpn_gateways/0738-7fd72524-6e2d-49a6-b975-0071efccd89a/connections/0738-a252d380-0784-45ff-8fc0-c2b58e446b4d",
"local_cidrs": [
"192.168.100.0/24"
],
"peer_cidrs": [
"192.168.0.0/24"
],
"peer_address": "169.61.161.150",
"admin_state_up": true,
"psk": "VPNDemoPassword",
"dead_peer_detection": {
"action": "none",
"interval": 30,
"timeout": 120
},
"created_at": "2018-07-06T19:50:49.252072Z",
"route_mode": "policy",
"authentication_mode": "psk",
"status": "up"
}
]
}
建立 VPN 連線之後,您可以從 VPC 1 中的子網路連接至 VPC 2 中的虛擬伺服器實例,反之亦然。