1. 测试资源准备
VPC环境内
a) ECS HA1: 172.16.0.224
b) ECS HA2: 172.16.0.225
c) EIP: 139.196.75.13
d) HAVIP: 172.16.0.222

2.Nginx安装
分别在两台ECS中安装Nginx,安装过程不再赘述。

更改相应的nginx index.html,以便测试时方便识别主机。

3.keepalived安装
3.1安装依赖包
yum -y install gcc gcc+ gcc-c++
yum install popt-devel openssl openssl-devel libssl-dev libnl-devel popt-devel
3.2 建立kernel的软连接
ln -s /usr/src/kernels/2.6.32-431.23.3.el6.x86_64/ /usr/src/linux
3.3 安装配置keepalived
yum install keepalived
配置keepalived.conf
Keepalived会定期检查服务器的健康情况,并通过VRRP协议互通监控情况(默认为组播方式)。一旦任何一个服务出现故障,Keepalived会广播ARP包告诉大家,这台故障的服务器不再拥有这个虚拟IP,而是另一台健康的资源服务器才是这个虚拟IP的真正归属。这样所有发送给故障服务的请求就发送给另外一台资源服务器了。
但在阿里云VPC环境中,目前网络环境并不支持VRRP的组播方式,需要将keepalived的通信方式改为单播。
VRRP组播可能导致脑裂,争抢IP,频繁切换,无法连接等情况。典型的错误如下:

正确编辑配置文件,需要注意以下几点:
1) 两台机器的vrrp_instance State应根据主备需求,分别设置为MASTER或BACKUP。
2) virtual_ipaddress为绑定的havip,并且需要指定绑定的虚拟网卡(如eth0:1)。
3) 务必使用unicast_src_ip 和unicast_peer,将vrrp通信方式改为单播,否则两台机器无法通信。
4) 注意配置文件中virtual_ipaddress与unicast_src_ip/unicast_peer的顺序,否则会影响互备切换。

同理配置ECS HA2。
完整的keepalived.conf配置文件如下所示(文件路径:/etc/keepalived/keepalived.conf):
####主服务器配置文件##########
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 120
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.0.222/16 dev eth0 label eth0:1
}
unicast_src_ip 172.16.0.224
unicast_peer {
172.16.0.225
}
}
virtual_server 172.16.0.222 80 {
delay_loop 6
lb_algo rr
lb_kind NAT
nat_mask 255.255.0.0
persistence_timeout 50
protocol TCP
real_server 172.16.0.224 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
#######备服务器配置文件#########
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 120
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.0.222/16 dev eth0 label eth0:1
}
unicast_src_ip 172.16.0.225
unicast_peer {
172.16.0.224
}
}
virtual_server 172.16.0.222 80 {
delay_loop 6
lb_algo rr
lb_kind NAT
nat_mask 255.255.0.0
persistence_timeout 50
protocol TCP
real_server 172.16.0.225 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
4. 绑定HaVIP
在管理控制台把两台ECS分别绑上HAVIP

分别在两台机器上启动nginx和keepalived
# /usr/local/nginx/nginx
#/etc/init.d/keepalived start
此时可看到,绑定资源界面主实例一侧链接线已由虚线变为实线,说明链接成功。

在管理控制台中可看到实例i-113xhadtu为主实例,i-11kf0kbv7为备实例。

在主实例上,通过ifconfig,可以看到,havip已经被绑定到eth0:1上生效。

5. 绑定EIP
在管理控制台—专有网络—高可用虚拟IP -- 绑定资源界面中,将HaVIP绑上弹性公网EIP。弹性公网与高可用虚拟IP连接线由虚线变为实线,连接成功。

6. 测试
1)访问测试,浏览器中访问EIP地址,可见nginx的欢迎界面,且欢迎界面来自HA1。

2)将i-113xhadtu实例关机

在高可用虚拟IP界面看到,原来的i-113xhadtu实例已经变为备实例,主实例切换为了i-11kf0kbv7。

同时,图形界面里的绑定的虚实关系也发生变化,实线连到了当前的新主实例i-11kf0kbv7。

3) 仍然访问EIP地址,nginx欢迎界面已切换至HA2实例。

至此,在VPC环境中,利用HaVip+keepalived配置主备双机高可用完成。
7. 小结
由于HaVIP目前还处于有限用户公测阶段,使用时的限制小结如下:
1) 目前VPC中的网络通信不支持多播和广播,只支持单播;在使用keepalived之类的第三方软件实现高可用,需要通过配置文件把通信方式改成单播;
2) 如果是使用keepalived之类的第三方软件, 需要把信条消息的源IP改成ECS的私网IP(而不要用HaVip的私网IP进行心跳检查),否则可能造成脑裂;
3) 如使用HaVip做自建SNAT网关的高可用时,SNAT实例上配置的SNAT规则中,source IP应该是havip的私网IP而不是ECS自己的私网IP。
4) 目前尚不支持利用LVS搭建负责均衡。
5) 目前每个VPC中最多只能同时存在5个havip对象;