keepalived结合lvs

ip划分:

RS1:192.168.223.135

RS2:192.168.223.137

节点node1和node2的keepalived(node1:192.168.223.136,node2:192.168.223.128
 
1、首先设置RS
[root@wadeson ~]# cat set_arp_args.sh
#!/bin/bash
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 lo:0 192.168.223.100/32 broadcast 192.168.223.100 up
route add -host 192.168.223.100 dev lo:0
;;
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
;;
esac
 
2、设置节点node1和node2的keepalived(node1:192.168.223.136,node2:192.168.223.128)
node1设置:
[root@node1 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
json_hc@163.com
}
notification_email_from json_hc@163.com
smtp_server smtp.163.com
smtp_connect_timeout 30
router_id node1
}
vrrp_script chk_keepalived_down {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1
weight -2
}
 
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass password
}
virtual_ipaddress {
192.168.223.100/24 dev eth0 label eth0:0
}
 
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
 
track_script {
chk_keepalived_down
}
}
virtual_server 192.168.223.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
#persistence_timeout 50
protocol TCP
 
real_server 192.168.223.135 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.223.137 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
 
node2设置:
[root@node2 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
json_hc@163.com
}
notification_email_from json_hc@163.com
smtp_server smtp.163.com
smtp_connect_timeout 30
router_id node2
}
vrrp_script chk_keepalived_down {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1
weight -2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass password
}
virtual_ipaddress {
192.168.223.100/24 dev eth0 label eth0:0
}
 
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
 
track_script {
chk_keepalived_down
}
}
virtual_server 192.168.223.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
#persistence_timeout 50
protocol TCP
 
real_server 192.168.223.135 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.223.137 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
 
然后测试网页:由于现在vip在node1上面,需要在node2上面访问
[root@node2 ~]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node2 ~]# curl http://192.168.223.100
<h1>test 192.168.223.137 html</h1>
[root@node2 ~]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node2 ~]# curl http://192.168.223.100
<h1>test 192.168.223.137 html</h1>
[root@node2 ~]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node2 ~]# curl http://192.168.223.100
<h1>test 192.168.223.137 html</h1>
 
现在在node1上面创建down,使vip漂移到node2节点上:
[root@node1 keepalived]# touch down
观察日志信息可以看见vip已经飘到node2节点上了,而且我的163邮件已经收到了邮件
现在测试curl http://192.168.223.100只能在node1节点上测试
[root@node1 ~]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node1 ~]# curl http://192.168.223.100
<h1>test 192.168.223.137 html</h1>
[root@node1 ~]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node1 ~]# curl http://192.168.223.100
<h1>test 192.168.223.137 html</h1>
 
notify.sh脚本通知到邮箱:
[root@node1 keepalived]# cat notify.sh
#!/bin/bash
send_mail="json_hc@163.com"
HOSTNAME=$(hostname)
notify() {
mail_subject="$HOSTNAME to be $1:vip floating"
mail_body="$(date +'%Y-%m-%d %H:%M:%S'):vrrp transaction change to be $1"
echo ${mail_body}|mail -s "${mail_subject}" ${send_mail}
}
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
 
现在将RS其中的某一台宕掉:
[root@wadeson script]# /usr/local/apache2.4/bin/apachectl stop
测试:
[root@node1 keepalived]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node1 keepalived]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node1 keepalived]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node1 keepalived]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node1 keepalived]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
 
然后将该宕掉的RS恢复:可能需要稍等一下
[root@node1 keepalived]# curl http://192.168.223.100
<h1>test 192.168.223.137 html</h1>
[root@node1 keepalived]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node1 keepalived]# curl http://192.168.223.100
<h1>test 192.168.223.137 html</h1>
[root@node1 keepalived]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node1 keepalived]# curl http://192.168.223.100
<h1>test 192.168.223.137 html</h1>
 
当然如果后端的RS都宕机了那么可以设置keepalived自身具有web服务提供一个友好的页面提示:
配置操作如下:
virtual_server 192.168.223.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
#persistence_timeout 50
protocol TCP
只需在这后面加上:
sorry_server 127.0.0.1 80(两台keepalived节点都给加上)
那么RS都宕机后,本机会提供一个友好的web页面
 
除了HTTP_CHECK还可以TCP_CHECK:
TCP_CHECK {
    connect_timeout 3
}
posted on 2017-07-28 15:37  wadeson  阅读(335)  评论(0编辑  收藏  举报