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: [email protected]
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: [email protected]
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 {
[Email protected]
}
Notification_email_from [email protected]
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 {
[Email protected]
}
Notification_email_from [email protected]
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)
This article from the "Chengdu @ A like" blog, please be sure to keep this source http://azhuang.blog.51cto.com/9176790/1440168
Haproxy + keepalived high-availability server Load balancer Configuration