Help Center> Virtual Private Cloud> API Reference> Application Examples> Example 4: Assigning a Virtual IPv6 Address to ECSs for HA
Updated on 2022-12-13 GMT+08:00

Example 4: Assigning a Virtual IPv6 Address to ECSs for HA

Scenarios

Virtual IP addresses are used for high availability as they make active/standby ECS switchover possible. This way if one ECS goes down for some reason, the other one can take over and services continue uninterrupted.

This section describes how to assign a virtual IPv6 address to ECSs for HA by calling APIs.

Prerequisites

  • You have created a VPC and a subnet that support both IPv4 and IPv6 and obtained the VPC ID and subnet ID. For details, see Creating a VPC and Subnet.
  • You have purchased an ECS. For details, see Overview.
  • If you use a token for authentication, you must obtain the token and add X-Auth-Token to the request header when making an API call. Obtain the token by referring to Authentication.

The token obtained from IAM is valid for only 24 hours. If you want to use a token for authentication, you can cache it to avoid frequent calling.

Procedure

  1. Assign a virtual IPv6 address.
    1. Send POST https://VPC endpoint/v2.0/ports.
    2. Add X-Auth-Token to the request header.
    3. Set the following parameters in the request body. The subnet where the virtual IP address resides must be the same as that of the ECS. Set subnet_id to the ID of the IPv6 subnet.
      {
          "port":{
              "network_id":"b0ad9b80-bb16-4550-8ce0-514f949e35ee",
              "device_owner":"neutron:VIP_PORT",
              "name":"ipv6_vip_port_test",
              "fixed_ips":[
                  {
                      "subnet_id":"33ce2628-6246-4e3a-859f-99cd753ff704"
                  }
              ]
          }
      }
    4. Check the response message.
      • The request is successful if the following response is displayed:
        {
            "port": {
                "id": "d92cfee7-9ebe-4483-85c1-00ffb1e45cd8",
                "name": "ipv6_vip_port_test",
                "status": "DOWN",
                "admin_state_up": true,
                "fixed_ips": [
                    {
                        "subnet_id": "33ce2628-6246-4e3a-859f-99cd753ff704",
                        "ip_address": "2001:db8:a583:21d:2e25:9403:6f3d:4664"
                    }
                ],
                "mac_address": "fa:16:3e:99:2e:92",
                "network_id": "b0ad9b80-bb16-4550-8ce0-514f949e35ee",
                "tenant_id": "060576782980d5762f9ec014dd2f1148",
                "project_id": "060576782980d5762f9ec014dd2f1148",
                "device_id": "",
                "device_owner": "neutron:VIP_PORT",
                "security_groups": [],
                "extra_dhcp_opts": [],
                "allowed_address_pairs": [],
                "binding:vnic_type": "normal",
                "binding:vif_details": {},
                "binding:profile": {},
                "port_security_enabled": true,
                "created_at": "2020-12-15T03:01:07",
                "updated_at": "2020-12-15T03:01:07"
            }
        }
      • For details about the error codes displayed if the request fails, see section Error Codes.
  2. Query the NIC information according to the ECS ID. The value of fixed_ips contains IPv4 and IPv6 addresses.
    1. Send GET https://VPC endpoint/v2.0/ports?device_id={ecs_id}&network_id={network_id}.
    2. Add X-Auth-Token to the request header.
    3. Check the response message.
      • The request is successful if the following response is displayed:
        { 
             "ports": [{
                    "id": "47b4cd46-cfe5-415d-957f-5068189dce94",
                    "name": "",
                    "status": "ACTIVE",
                    "admin_state_up": true,
                    "fixed_ips": [
                        {
                            "subnet_id": "0dd17989-1c23-4501-8dc1-40e4085f793f",
                            "ip_address": "172.16.0.191"
                        },
                        {
                            "subnet_id": "33ce2628-6246-4e3a-859f-99cd753ff704",
                            "ip_address": "2001:db8:a583:21d:dfc0:d452:e9ab:65cf"
                        }
                    ],
                    "mac_address": "fa:16:3e:1e:f7:9a",
                    "network_id": "b0ad9b80-bb16-4550-8ce0-514f949e35ee",
                    "tenant_id": "060576782980d5762f9ec014dd2f1148",
                    "project_id": "060576782980d5762f9ec014dd2f1148",
                    "device_id": "ab7ca781-66bf-48a8-814b-1568cb393a38",
                    "device_owner": "compute:xxx",
                    "security_groups": [
                        "0552091e-b83a-49dd-88a7-4a5c86fd9ec3"
                    ],
                    "extra_dhcp_opts": [],
                    "allowed_address_pairs": [],
                    "binding:vnic_type": "normal",
                    "binding:vif_details": {
                        "primary_interface": true
                    },
                    "binding:profile": {},
                    "port_security_enabled": true,
                    "dns_assignment": [
                        {
                            "hostname": "ip-172-16-0-191",
                            "ip_address": "172.16.0.191",
                            "fqdn": "ip-172-16-0-191.br-iaas-odin1.compute.internal."
                        }
                    ],
                    "dns_name": "ip-172-16-0-191",
                    "created_at": "2020-11-19T13:32:37",
                    "updated_at": "2020-11-19T13:33:50"
                }] 
         }
      • For details about the error codes displayed if the request fails, see section Error Codes.
  3. Bind an ECS to the virtual IP address.
    1. Send PUT https://VPC endpoint/v2.0/ports/{port_id}. port_id indicates the port ID corresponding to the assigned virtual IPv6 address.
    2. Add X-Auth-Token to the request header.
    3. Set the following parameters in the request body. Set the value of ip_address to the NIC IPv6 address of the ECS obtained in 2.
      { 
           "port": { 
               "allowed_address_pairs": [{ 
                   "ip_address": "2001:db8:a583:21d:dfc0:d452:e9ab:65cf" 
               }] 
           } 
       }
    4. Check the response message.
      • The request is successful if the following response is displayed:
        {
            "port": {
                "id": "d92cfee7-9ebe-4483-85c1-00ffb1e45cd8",
                "name": "ipv6_vip_port_test",
                "status": "DOWN",
                "admin_state_up": true,
                "fixed_ips": [
                    {
                        "subnet_id": "33ce2628-6246-4e3a-859f-99cd753ff704",
                        "ip_address": "2001:db8:a583:21d:2e25:9403:6f3d:4664"
                    }
                ],
                "mac_address": "fa:16:3e:99:2e:92",
                "network_id": "b0ad9b80-bb16-4550-8ce0-514f949e35ee",
                "tenant_id": "060576782980d5762f9ec014dd2f1148",
                "project_id": "060576782980d5762f9ec014dd2f1148",
                "device_id": "",
                "device_owner": "neutron:VIP_PORT",
                "security_groups": [],
                "extra_dhcp_opts": [],
                "allowed_address_pairs": [{ 
                     "ip_address": "2001:db8:a583:21d:dfc0:d452:e9ab:65cf " 
                 }],
                "binding:vnic_type": "normal",
                "binding:vif_details": {},
                "binding:profile": {},
                "port_security_enabled": true,
                "created_at": "2020-12-15T03:01:07",
                "updated_at": "2020-12-15T03:01:07"
            }
        }
      • For details about the error codes displayed if the request fails, see section Error Codes.
  4. Disable the source/destination check function for the ECS NIC.
    1. Send PUT https://VPC endpoint/v2.0/ports/{port_id}. port_id is the NIC ID obtained in 2.
    2. Add X-Auth-Token to the request header.
    3. Set the following parameters in the request body. Set the value of ip_address to 1.1.1.1/0, the NIC IP address of the ECS.
      {
      	"port": {
      		"allowed_address_pairs": [{
      			"ip_address": "1.1.1.1/0"
      		}]
      	}
      }
    4. Check the response message.
      • The request is successful if the following response is displayed:
        { 
             "port": { 
        "id": "47b4cd46-cfe5-415d-957f-5068189dce94",
                    "name": "",
                    "status": "ACTIVE",
                    "admin_state_up": true,
                    "fixed_ips": [
                        {
                            "subnet_id": "0dd17989-1c23-4501-8dc1-40e4085f793f",
                            "ip_address": "172.16.0.191"
                        },
                        {
                            "subnet_id": "33ce2628-6246-4e3a-859f-99cd753ff704",
                            "ip_address": "2001:db8:a583:21d:dfc0:d452:e9ab:65cf"
                        }
                    ],
                    "mac_address": "fa:16:3e:1e:f7:9a",
                    "network_id": "b0ad9b80-bb16-4550-8ce0-514f949e35ee",
                    "tenant_id": "060576782980d5762f9ec014dd2f1148",
                    "project_id": "060576782980d5762f9ec014dd2f1148",
                    "device_id": "ab7ca781-66bf-48a8-814b-1568cb393a38",
                    "device_owner": "compute:xxx",
                    "security_groups": [
                        "0552091e-b83a-49dd-88a7-4a5c86fd9ec3"
                    ],
                    "extra_dhcp_opts": [],
                    "allowed_address_pairs": [{ 
                     "ip_address": "1.1.1.1/0" 
                 }],
                    "binding:vnic_type": "normal",
                    "binding:vif_details": {
                        "primary_interface": true
                    },
                    "binding:profile": {},
                    "port_security_enabled": true,
                    "dns_assignment": [
                        {
                            "hostname": "ip-172-16-0-191",
                            "ip_address": "172.16.0.191",
                            "fqdn": "ip-172-16-0-191.br-iaas-odin1.compute.internal."
                        }
                    ],
                    "dns_name": "ip-172-16-0-191",
                    "created_at": "2020-11-19T13:32:37",
                    "updated_at": "2020-11-19T13:33:50"
        
             } 
         }
      • For details about the error codes displayed if the request fails, see section Error Codes.