haproxy+keepalived high availability for load balancing
First, the environment
5 virtual machines, respectively:
1 sets of test machines (192.168.2.83);
2 sets of haproxy/keepalived (192.168.2.230/192.168.2.231);
2 Web Servers (192.168.2.232/192.168.2.233);
Note: The VIP setting is 192.168.2.228;
Second, install the configuration Web Server
Because the installation and configuration of Web server is very simple, according to their own preferences, install one, such as: Apache, Nginx, Tomcat and so on. No further details are to be described here;
Three, installation configuration Haproxy
Yum-y install gcc kernel-devel openssl-devel lrzsz vim wget
wget http://www.haproxy.org/download/1.5/src/haproxy-1.5.12.tar.gz
Tar xzvf haproxy-1.5.12.tar.gz
CD haproxy-1.5.12
Make Target=linux26 Prefix=/usr/local/haproxy
Make install Prefix=/usr/local/haproxy
Mkdir-p/usr/local/haproxy/{conf,run,log}
CP./examples/haproxy.cfg/usr/local/haproxy/conf/
cd/usr/local/haproxy/conf/
>haproxy.cfg
Cat <<eof >>/usr/local/haproxy/conf/haproxy.cfg
Global
Log 127.0.0.1 local3
maxconn 4096
chroot/usr/local/haproxy
UID 501
GID 501
nbproc 2
pidfile/usr/local/haproxy/run/haproxy.pid
Daemon
Defaults
log Global
mode http
option Httplog
option dontlognull
Retries 2
option redispatch
option forwardfor
option Httpclose
maxconn
Balance Roundrobin
Timeout connect 5s
Timeout Client 50000ms
Timeout Server 50000ms
Frontend http
Bind *:80
Mode http
Option Httplog
Default_backend Http_pool
Backend Http_pool
Option Httpchk head/index.html http/1.0
Balance Source
Server Html_1 192.168.2.232:80 Cookie 1 weight 2 check Inter rise 2 Fall 3
Server html_2 192.168.2.233:80 Cookie 2 weight 2 check Inter rise 2 Fall 3
Listen stats
Bind *:8888
Mode http
Bind-process 1
Stats Uri/haproxy-stats
Stats Realm Haproxy \ Statistic
Stats Auth Admin:admin
Eof
Set the Haproxy server startup script (/etc/init.d/haproxy) with the following content:
#! /bin/sh set -e path=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/haproxy/sbin progdir=/usr/local/haproxy progname=haproxy daemon= $PROGDIR/sbin/$PROGNAME config= $PROGDIR/conf/$PROGNAME. cfgpidfile= $PROGDIR/run/$PROGNAME. pid desc= "HAProxy daemon " scriptname=/etc/init.d/$PROGNAME # Gracefully exit if the package has been removed. test -x $DAEMON || exit 0 start () { echo -n "starting $DESC: $PROGNAME" $DAEMON -f $CONFIG echo "." } stop () { echo -n "stopping $DESC: $PROGNAME" #haproxy_pid = ' cat $PIDFILE ' ' Pkill -9 haproxy ' echo "." } restart () { echo -n "restarting $DESC: $PROGNAME" $DAEMON -f $CONFIG -p $PIDFILE -sf $ (cat $ Pidfile) echo "." } case "$" in start) start ;; stop) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; stop ;; restart) restart ;; *) echo "usage: $ Scriptname {start|stop|restart} " >&2 exit 1 ;; esac exit 0
chmod +x/etc/init.d/haproxy
Note: The above operation, both are required to operate, so our HA is installed and configured.
Iv. Installation Configuration keepalived
wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz
Tar xzvf keepalived-1.2.15.tar.gz
CD keepalived-1.2.15
./configure--sysconf=/etc/--with-kernel-dir=/usr/src/kernels/2.6.32-504.23.4.el6.x86_64/
#内核参数, depending on the operating system, will be different, notice changes to the kernel path on your server;
Make
Make install
Ln-s/usr/local/sbin/keepalived/sbin/
cd/etc/keepalived/
CP keepalived.conf Keepalived.conf.bak
Edit the keepalive.conf master configuration file, as follows:
! Configuration File for Keepalived
Global_defs {
router_id Lvs_devel
}
Vrrp_script chk_haproxy {
script/etc/keepalived/check_ haproxy.sh "
interval 2
Weight 2
}
Vrrp_instance Vi_1 {
State Master/backup #前主后备
Interface eth0
VIRTUAL_ROUTER_ID 66
Priority 100/80 #前主后备
Advert_int 3
Authentication {
Auth_type PASS
Auth_pass 333333
}
virtual_ipaddress {
192.168.2.228
}
Track_script {
Chk_haproxy
}
}
Because the main configuration file is useful to a script that detects the Haproxy state, another script needs to be created, as follows:
# Cat Check_haproxy.sh
#!/bin/bashhaproxy= ' ps-c haproxy--no-header|wc-l ' If [$haproxy-eq 0]; Then/etc/init.d/haproxy Start >/dev/nullsleep 5hastatus= ' ps-c haproxy--no-header|wc-l ' If [$hastatus-eq 0]; then/etc/init.d/keepalived Stop >/dev/nullfifi
chmod +x/etc/keepalived/check_haproxy.sh
Note: Similarly, all of the above operations, need two servers on the same operation, pay attention to change the main standby and priority, to this, the relevant installation and configuration of keepalive is OK;
V. Testing and Calibration
Start Keepalived (two units):/etc/init.d/keepalived start
The Haproxy or keepalived service is turned off on the primary server to observe the availability of the service;
If floating to the standby server, the same, on the standby server on the Haproxy or keepalived services to shut down, and then observe;
Finally, it should be noted that if the server has security software enabled, such as Ipatalbes, it is necessary to let the server access to each other, not only ping pass, such as: Iptables-i input-s 192.168.2.0/24-j ACCEPT
This article is from the "Square Hill" blog, please be sure to keep this source http://63638790.blog.51cto.com/513514/1663159
haproxy+keepalived high availability for load balancing