I. Preface
Haproxy is a stable, high-performance, and high-availability server Load balancer solution that supports HTTP and TCP proxy backend server pools. It is widely used as an HTTP reverse proxy because it supports powerful and flexible layer-7 acl rules. This article describes in detail how to use its layer-4 switching and Keepalived to implement a Load balancer, suitable for any TCP services such as Socket, ICE, Mail, Mysql, and private communication. The system architecture diagram is as follows:
Ii. platform environment
Reference
OS: Centos5.4 (64X)
MASTER: 192.168.0.20
BACKUP: 192.168.0.21
VIP: 192.168.0.100
Serivce Port: 11231
Iii. Platform installation and configuration
1. Add non-local IP address bonding support
Reference
# Vi/etc/sysctl. conf
Net. ipv4.ip _ nonlocal_bind = 1
# Sysctl-p
2. Configure platform log support
Reference
# Vi/etc/syslog. conf
Add:
Local3. */var/log/haproxy. log
Local0. */var/log/haproxy. log
# Vi/etc/sysconfig/syslog
Modify:
SYSLOGD_OPTIONS = "-r-m 0"
#/Etc/init. d/syslog restart
3. Disable SELINUX.
Reference
Vi/etc/sysconfig/selinux
Modify:
SELINUX = disabled
# Setenforce 0
4. Configure iptables and add VRRP communication support
Reference
Iptables-a input-d 224.0.0.18-j ACCEPT
5. install and configure Keepalived
Reference
# Mkdir-p/home/install/keepalivedha
# Cd/home/install/keepalivedha
# Wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
# Tar zxvf keepalived-1.2.2.tar.gz
# Cd keepalived-1.2.2
#./Configure
# Make & make install
Reference
# 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/etc/keepalived. conf/etc/keepalived/
# Cp/usr/local/sbin/keepalived/usr/sbin/
# Vi/etc/keepalived. conf
View plaincopy to clipboardprint?
! Configuration File for keepalived
Global_defs {
Notification_email {
Liutiansi@gmail.com
}
Notification_email_from liutiansi@gmail.com
Smtp_connect_timeout 3
Smtp_server 127.0.0.1
Router_id LVS_DEVEL
}
Vrrp_script chk_haproxy {
Script "killall-0 haproxy"
Interval 2
Weight 2
}
Vrrp_instance VI_1 {
Interface eth1
State MASTER # slave is "BACKUP"
Priority 101 # from 100
Virtual_router_id 50 # route ID, which can be viewed through # tcpdump vrrp.
Garp_master_delay 1 # Master-slave switchover time, in seconds.
Authentication {
Auth_type PASS
Auth_pass KJj23576hYgu23IP
}
Track_interface {
Eth0
Eth1
}
Virtual_ipaddress {
192.168.0.100
}
Track_script {
Chk_haproxy
}
# Status notification
Notify_master "/etc/keepalived/mailpolicy. py master"
Notify_backup "/etc/keepalived/mailpolicy. py backup"
Notify_fault "/etc/keepalived/mailpolicy. py fault"
}
! Configuration File for keepalived
Global_defs {
Notification_email {
Liutiansi@gmail.com
}
Notification_email_from liutiansi@gmail.com
Smtp_connect_timeout 3
Smtp_server 127.0.0.1
Router_id LVS_DEVEL
}
Vrrp_script chk_haproxy {
Script "killall-0 haproxy"
Interval 2
Weight 2
}
Vrrp_instance VI_1 {
Interface eth1
State MASTER # slave is "BACKUP"
Priority 101 # from 100
Virtual_router_id 50 # route ID, which can be viewed through # tcpdump vrrp.
Garp_master_delay 1 # Master-slave switchover time, in seconds.
Authentication {
Auth_type PASS
Auth_pass KJj23576hYgu23IP
}
Track_interface {
Eth0
Eth1
}
Virtual_ipaddress {
192.168.0.100
}
Track_script {
Chk_haproxy
}
# Status notification
Notify_master "/etc/keepalived/mailpolicy. py master"
Notify_backup "/etc/keepalived/mailpolicy. py backup"
Notify_fault "/etc/keepalived/mailpolicy. py fault"
}
6. installation and configuration of Haproxy
Reference
# Cd/home/install/keepalivedha
# Wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.11.tar.gz
# Tar-zxvf haproxy-1.4.11.tar.gz
# Cd haproxy-1.4.11
# Make install
# Mkdir-p/usr/local/haproxy/etc
# Mkdir-p/usr/local/haproxy/sbin
# Cp examples/haproxy. cfg/usr/local/haproxy/etc
# Ln-s/usr/local/sbin/haproxy/usr/local/haproxy/sbin/haproxy
# Vi/usr/local/haproxy/etc/haproxy. cfg
View plaincopy to clipboardprint?
# This config needs haproxy-1.1.28 or haproxy-1.2.1
Global
# Log 127.0.0.1 local0
Log 127.0.0.1 local1 notice
Maxconn 5000
Uid 99
Gid 99
Daemon
Pidfile/usr/local/haproxy. pid
Ults
Log global
Mode http