图为Keepalived工作的基本模型图:
将两个Director做成高可用集群,用户平时访问的是其中一台的服务,当其中一台服务器宕机的时候,keepalived可以将VIP地址和ipvsadm规则转移到另一台上,让用户的访问可以继续进行。
说起keepalived不得不说一下VRRP协议,VRRP是一种路由容错协议,也可以叫做备份路由协议。一个局域网络内的所有都设置缺省路由,当网内主机发出的目的地址不在本网段时,报文将被通过缺省路由发往外部路由器,从而实现了主机与外部网络的通信。当缺省路由器down掉(即端口关闭)之后,内部主机将无法与外部通信,如果路由器设置了VRRP时,那么这时,虚拟路由将启用备份路由器,从而实现全网通信。
Keepalived就是通过VRRP协议来实现VIP地址转移和ipvsadm规则转移的。
配置过程:
本次keepalived配置在DR模型上,所需环境要有四台机器。
配置环境:VMware workstation 9.0 RHEL5.8
RS1:172.16.125.3
RS2:172.16.125.4
Director
VIP:172.16.125.10
Master DIP:172.16.125.1
Backup DIP:172.16.125.5
Realserver需要配置网卡别名作为VIP来发出响应报文,需要将网卡别名加入到route中。
# ifconfig eth0:0 172.16.125.1 netmask 255.255.255.255 broadcast 172.16.125.1
# route add -host 172.16.125.1 dev eth0:0
想要实现集群必须要先实现双机互信
Director1上执行下列命令:
# hostname node1.earl.com# vim /etc/sysconfig/networkHOSTNAME=node1.earl.com# vim /etc/hosts IP地址 主机名 172.16.125.1 node1.earl.com node1 172.16.125.5 node2.earl.com node2# ssh-keygen -t rsa -f /root/.ssh/id_rsa -P ''# ssh-copy-id -i .ssh/id_rsa.pub root@node2.earl.comDirector2上执行下列命令:# hostname node2.earl.com# vim /etc/sysconfig/networkHOSTNAME=node2.earl.com# vim /etc/hosts IP地址 主机名 172.16.125.1 node1.earl.com node1 172.16.125.5 node2.earl.com node2# ssh-keygen -t rsa -f /root/.ssh/id_rsa -P ''# ssh-copy-id -i .ssh/id_rsa.pub root@node1.earl.com在两台Director上安装keepalived和ipvsadm,使用yum命令安装# yum -y --nogpgcheck localinstall keepalived-1.2.7-5.el5.i386.rpm# yum install ipvsadm -yDirector1:安装完成后编辑keepalived.conf文件# cd /etc/keepalived# cp keepalived.conf keepalived.conf.bak# vim keepalived.conf编辑其中内容为:! Configuration File for keepalivedglobal_defs { notification_email { root@localhost #当发生故障的时候给谁发邮件 } notification_email_from keepalived@localhost #发件人 smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL}vrrp_instance VI_1 { #定义备份组 state MASTER #定义是Master还是Backup interface eth0 virtual_router_id 51 priority 101 advert_int 1 authentication { auth_type PASS auth_pass keepalivedpass } virtual_ipaddress { #定义偏移IP地址 172.16.125.10/16 dev eth0 label eth0:0 }} virtual_server 172.16.125.10 80 { # 提供web服务IP delay_loop 6 lb_algo rr #选择rr类型的调度算法 lb_kind DR nat_mask 255.255.0.0 persistence_timeout 50 protocol TCP real_server 172.16.125.3 80 { weight 1 SSL_GET { url { path / status_code 200 } connect_timeout 3 #超时时长 nb_get_retry 3 #重试次数 delay_before_retry 3 #连接时间 } } real_server 172.16.125.4 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 } }}
保存退出后拷贝一份到Director2上。
scp keepalived.conf node2:/etc/keepalived/
在Director2上编辑文件,将其中的state MASTER改写为state BACKUP和priority 101改写为priority 100,保存退出即可。
在两台Director上启动keepalived服务。
# service keepalived start
在两个realserver上执行如下命令:
# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
# echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
# echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
这四项内容是用来修正内核参数的。
在此之前要保证两个realserver上的httpd服务已经安装并成功启动。
利用ifconfig查看当前的网卡状况可以看到:
用ipvsadm命令查看ipvs规则可以看到:
当我们模拟其中一个rs服务停掉之后可以看到:
Ipvs规则上的rs1的规则已经自动减去。
在生产环境中我们可能遇到所有的realserver全部宕机的情况,我们不可能让用户一直访问不上web界面。这种情况下我们要为用户提供一个提醒界面,提示用户当前网页服务无法打开。
在两台Director上安装httpd服务,并提供一个错误界面,当做realserver宕机时的应急界面使用。
# yum -y install httpd
# echo “The website is down for maintenance...” > /var/www/html/index.html
拷贝一份到node2上
# scp /var/www/html/index.html node2: /var/www/html/
停止两个realserver上的httpd服务,用ipvsadm命令可以看到Director的httpd服务已经启动。
访问web界面结果为:
当其中一个Director宕机的时候,我们也要定制好策略。让另外一台Director顶上去,不至于让服务断开。
在两个Director中的keepalived.conf文件中添加如下行:
vrrp_script chk_schedown {
script "[ -e /etc/keepalived/down ] && exit 1 || exit 0"#检测是否有down文件存在
interval 1# 多长时间检测一次
weight -3# 检测到后权重的处理,此处权重减去后一定要比另一个Director中的小
fall 2# 认为失败是检测几次后
rise 1# 认为成功是检测几次
}
添加这个脚本后要让它执行起来,在vrrp_instance VI_1中添加:
track_script { #执行定义的脚本
chk_schedown
}
然后重启两个Director的keepalived服务,并在主Director上的/etc/keepalived/下touch一个文件,文件名为down。然后查看日志文件可以看到eth0上的偏移地址已经偏移走了。
在另一台Director上可以看到地址已经偏移到
到这里keepalived的配置已经完成,如果想要让VIP偏移时给某个特定用户发送邮件,可以用脚本来实现,并在两个Director的配置文件中定义即可。
以下附上主Director的配置文件内容:
! Configuration File for keepalivedglobal_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL}vrrp_script chk_schedown { script "[ -e /etc/keepalived/down ] && exit 1 || exit 0" interval 1 weight -3 fall 2 rise 1}vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 73 priority 101 advert_int 1 authentication { auth_type PASS auth_pass keepalivedpass } virtual_ipaddress { 172.16.125.10/16 dev eth0 label eth0:0 } track_script { chk_schedown }}virtual_server 172.16.125.10 80 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.0.0 protocol TCP sorry_server 127.0.0.1 80 real_server 172.16.125.3 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 } } real_server 172.16.125.4 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 } }}