Keepalived + Haproxy for high-availability load balancing

Source: Internet
Author: User
Tags haproxy

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



Virtual ip:

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


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

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/"     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     #MASTER服务器IP, write ip     from server to server authentication {        auth_type pass         auth_pass 1111    }    virtual_ipaddress {     #虚拟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/"     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     #BACKUP服务器IP, the master server writes the ip     of the primary server authentication {        auth_type pass         auth_pass 1111    }    virtual_ipaddress {     #虚拟IP     }     track_script {        chk_haproxy    # Monitor Haproxy process status     }}

3) haproxy detection Script ( 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/ #脚 You need to assign permissions after this writing is completed.

4) keepalived Start-Stop

Service keepalived start #停止service keepalived stop #启动service keepalived restart #重启

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 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 brd scope global eth0   &NBSP;&NBSP;INET&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  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 brd 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 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 brd scope global eth0   &NBSP;&NBSP;INET&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 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 brd scope global eth0     inet 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

Keepalived + Haproxy for high-availability load balancing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.