使用LVS DR模式进行负载均衡,keepalive做高可用。
步骤:
1,配置RS。
2,ipvsadm配置node1,node2 为DR,测试分别做为DR是成功的。
3,配置keepalived实现ipvsadm的功能(后端RS健康监测和keepalive状态通知)。
4, 测试。
实验拓扑:
1,配置RS
node3,node4,为RS
node3,node4执行脚本
vim lvs_dr_rs.sh
#!/bin/bash
#
vip="192.168.0.50"
interface="lo:0"
case $1 in
start)
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig $interface $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev $interface
;;
stop)
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig $interface down
;;
status)
if ifconfig lo:0 |grep $vip &> /dev/null; then
echo "ipvs is running."
else
echo "ipvs is stopped."
fi
;;
*)
echo "Usage: `basename $0` {start|stop|status}"
exit 1
esac
分别配置apached单独访问正常!
2,ipvsadm配置node1,node2 为DR,测试分别做为DR是成功的。(最后清楚手动规则,这里只是测试能否手动配置ipvs正常工作,实际keepalived不需要使用ipvsadm工具)
3,配置keepalived实现ipvsadm的功能。
[root@node1 keepalived]# vim keepalived.conf
[root@node1 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from root@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_mt_down {
script "[ -f /etc/keepalived/down ] && exit 1 || exit 0" #监测down文件,有down文件则意味着是要down机的。
interval 1
weight -5
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 57
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.50/24 brd 192.168.0.50 dev eth0 lable eth0:0
}
track_script {
chk_mt_down
}
notify_master "/etc/keepalived/notify.sh master" #添加通知信息
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
virtual_server 192.168.0.50 80 {
delay_loop 6 #每一次检查后端RS服务间隔时间。
lb_algo rr #负载均衡调度算法
lb_kind DR #负载均衡类型
nat_mask 255.255.255.0
# persistence_timeout 50 #是否使用持久连接,连接超时时间50,注释表示不使用持久连接。
protocol TCP
real_server 192.168.0.22 80 {
weight 1 #权重
HTTP_GET {
url {
path /index.html #url测试页
status_code 200 #状态码
}
connect_timeout 2 #连接超时时间
nb_get_retry 3 #总共重试次数为3次
delay_before_retry 1 #延时为1秒
}
}
real_server 192.168.0.23 80 {
weight 1 #权重
HTTP_GET {
url {
path /index.html #url测试页
status_code 200 #状态码
}
connect_timeout 2 #连接超时时间
nb_get_retry 3 #总共重试次数为3次
delay_before_retry 1 #延时为1秒
}
}
}
创建通知脚本文件:notify.sh
添加kaadmin用户接受邮件。
[root@node1 keepalived]# cat notify.sh
#!/bin/bash
# Author: MageEdu <[email protected]>
# description: An example of notify script
#
vip="192.168.0.59"
contact="kaadmin@localhost"
notify() {
mailsubject="`hostname` to be $1: $vip floating"
mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"
echo $mailbody | mail -s "$mailsubject" $contact
}
case "$1" in
master)
notify master
exit 0
;;
backup)
notify backup
exit 0
;;
fault)
notify fault
exit 0
;;
*)
echo 'Usage: `basename $0` {master|backup|fault}'
exit 1
;;
esac
copy keepalived.conf和notify.sh文件到node2
[root@node1 keepalived]# scp keepalived.conf notify.sh node2:/etc/keepalived/
修改:keepalived.conf
state BACKUP
priority 99
即可
查看日志:/var/log/message
[root@node1 keepalived]# watch -n1 'ipvsadm -Ln' 动态查看ipvs状况。
4,测试
使用命令
[root@node1 keepalived]# watch -n1 'ipvsadm -Ln' 动态查看ipvs状况。
1,停止RS node3 httpd服务,master DR(node1)将从ipvs列表剔除node3节点,启动服务后将又添加。
2,删除node3 /index.html文件,master DR(node1)将在ipvs列表剔除node3节点。(状态监测失败)通过:status_code 200 实现,监测的后端RS返回状态码200
3,在master DR touch /etc/keepalived/down 将触发master down机,BACK UP DR将顶替。通过:vrrp_script chk_mt_down代码段实现
注意:状态监测 不止有HTTP_GET 还有tcp等很多方式,列如后端如果是mysql RS,则可使用TCP监测,端口3306等。
4,切换到kaadmin用户查看邮件keepalive状态变更