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。

使用 VPN 网关实现 VPC 互联
使用 VPN 网关实现 VPC 互联

以下示例假定:

  1. 您已创建 VPC,子网和虚拟服务器实例。 有关创建 VPC 资源的更多信息,请参阅 Virtual Private Cloud(VPC)入门
  2. 设置 API 环境 并初始化所需的所有变量。

您还可以创建 VPN 网关,并使用 UI 添加 VPN 连接。 有关指示信息,请参阅 使用 UI 创建 VPN 网关

步骤 1. 在第一个 VPC 中创建 VPN 网关

将在与所选子网关联的专区中创建 VPN 网关。 为了获得最佳性能,请在没有任何其他 VPC 资源的子网中创建 VPN 网关,以确保有足够的专用 IP 可用于该网关。 VPN网关需要8个私有IP地址,以满足高可用性和滚动升级的需求。

VPN 网关只能连接到其所部署的区域中的虚拟服务器实例。 其他区域中的实例无法使用此 VPN 网关与其他网络进行通信。 为了实现区域容错,请为每个区域部署一个 VPN 网关。

使用 get subnet 命令查找子网标识,然后填充变量:

export SubnetId1=<your_subnet_id>

以下命令将在 Default 资源组中部署 VPN 网关。

curl -X POST "$vpc_api_endpoint/v1/vpn_gateways?version=$api_version&generation=2" \
    -H "Authorization: Bearer $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: Bearer $iam_token"

保存第一个 VPN 网关的 public_ip.address,例如:

export gwaddress1=<your_vpngateway_public_ip>

步骤 2. 在第二个 VPC 中创建 VPN 网关

使用 get subnet 命令查找第二个 VPC 的子网标识,然后填充变量:

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: Bearer $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: Bearer $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: Bearer $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: Bearer $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: Bearer $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 中的子网访问虚拟服务器实例,反之亦然。