Keepalived is a two-machine hot-standby (HA) software, often and haproxy combined to do a warm standby + load balancing, to achieve high availability
1.Experimental Environment
master:10.20.2.233
backup:10.20.2.235
Virtual ip:10.20.2.238
2.keepalived Installation
1) install software dependency package via Yum
Yum install-y wget make gcc openssl-devel popt-devel
2) keepalived source package Download Installation
: http://www.keepalived.org/software/keepalived-1.2.23.tar.gz
TAR-ZXF keepalived-1.2.23.tar.gz-c/usr/srccd/sru/src/keepalived-1.2.23/./configure--prefix=/usr/local/ Keepalived/makemake Install
3) service mode after successful installation
cp/usr/local/keepalived/sbin/keepalived/usr/sbin/cp/usr/local/keepalived/etc/sysconfig/keepalived/etc/ sysconfig/cp/usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/init.d/
3.Editing a configuration file
Mkdir-p/etc/keepalivedcp/usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/
master configuration file are as follows:
! configuration file for keepalived global_defs { router_id HA_TEST} #监测haproxy进程状态, Vrrp_script chk_haproxy { script once every 2 seconds "/etc/keepalived/check_haproxy.sh" interval 2 weight 2} vrrp_instance VI_1 { state MASTER # from for backup From priority 200 # to 100 virtual_router_id 51 #路由ID and can be viewed via #tcpdump vrrp. garp_master_delay 1 #主从切换时间 units in seconds. interface eth0 advert_int 1 mcast_src_ip 10.20.2.233 #MASTER服务器IP, write ip from server to server authentication { auth_type pass auth_pass 1111 } virtual_ipaddress { 10.20.2.238 #虚拟IP } track_script { chk_haproxy # Monitor Haproxy process status }}
backup configuration file are as follows:
! configuration file for keepalived global_defs { router_id HA_TEST} #监测haproxy进程状态, Vrrp_script chk_haproxy { script once every 2 seconds "/etc/keepalived/check_haproxy.sh" interval 2 weight 2} vrrp_instance VI_1 { state BACKUP # Master as Master priority 100 # Master for 101 virtual_router_id 51 #路由ID and can be viewed via #tcpdump vrrp. garp_master_delay 1 #主从切换时间 units in seconds. interface eth0 advert_int 1 mcast_src_ip 10.20.2.235 #BACKUP服务器IP, the master server writes the ip of the primary server authentication { auth_type pass auth_pass 1111 } virtual_ipaddress { 10.20.2.238 #虚拟IP } track_script { chk_haproxy # Monitor Haproxy process status }}
3) haproxy detection Script (check_haproxy.sh) content is as follows
#!/bin/bash a= ' ps-c haproxy--no-header |wc-l ' If [$A-eq 0];then/etc/init.d/haproxyd startsleep 3 if [' Ps-c ha Proxy--no-header |wc-l '-eq 0];then/etc/init.d/keepalived stop fi fichmod 755/etc/keepalived/check_haproxy.sh #脚 You need to assign permissions after this writing is completed.
4) keepalived Start-Stop
Service keepalived start #停止service keepalived stop #启动service keepalived restart #重启
4.Test:
1) start the keepalived on master and backup
Service keepalived Start (keepalived starts at the same time as haproxy) [[email protected] keepalived]# service keepalived startstarting keepalived: [OK][[email protected] keepalived]# service Haproxyd statushaproxy (p ID 32077) is running!
Master binds the Vip,backup unbound VIP
[[Email protected] keepalived]# ip a1: lo: <loopback,up,lower_up> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever2: eth0: <broadcast ,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state up qlen 1000 link/ether 00:50:56:85:46:b2 brd ff:ff:ff:ff:ff:ff inet 10.20.2.233/24 brd 10.20.2.255 scope global eth0 &NBSP;&NBSP;INET&NBSP;10.20.2.238/32&NBSP;SCOPE&NBSP;GLOBAL&NBSP;ETH0&NBSP;&NBSP;&NBSP;&NBSP;INET6&NBSP;FE80: : 250:56ff:fe85:46b2/64 scope link valid_lft forever preferred_lft Forever[[email protected] keepalived]# ip a1: lo: <loopback,up,lower_up> mtu 65536 qdisc noqueue state unknown link/ Loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever2: eth0: < Broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state up qlen 1000 link/ether 00:50:56:85:73:ce brd ff:ff:ff:ff:ff:ff inet 10.20.2.235/24 brd 10.20.2.255 scope global eth0 inet6 fe80::250: 56ff:fe85:73ce/64 scope link valid_lft forever preferred_lft forever
2) when the haproxy,3 seconds are stopped, the keepalived will start
[[Email protected] keepalived]# service haproxyd stopstop HAProxy listenstop HAProxy process[[email protected] keepalived ]# service Haproxyd statushaproxy (PID 32368) is running!
3) Stop the keepalived of the Lord and take over the service immediately .
Backup's got a VIP on the set.
[[Email protected] keepalived]# ip a1: lo: <loopback,up,lower_up> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever2: eth0: <broadcast ,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state up qlen 1000 link/ether 00:50:56:85:73:ce brd ff:ff:ff:ff:ff:ff inet 10.20.2.235/24 brd 10.20.2.255 scope global eth0 &NBSP;&NBSP;INET&NBSP;10.20.2.238/32&NBSP;SCOPE&NBSP;GLOBAL&NBSP;ETH0&NBSP;&NBSP;&NBSP;&NBSP;INET6&NBSP;FE80: : 250:56ff:fe85:73ce/64 scope link valid_lft forever preferred_lft Forever
start the Keepalived,master binding vip,backup release VIP
[[Email protected] keepalived]# service keepalived startstarting keepalived: [ OK ][[email protected] keepalived]# ip a1: Lo: <loopback,up,lower_up> mtu 65536 qdisc noqueue state unknown link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft Forever2: eth0: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_ Fast state up&nbSp;qlen 1000 link/ether 00:50:56:85:73:ce brd ff:ff:ff:ff:ff:ff inet 10.20.2.235/24 brd 10.20.2.255 scope global eth0 inet 10.20.2.238/32 scope global eth0 inet6 fe80::250:56ff:fe85:73ce/64 scope link valid_ Lft forever preferred_lft forever
This article is from the "11528244" blog, please be sure to keep this source http://11538244.blog.51cto.com/11528244/1851606
Keepalived + Haproxy for high-availability load balancing