A high-availability virtual IP address (HaVip) keeps your service IP unchanged during failover between ECS instances in the same zone.
How it works
A typical high-availability cluster consists of one HaVip and two ECS instances in a primary/secondary configuration:
-
Keepalived configuration: Associate the HaVip with ECS1 and ECS2, and install Keepalived on both. Set
virtual_ipaddressto the HaVip address and assign apriorityvalue — higher priority wins the primary election. -
Primary election: Keepalived compares
priorityvalues via VRRP and elects the higher-priority instance as primary. The system updates the HaVip mapping, and all traffic destined for the HaVip forwards to ECS1. -
Failover: ECS1 sends heartbeats to ECS2 at the interval specified by
advert_int. If ECS2 misses a heartbeat within the timeout, Keepalived promotes ECS2 to primary. The system updates the HaVip mapping to ECS2, and all traffic shifts accordingly — the service IP remains unchanged.
To enable public access, associate the HaVip with an EIP. Traffic then routes through the EIP to provide highly available internet-facing services.
Use an HaVip for failover
Associate an HaVip with ECS instances or elastic network interfaces (ENIs) in the same vSwitch. Combined with Keepalived, this keeps your service IP unchanged during failover.
-
Quota: Log on to the Quota Center console and apply for HaVip creation permission. A quota value of 1 means HaVip creation is enabled. Each account can create up to 50 HaVips.
-
IP version: HaVip supports only IPv4.
-
Associated resources:
-
An HaVip can be associated with only one resource type at a time. To switch types, disassociate the current resources first.
-
When you associate an HaVip with an ENI, make sure the ENI is attached to an ECS instance.
-
If an associated ECS instance or ENI is deleted, the system automatically disassociates the HaVip from that resource.
-
Detaching a secondary ENI from an ECS instance does not affect the HaVip-to-ENI association.
-
Console
Create and associate an HaVip
-
Navigate to the VPC Console - HaVip page. Select the region of your ECS instances, and then click Create HaVip.
-
Select the VPC and vSwitch to which the ECS instances belong. You can automatically assign a private IP address from the vSwitch's CIDR block or specify an unassigned IP address.
-
Install Keepalived on the primary and standby ECS instances, and run
systemctl start keepalivedto start Keepalived. -
Click the ID of the target HaVip. In the Resources section, click Bind to the right of ECS Instances, and then select the ECS instances or ENIs to associate.
After the association is complete, you can view the primary and secondary status in the Associated Instance column or in the Resources section of the details page.
-
Verify the result:
-
Run the following commands on the primary and secondary instances to create a web test service that returns different results.
Check the port usage by running the
netstat -an | grep 8000command. If port 8000 is occupied, you need to select another port.Primary instance:
echo "ECS 1" > index.html # The primary instance returns "ECS 1" python3 -m http.server 8000Secondary instance:
echo "ECS 2" > index.html # The secondary instance returns "ECS 2" python3 -m http.server 8000 -
From another ECS instance in the same VPC, running
curl <havip_private_ip>:8000returnsECS 1. After the primary server stops, it returnsECS 2.Ensure that the security groups of the primary and secondary instances allow HTTP traffic from within the same VPC to access port 8000.
-
Disassociate resources
Click the ID of the target HaVip. In the Resources section, find the target ECS instance or elastic network interface in the Associated resources list, and click Delete Association.
Delete an HaVip
Before you delete an HaVip, make sure it is not associated with any ECS instances, ENIs, or EIPs. In the Actions column for the target HaVip, or on its details page, click Delete.
API
-
Call CreateHaVip to create an HaVip.
-
Call AssociateHaVip to associate an HaVip with an ECS instance or ENI.
-
Call UnassociateHaVip to disassociate an HaVip from an ECS instance or ENI.
-
Call DeleteHaVip to delete an HaVip.
Terraform
Resource: alicloud_havip, alicloud_havip_attachment, alicloud_instance, alicloud_security_group, alicloud_security_group_rule
# Specify the region where you want to create the HaVip.
provider "alicloud" {
region = "cn-hangzhou"
}
# Specify the ID of the VPC.
variable "vpc_id" {
default = "vpc-bp1k******" # Replace with the actual ID of your VPC.
}
# Specify the vSwitch ID.
variable "vswitch_id" {
default = "vsw-bp1y******" # Replace with the actual ID of your vSwitch.
}
# Specify the instance type.
variable "instance_type" {
default = "ecs.e-c1m1.large"
}
# Specify the image ID.
variable "image_id" {
default = "aliyun_3_x64_20G_alibase_20221102.vhd"
}
# Create an HaVip.
resource "alicloud_havip" "test_havip" {
ha_vip_name = "test_havip_name"
vswitch_id = var.vswitch_id
ip_address = "192.168.0.24" # Specify an IP address for the HaVip from the vSwitch CIDR block. If not specified, the system assigns one.
}
# Create a security group.
resource "alicloud_security_group" "test_security_group" {
security_group_name = "test_security_group_name"
vpc_id = var.vpc_id
}
# Create a security group rule. You need to adjust the protocol, source, and port based on your actual traffic.
resource "alicloud_security_group_rule" "allow_vpc_tcp" {
type = "ingress"
ip_protocol = "tcp"
nic_type = "intranet"
policy = "accept"
port_range = "8000/8000"
priority = 1
security_group_id = alicloud_security_group.test_security_group.id
cidr_ip = "192.168.0.0/24"
}
# Create the primary server.
resource "alicloud_instance" "test_master_instance" {
instance_name = "test_master_instance_name"
vswitch_id = var.vswitch_id
instance_type = var.instance_type
image_id = var.image_id
system_disk_category = "cloud_essd"
security_groups = [alicloud_security_group.test_security_group.id]
user_data = base64encode(<<-EOT
#!/bin/sh
yum install keepalived -y
printf '! Configuration File for keepalived
vrrp_instance VI_1 {
state MASTER # Set as the primary instance.
interface eth0 # The network interface to which the virtual IP address is bound. This example uses eth0.
virtual_router_id 51 # The virtual_router_id of the primary/secondary cluster. Different clusters in the same VPC require different virtual_router_id values.
nopreempt # Set to non-preemptive mode.
priority 100 # The priority. A higher value indicates a higher priority. This example sets the priority to 100 to make this instance the primary instance.
advert_int 1 # The interval at which heartbeat messages are sent, in seconds. A small value makes the configuration susceptible to network jitter, which can cause frequent failovers or temporary split-brain scenarios. A large value can increase the failover time after the primary instance fails.
authentication {
auth_type PASS
auth_pass 1111
}
unicast_src_ip 192.168.0.25 # The private IP address of this instance. This example uses 192.168.0.25.
unicast_peer {
192.168.0.26 # The private IP address of the peer instance. This example uses 192.168.0.26. If you have multiple secondary ECS instances, declare the IP address of each peer instance on a new line. Do not use commas or other separators.
}
virtual_ipaddress {
192.168.0.24 # The virtual IP address. Set this to the HaVip's IP address. This example uses 192.168.0.24.
}
garp_master_delay 1 # The delay in seconds before updating the ARP cache after a failover to this instance.
garp_master_refresh 5 # The interval at which ARP messages are sent, in seconds.
track_interface {
eth0 # The network interface to which the virtual IP address is bound. This example uses eth0.
}
}' > /etc/keepalived/keepalived.conf
systemctl start keepalived
EOT
) # Specify the initialization script for the primary server to install Keepalived.
private_ip = "192.168.0.25" # Specify the private IP address of the primary server.
instance_charge_type = "PostPaid" # Set the billing method to pay-as-you-go.
spot_strategy = "SpotWithPriceLimit" # Set as a spot instance with a price limit.
}
# Create the secondary server.
resource "alicloud_instance" "test_backup_instance" {
instance_name = "test_backup_instance_name"
vswitch_id = var.vswitch_id
instance_type = var.instance_type
image_id = var.image_id
system_disk_category = "cloud_essd"
security_groups = [alicloud_security_group.test_security_group.id]
user_data = base64encode(<<-EOT
#!/bin/sh
yum install keepalived -y
printf '! Configuration File for keepalived
vrrp_instance VI_1 {
state BACKUP # Set as the secondary instance.
interface eth0 # The network interface to which the virtual IP address is bound. This example uses eth0.
virtual_router_id 51 # The virtual_router_id of the primary/secondary cluster. Different clusters in the same VPC require different virtual_router_id values.
nopreempt # Set to non-preemptive mode.
priority 10 # The priority. A higher value indicates a higher priority. This example sets the priority to 10 to make this instance the secondary instance.
advert_int 1 # The interval at which heartbeat messages are sent, in seconds. A small value makes the configuration susceptible to network jitter, which can cause frequent failovers or temporary split-brain scenarios. A large value can increase the failover time after the primary instance fails.
authentication {
auth_type PASS
auth_pass 1111
}
unicast_src_ip 192.168.0.26 # The private IP address of this instance. This example uses 192.168.0.26.
unicast_peer {
192.168.0.25 # The private IP address of the peer instance. This example uses 192.168.0.25. If you have multiple secondary ECS instances, declare the IP address of each peer instance on a new line. Do not use commas or other separators.
}
virtual_ipaddress {
192.168.0.24 # The virtual IP address. Set this to the HaVip's IP address. This example uses 192.168.0.24.
}
garp_master_delay 1 # The delay in seconds before updating the ARP cache after a failover to this instance.
garp_master_refresh 5 # The interval at which ARP messages are sent, in seconds.
track_interface {
eth0 # The network interface to which the virtual IP address is bound. This example uses eth0.
}
}' > /etc/keepalived/keepalived.conf
systemctl start keepalived
EOT
) # Specify the initialization script for the secondary server to install Keepalived.
private_ip = "192.168.0.26" # Specify the private IP address of the secondary server.
instance_charge_type = "PostPaid" # Set the billing method to pay-as-you-go.
spot_strategy = "SpotWithPriceLimit" # Set as a spot instance with a price limit.
}
# Associate with the primary server.
resource "alicloud_havip_attachment" "test_havip_attachment" {
ha_vip_id = alicloud_havip.test_havip.id
instance_id = alicloud_instance.test_master_instance.id # Specify the ID of the instance to associate with the HaVip.
}
# Associate with the secondary server.
resource "alicloud_havip_attachment" "test_havip_attachment_new" {
ha_vip_id = alicloud_havip.test_havip.id
instance_id = alicloud_instance.test_backup_instance.id # Specify the ID of the instance to associate with the HaVip.
}
Associate an EIP for public access
An HaVip is a private IP within a vSwitch. To enable internet access, associate an Elastic IP Address (EIP) with the HaVip. Note that EIPs incur fees.
1. The EIP must be in the same region as the HaVip and be available.
2. When an ECS instance accesses the internet through an EIP associated with an HaVip, the source IP of outbound packets is the HaVip's private IP, not the ECS instance's private IP.
Console
Associate or disassociate an EIP
Before associating an EIP, create one in the EIP console or click Create EIP on the association page.
In the Actions column for the target HaVip, click Associate EIP or Disassociate EIP.
API
Before associating an EIP, call AllocateEipAddress to create one.
-
Call AssociateEipAddress to associate an EIP with an HaVip.
-
Call UnassociateEipAddress to disassociate an EIP from an HaVip.
Terraform
Resource: alicloud_eip_address, alicloud_eip_association
# Specify the region of the HaVip.
provider "alicloud" {
region = "cn-hangzhou"
}
# Specify the ID of the HaVip.
variable "havip_id" {
default = "havip-8vb0******" # Replace with the actual ID of your HaVip.
}
# Create an EIP.
resource "alicloud_eip_address" "test_eip" {
address_name = "test_eip_name"
isp = "BGP"
netmode = "public"
bandwidth = "1"
payment_type = "PayAsYouGo"
}
# Associate the EIP.
resource "alicloud_eip_association" "test_eip_havip_association" {
allocation_id = alicloud_eip_address.test_eip.id
instance_type = "HAVIP"
instance_id = var.havip_id # Specify the ID of the HaVip.
}
More information
Billing
HaVip is in public preview and free of charge. No SLA is provided during the public preview.
Associated resources such as ECS instances and EIPs are billed according to their respective pricing rules.
Supported regions
Supported public cloud regions
Area | Regions |
Asia Pacific - China | China (Hangzhou), China (Shanghai), China (Nanjing - Local Region, decommissioning), China (Qingdao), China (Beijing), China (Zhangjiakou), China (Hohhot), China (Ulanqab), China (Shenzhen), China (Heyuan), China (Guangzhou), China (Chengdu), China (Zhongwei), China (Hong Kong), China (Wuhan - Local Region), and China (Fuzhou - Local Region, decommissioning) |
Asia Pacific - Others | Japan (Tokyo), South Korea (Seoul), Singapore, Malaysia (Kuala Lumpur), Indonesia (Jakarta), Philippines (Manila), Thailand (Bangkok), and Malaysia (Johor) |
Europe & Americas | Germany (Frankfurt), UK (London), France (Paris), US (Silicon Valley), US (Virginia), and Mexico |
Middle East | UAE (Dubai) and Saudi Arabia (Riyadh, partner region) |
Supported Finance Cloud regions
Area | Regions |
Asia Pacific | China (Shenzhen) Finance, China (Shanghai) Finance, and China (Beijing) Finance (invitation-only beta) |
Supported Gov Cloud regions
Area | Regions |
Asia Pacific | China (Beijing) Government Cloud 1 |
Quotas
HaVip is in public preview. To enable it, log on to the Quota Center console and submit an application.
Quota name | Description | Default limit | Adjustable |
None | The network type that supports high-availability virtual IP addresses (HaVips). | VPC | No. |
HaVips per ECS instance. | 5 | ||
EIPs per HaVip. | 1 | ||
ECS instances or ENIs per HaVip. | 10 1. An HaVip can be associated with 10 ECS instances or 10 ENIs at the same time. However, an HaVip cannot be associated with ECS instances and ENIs at the same time. 2. An HaVip has the subnet property. It can be associated only with ECS instances or ENIs that are in the same vSwitch. | ||
Whether HaVips support broadcast and multicast. | No HaVips support only unicast communication. If you use third-party software such as Keepalived to implement high availability, you must change the communication mode to unicast in the configuration file. | ||
HaVips per account. | 50 | ||
HaVips per VPC. | 50 | ||
vpc_quota_havip_custom_route_entry | Entries whose destination is an HaVip in a route table. | 5 | Yes. Go to the Quota Management page or Quota Center to request a quota increase. |