keepalived实现haproxy高可用模型
keepalived节点1
----------------------------------------------------------------
vrrp_script chk_haproxy {
script "killall -0 haproxy"
interval 1
weight -2
}
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 12345
}
virtual_ipaddress {
172.16.58.100
}
track_script {
chk_haproxy
chk_mantaince_down
}
notify_master "/etc/keepalived/notify.sh master ‘172.16.58.100‘"
notify_backup "/etc/keepalived/notify.sh backup ‘172.16.58.100‘"
notify_fault "/etc/keepalived/notify.sh fault ‘172.16.58.100‘"
}
vrrp_instance VI_2 {
state BACKUP
interface eth1
virtual_router_id55
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 54321
}
virtual_ipaddress {
172.16.58.200
}
track_script {
chk_haproxy
}
notify_master "/etc/keepalived/notify.sh master ‘172.16.58.200‘"
notify_backup "/etc/keepalived/notify.sh backup ‘172.16.58.200‘"
notify_fault "/etc/keepalived/notify.sh fault ‘172.16.58.200‘"
}
----------------------------------------------------------------
keepalived节点2
----------------------------------------------------------------
vrrp_script chk_haproxy {
script "killall -0 haproxy"
interval 1
weight -2
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 12345
}
virtual_ipaddress {
172.16.58.100
}
track_script {
chk_haproxy
chk_mantaince_down
}
notify_master "/etc/keepalived/notify.sh master ‘172.16.58.100‘"
notify_backup "/etc/keepalived/notify.sh backup ‘172.16.58.100‘"
notify_fault "/etc/keepalived/notify.sh fault ‘172.16.58.100‘"
}
vrrp_instance VI_2 {
state MASTER
interface eth1
virtual_router_id55
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 54321
}
virtual_ipaddress {
172.16.58.200
}
track_script {
chk_haproxy
}
notify_master "/etc/keepalived/notify.sh master ‘172.16.58.200‘"
notify_backup "/etc/keepalived/notify.sh backup ‘172.16.58.200‘"
notify_fault "/etc/keepalived/notify.sh fault ‘172.16.58.200‘"
}
----------------------------------------------------------------------------------
通知脚本,每个节点一份都一样。其实就是利用keepalived状态转换之间触发的通知脚本来控***务的启动
----------------------------------------------------------------------------------
#!/bin/bash
#
vip=$2
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
killall -0 haproxy &> /dev/null || /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
----------------------------------------------------------------------------------
haproxy动静分离配置文件,两个节点提供服务一样,配置文件当然也一样。
----------------------------------------------------------------------------------
global
# 定义日志,传递给系统rsyslog
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
# 单进程能够启动的最大连接数
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#==============================
# 默认配置,下面前端和后端没定义的将集成这里的定义
#==============================
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
#==============================
# 前端开启80端口,acl对访问7层信息进行过滤
#==============================
frontend web
bind *:80
acl status path_beg -i /haproxy
acl url_static path_end -i .html .htm .jpg .gif .png .css .js .ico .xml
redirect location http://172.16.58.2:8009/admin?stats if status
use_backend static if url_static
default_backend dynamic
#==============================
# 静态服务器,纯粹负载均衡
#==============================
backend static
balance roundrobin
server node2 172.16.58.3:80 check
server node2 172.16.58.4:80 check
#==============================
# 后端动态服务器,一致性hash算法的source,实现session保持
#==============================
backend dynamic
hash-type consistent
balance source
server node1 172.16.58.5:80 check
server node2 172.16.58.6:80 check
#==============================
# 单独绑定状态输出页面
#==============================
listen status
bind *:8009
stats hide-version
stats enable
stats auth sun:google
stats admin if TRUE
stats uri /admin?stats
----------------------------------------------------------------------------------
测试静态页面轮询VIP1
测试静态页面轮询VIP2
状态页面
=======================================================================
#!/bin/bash
inotifywait -mrq -e modify,delete,create,attrib /www/docs/node1/ | while read files;do
rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.passwd /www/docs/node1/ [email protected]::web
done
=======================================================================
基于keepalived实现haproxy高可用