Use haproxy for TCP layer load balancing in Ubuntu

Source: Internet
Author: User
Tags haproxy rsyslog

First download the haproxy package:

Wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.13.tar.gz

Installation:

Tar zxvf haproxy-1.4.13.tar.gz

Make target = linux26 prefix =/usr/local/haproxy # My system kernel is 2.6, so target = linux26

Make install prefix =/usr/local/haproxy

Then configure:

VI/etc/haproxy. cfg

# Write the following content:

########### Global configuration #########
Global
Daemon
Nbproc 1
Pidfile/var/run/haproxy. PID


######## Default configuration ############
Ults
Mode HTTP # default mode {tcp | HTTP | health}, TCP is Layer 4, HTTP is Layer 7, health only returns OK
Retries 2 # If the connection fails twice, the server is considered unavailable. You can also set
Option redispatch # When the server corresponding to the serverid fails, it is forcibly directed to other healthy servers.
Option abortonclose # When the server load is high, the link for processing the queue for a long time is automatically terminated.
Maxconn 4096 # default maximum number of connections
Timeout connect 5000 MS # connection timeout
Timeout client 30000 ms # client timeout
Timeout server 30000 ms # server timeout
# Timeout Check 2000 # = heartbeat detection timeout
Log 127.0.0.1 local0 err # [err warning info debug]


######## Statistics page configuration ########
Listen admin_stats
Bind 0.0.0.0: 1080 # listening port
Mode HTTP # Layer-7 http mode
Option httplog # Use http log format
# Log 127.0.0.1 local0 err
Maxconn 10
Stats refresh 30 s # statistics page automatic refresh time
Stats uri/stats # URL of the statistics page
Stats realm xingcloud \ haproxy # text displayed in the password box on the statistics page
Stats auth admin: Admin # user name and password settings on the statistics page
Stats hide-version # Hide the haproxy version on the statistics page


######## Test1 configuration #################
Listen test1
Bind 0.0.0.0: 90
Mode TCP
# Maxcon 4086
# Log 127.0.0.1 local0 debug
Server S1 10.18.138.201: 80
Server S2 10.18.102.190: 80
Server S3 10.18.102.189: 80
Server S4 10.18.102.188: 80
Server S5 10.18.102.187: 80
######## Frontend configuration ##############


######## Test2 configuration #################
Listen Test2
Bind 0.0.0.0: 91
Mode TCP
# Maxcon 4086
# Log 127.0.0.1 local0 debug
Server S1 10.18.138.130: 80 weight 1
Server S2 10.18.138.201: 8080 weight 6
######## Frontend configuration ##############

OK. Start haproxy.

Haproxy/etc/haproxy. cfg

Try it immediately and find that the load is successful.

However, there is a problem where you cannot see the log because I didn't start syslog.

In ubuntu, rsyslogd must be started.

My machine is not installed. Install rsyslogd first.

Apt-Get install rsyslog

After the installation is complete, configure rsyslog

VI/etc/rsyslog. d/haproxy. conf

Add the following content:

$ Modload imudp
$ Udpserverrun 514

Local0. */var/log/haproxy. Log

Save and restart rsyslogd.

Restart rsyslogd

OK. Now you can see the log.

Try it.

Tail-F/var/log/haproxy. Log

Finally, add a command for smooth restart:

Haproxy-F/etc/haproxy. cfg-SF 'cat/var/run/haproxy. Pi'

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.