Skip to content

Instantly share code, notes, and snippets.

@kenshinx
Last active January 3, 2016 08:09
Show Gist options
  • Save kenshinx/8433935 to your computer and use it in GitHub Desktop.
Save kenshinx/8433935 to your computer and use it in GitHub Desktop.
Keepalived + Haproxy VIP: 123.125.22.90 Master: 123.125.22.97 Backup: 123.125.22.98 Server1: 123.125.22.99 Server2: 123.125.22.100
# author: weizhifeng
status=$(ps aux|grep haproxy | grep -v grep | grep -v bash | wc -l)
if [ "${status}" = "0" ]; then
/etc/init.d/haproxy start
status2=$(ps aux|grep haproxy | grep -v grep | grep -v bash |wc -l)
if [ "${status2}" = "0" ]; then
/etc/init.d/keepalived stop
fi
fi
~
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
log 127.0.0.1 local3 debug
#log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 65535
#chroot /usr/share/haproxy
user haproxy
group haproxy
daemon
nbproc 1
pidfile /var/run/haproxy.pid
#debug
#quiet
defaults
log global
mode http
option httplog
option dontlognull
retries 3
redispatch
stats enable
stats uri /haproxy-status
stats refresh 5s
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen swaf-loadbalance 0.0.0.0:80
cookie SERVERID rewrite
balance roundrobin
server swaf_99 123.125.22.99:80 cookie app1inst1 check inter 2000 rise 2 fall 5
server sawf_100 123.125.22.100:80 cookie app1inst2 check inter 2000 rise 2 fall 5
! Configuration File for keepalived
global_defs {
notification_email {
xxx@sina.com.cn
}
notification_email_from sec@sina.com.cn
smtp_server mail.sina.com.cn
smtp_connect_timeout 30
router_id swaf_haproxy_2
}
vrrp_script chk_haproxy {
script "/usr/local/keepalived/chk_haproxy.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 21
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass sinasec
}
track_script {
chk_haproxy
}
virtual_ipaddress {
123.125.22.90
}
}
! Configuration File for keepalived
global_defs {
notification_email {
xxx@sina.com.cn
}
notification_email_from sec@sina.com.cn
smtp_server mail.sina.com.cn
smtp_connect_timeout 30
router_id swaf_haproxy_1
}
vrrp_script chk_haproxy {
script "/usr/local/keepalived/chk_haproxy.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 21
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass sinasec
}
track_script {
chk_haproxy
}
virtual_ipaddress {
123.125.22.90
}
}
@kenshinx
Copy link
Author

VIP: 123.125.22.90
Master: 123.125.22.97
Backup: 123.125.22.98
Server1: 123.125.22.99
Server2: 123.125.22.100

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment