High-availability server Load balancer configuration for HAProxy + Keepalived in CentOS5.5
I. system environment:
System Version: CentOS5.5 x86_64
Master_ip: 172.20.27.40
Backup_ip: 172.20.27.50
Vip: 172.20.27.200
Web_1: 172.20.27.90
Web_2: 172.20.27.100
Ii. haproxy installation:
1. First install 172.20.27.40:
1.1 installation
Tar zxvf haproxy-1.3.20.tar.gz
Cd haproxy-1.3.20
Make TARGET = linux26 PREFIX =/usr/local/haproxy
Make install PREFIX =/usr/local/haproxy
1. 2. Configuration
Cd/usr/local/haproxy
Mkdir etc
Cd etc
Vi haproxy. cfg
Global
Log 127.0.0.1 local0
Maxconn 65535
Chroot/usr/local/haproxy
Uid 99
Gid 99
Daemon
Nbproc 12
Pidfile/usr/local/haproxy. pid
Ults
Log 127.0.0.1 local3
Mode http
Option httplog
Option httpclose
Option dontlognull
Option forwardfor
Option redispatch
Retries 2
Maxconn 65535
Balance source
Stats uri/web-status
Stats auth admin: admin @ admin
Stats refresh 60 s
Contimeout 5000
Clitimeout 50000
Srvtimeout 50000
Listen 172.20.27.200
Bind *: 80
Mode http
Option httplog
Log global
Option httpchk HEAD/t.htm HTTP/1.0
Server web1 172.20.27.90: 80 weight 5 check inter 2000 rise 2 fall 3
Server web2 172.20.27.100: 80 weight 5 check inter 2000 rise 2 fall 3
Start haproxy and run the following startup command:
/Usr/local/haproxy/sbin/haproxy-f/usr/local/haproxy/etc/haproxy. cfg
2. First install 172.20.27.50:
2. 1. Install
Tar zxvf haproxy-1.3.20.tar.gz
Cd haproxy-1.3.20
Make TARGET = linux26 PREFIX =/usr/local/haproxy
Make install PREFIX =/usr/local/haproxy
2. Configuration
Cd/usr/local/haproxy
Mkdir etc
Cd etc
Vi haproxy. cfg
Global
Log 127.0.0.1 local0
Maxconn 65535
Chroot/usr/local/haproxy
Uid 99
Gid 99
Daemon
Nbproc 12
Pidfile/usr/local/haproxy. pid
Ults
Log 127.0.0.1 local3
Mode http
Option httplog
Option httpclose
Option dontlognull
Option forwardfor
Option redispatch
Retries 2
Maxconn 65535
Balance source
Stats uri/web-status
Stats auth admin: admin @ admin
Stats refresh 60 s
Contimeout 5000
Clitimeout 50000
Srvtimeout 50000
Listen 172.20.27.200
Bind *: 80
Mode http
Option httplog
Log global
Option httpchk HEAD/t.htm HTTP/1.0
Server web1 172.20.27.90: 80 weight 5 check inter 2000 rise 2 fall 3
Server web2 172.20.27.100: 80 weight 5 check inter 2000 rise 2 fall 3
Start haproxy and run the following startup command:
/Usr/local/haproxy/sbin/haproxy-f/usr/local/haproxy/etc/haproxy. cfg
3. Install keepalived:
1. First install 172.20.27.40:
1.1 installation
Tar zxvf keepalived-1.1.17.tar.gz
Cd keepalived-1.1.17
./Configure
Make
Make install
Cp/usr/local/etc/rc. d/init. d/keepalived/etc/rc. d/init. d
Cp/usr/local/etc/sysconfig/keepalived/etc/sysconfig
Mkdir/etc/keepalived
Cp/usr/local/sbin/keepalived/usr/sbin/
Cd/etc/keepalived
1. 2. Configuration
Vi keepalived. conf
! Configuration File for keepalived
Global_defs {
Notification_email {
331095659@qq.com
}
Notification_email_from 331095659@qq.com
Smtp_server 127.0.0.1
Smtp_connect_timeout 30
Router_id LVS_DEVEL
}
Vrrp_script chk_haproxy {
Script "/usr/local/haproxy/shell/check_haproxy.sh"
Interval 2
Weight 2
}
# VIP1
Vrrp_instance VI_1 {
State BACKUP
Interface eth0
Lvs_sync_daemon_inteface eth0
Virtual_router_id 151
Priority100
Advert_int 5
Nopreempt
Authentication {
Auth_typePASS
Auth_pass 2222
}
Virtual_ipaddress {
172.20.27.200
}
Track_script {
Chk_haproxy
}
}
Mkdir-p/usr/local/haproxy/shell
Cd/usr/local/haproxy/shell
# Create a monitoring haproxy script
Vi check_haproxy.sh
#! /Bin/bash
# Auto check haprox process
#2014-6-30
Killall-0 haproxy
If [[$? -Ne 0]; then
/Etc/init. d/keepalived stop
Fi
Chmod u + x check_haproxy.sh
Service keepalived restart
2. First install 172.20.27.50:
2.1 installation
Tar zxvf keepalived-1.1.17.tar.gz
Cd keepalived-1.1.17
./Configure
Make
Make install
Cp/usr/local/etc/rc. d/init. d/keepalived/etc/rc. d/init. d
Cp/usr/local/etc/sysconfig/keepalived/etc/sysconfig
Mkdir/etc/keepalived
Cp/usr/local/sbin/keepalived/usr/sbin/
Cd/etc/keepalived
2.2 Configuration
Vi keepalived. conf
! Configuration File for keepalived
Global_defs {
Notification_email {
331095659@qq.com
}
Notification_email_from 331095659@qq.com
Smtp_server 127.0.0.1
Smtp_connect_timeout 30
Router_id LVS_DEVEL
}
Vrrp_script chk_haproxy {
Script "/usr/local/haproxy/shell/check_haproxy.sh"
Interval 2
Weight 2
}
# VIP1
Vrrp_instance VI_1 {
State BACKUP
Interface eth0
Lvs_sync_daemon_inteface eth0
Virtual_router_id 151
Priority 90
Advert_int 5
Nopreempt
Authentication {
Auth_typePASS
Auth_pass 2222
}
Virtual_ipaddress {
172.20.27.200
}
Track_script {
Chk_haproxy
}
}
Mkdir-p/usr/local/haproxy/shell
Cd/usr/local/haproxy/shell
# Create a monitoring haproxy script
Vi check_haproxy.sh
#! /Bin/bash
# Auto check haprox process
#2014-6-30
Killall-0 haproxy
If [[$? -Ne 0]; then
/Etc/init. d/keepalived stop
Fi
Chmod u + x check_haproxy.sh
Service keepalived restart
Iv. Test
Bind vip 172.000027.200 to your domain name for testing.
1. ping the IP address 172.20.27.200 all the time, and then run the master_ip command to simulate the server downtime. At this time, the ping will be delayed for 3 seconds and automatically switch to backup_ip. (Test successful)