keepalived实现haproxy高可用。
haproxy实现RS负载均衡。
说明:
1,当keepalive节点状态master时,haproxy应该是start状态,backup为restart,fault为stop状态;(注意脚本监测权限减5,这样会有数字计算优先级方面的问题,因此在backup状态也使用restart haproxy)
2,对haproxy的start,restart,stop,都将用脚本的方式实现,在keepalived配置中调用脚本;
3,我们不只对keepalive的状态进行监控,更应该对haproxy服务进行状态监控,也在keepalive配置文件内定义。(使用 killall -0 service)
killall -0 service :是对一个服务进行状态监测的,使用信号0;
示例:httpd 运行状态返回值为0,停止状态返回值为1
步骤:
1,配置node1,node2 haproxy,实现基本的负载均衡代理。
2,haproxy,start,restart,stop和通知脚本
3,双主模型keepalived配置
4,测试:访问两个vip都可以实现负载均衡,互为主备。
5,通知修改(略)
1,配置node1,node2 haproxy,实现基本的负载均衡代理。
修改配置文件 haproxy.conf
frontend main *:80
default_backend websrvs
backend websrvs
balance roundrobin
server web1 192.168.0.22:80 check maxconn 5000
server web2 192.168.0.23:80 check maxconn 3000
listen stats *:9103
#http://192.168.0.21:9103/haproxy?stats状态页查看
stats enable
scp haproxy.conf node2:/etc/haproxy/haproxy.conf
单独测试负载均衡成功
chkconfig haproxy off
2,haproxy,start,restart,stop和通知脚本
#!/bin/bash
# Author: MageEdu <[email protected]>
# description: An example of notify script
#
vip="192.168.0.59"
contact=‘[email protected]‘
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
/etc/rc.d/init.d/haproxy start
exit 0
;;
backup)
notify backup
/etc/rc.d/init.d/haproxy restart
exit 0
;;
fault)
notify fault
/etc/rc.d/init.d/haproxy stop
exit 0
;;
*)
echo ‘Usage: `basename $0` {master|backup|fault}‘
exit 1
;;
esac
3,双主模型keepalived配置
node1
[[email protected] keepalived]$ cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id K1
}
vrrp_script chk_mt_down {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" #监测down文件,有down文件则意味着是要down机的。
interval 1
weight -5
}
vrrp_script chk_haproxy {
script "killall -0 haproxy &>/dev/null" #监测haproxy状态
interval 1
weight -5
}
vrrp_instance VI_1 { #实例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.59/24 brd 192.168.0.59 dev eth0 label eth0:0
}
track_script {
chk_mt_down
chk_haproxy
}
notify_master "/etc/keepalived/notify.sh master" #添加通知信息
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
vrrp_instance VI_2 { #实例2 备用状态
state BACKUP
interface eth0
virtual_router_id 58
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
192.168.0.60/24 brd 192.168.0.60 dev eth0 label eth0:1
}
track_script {
chk_mt_down
chk_haproxy
}
notify_master "/etc/keepalived/notify.sh master" #添加通知信息
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
node2,红色为修改
[[email protected] keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id K2
}
vrrp_script chk_mt_down {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" #监测down文件,有down文件则意味着是要down机的。
interval 1
weight -5
}
vrrp_script chk_haproxy {
script "killall -0 haproxy &>/dev/null"
interval 1
weight -5
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 57
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.59/24 brd 192.168.0.59 dev eth0 label eth0:0
}
track_script {
chk_mt_down
chk_haproxy
}
notify_master "/etc/keepalived/notify.sh master" #添加通知信息
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 58
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
192.168.0.60/24 brd 192.168.0.60 dev eth0 label eth0:1
}
track_script {
chk_mt_down
chk_haproxy
}
notify_master "/etc/keepalived/notify.sh master" #添加通知信息
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
4,测试:访问两个vip都可以实现负载均衡,互为主备。
都可实现负载均衡
5,通知具体情况略作修改(略)