This topic describes how to implement high availability by using high-availability virtual IP addresses (HAVIPs) and keepalived.
Prerequisites
- The HAVIP feature is in public preview. To use the feature, log on to the Quota Center console to submit an application.
- An elastic IP address (EIP) is created. For more information, see Apply for an EIP.
- Two Elastic Compute Service (ECS) instances are created and NGINX is deployed in both ECS instances. For more information, see Create an instance by using the wizard.
Background information
The following scenario is used as an example in this topic. A company has created two ECS instances and NGINX is deployed on both ECS instances. High availability is required due to business growth.
The ECS instances can function as a primary and secondary ECS instance with HAVIPs and keepalived. By default, the primary ECS instance (ECS 1) is accessed by the clients. If ECS 1 is not working as expected, the secondary ECS instance (ECS 2) takes over the services and virtual IP addresses of ECS 1. This implements high availability.

Procedure

Step 1: Create an HAVIP
HAVIPs are private IP address that can be created and released as independent resources. After you associate an HAVIP with an ECS instance, the ECS instance can advertise the HAVIP by sending Address Resolution Protocol (ARP) messages.
- Log on to the VPC console.
- In the left-side navigation pane, click HaVip.
- In the top navigation bar, select the region where you want to create the HAVIP.
- On the HaVip page, click Create HaVip.
- In the Create HaVip dialog box, set the following parameters and click OK:
- Region: displays the region of the HAVIP.
- VPC: specifies the VPC to which the HAVIP belongs.
- vSwitch: specifies the vSwitch to which the HAVIP belongs.
- vSwitch CIDR Block: displays the CIDR block of the vSwitch.
- Whether to automatically assign private IP addresses: specifies whether to automatically allocate private IP addresses.
- Yes: The system automatically allocates an idle private IP address from the vSwitch CIDR block.
- No: You must manually enter an idle private IP address from the vSwitch CIDR block.

Step 2: Install keepalived on the primary and secondary ECS instances
You can use keepalived to check the status of ECS instances. If ECS 1 is down, ECS 2 takes over the workloads and virtual IP address of ECS 1. This ensures high availability.
The following procedure describes how to install keepalived on an ECS instance that runs CentOS.
- Log on to ECS 1. For more information, see Connection methods.
- Run the following command to install keepalived:
yum install keepalived
- Modify the configuration file of keepalived.
- Run the following command to start keepalived on ECS 1:
systemctl start keepalived
- Log on to ECS 2.
- Run the following command to install keepalived:
yum install keepalived
- Modify the configuration file of keepalived.
- Run the following command to start keepalived on ECS 2:
systemctl start keepalived
Step 3: Associate the HAVIP with both ECS instances
Perform the following steps to associate the HAVIP with both the primary and secondary ECS instances:
- Log on to the VPC console.
- In the left-side navigation pane, click HaVip.
- In the top navigation bar, select the region where the HAVIP is created.
- Find the HAVIP created in Step 1: Create an HAVIP and click the ID of the HAVIP.
- In the Resources section, find ECS Instances and click Bind.
- In the dialog box that appears, select ECS 1 and click OK.
- Repeat the preceding steps to associate the HAVIP with ECS 2.

Step 4: Associate the HAVIP with an EIP
You can associate the HAVIP with an EIP. Then, the HAVIP can use the EIP to provide services over the Internet.
- Log on to the VPC console.
- In the left-side navigation pane, click HaVip.
- In the top navigation bar, select the region where the HAVIP is created.
- Find the HAVIP created in Step 1: Create an HAVIP and click the ID of the HAVIP.
- In the Resources section, click Bind next to EIP.
- In the dialog box that appears, select the EIP and click OK.

Step 5: Test high availability
Perform the following steps to test high availability:
- Open the browser on an on-premises computer.
- Enter the IP address of the EIP to access NGINX deployed on the ECS instance. Test results:
- When ECS 1 works as expected, ECS 1 is accessed.
- When ECS 1 is down, ECS 2 takes over the virtual IP address and workloads of ECS 1. This ensures high availability.
- When ECS 1 works as expected, ECS 1 is accessed.