Build a layer-4 load balancer based on Keepalived + Haproxy

Source: Internet
Author: User
Tags haproxy
I. Preface & nbsp; Haproxy is a stable, high-performance, and high-availability load balancing solution, it 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 how to use its layer-4 switching and Keepali

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
 

Related Article

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: info-contact@alibabacloud.com 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.