Load Balancing with haproxy

Source: Internet
Author: User
Tags custom name haproxy

1. Installation
Tar zxvf haproxy-1.4.22.tar.gz
CD haproxy-1.4.22
Make target = linux26 prefix =/usr/local/haproxy install (uppercase)
Note: linux26 is the version of the Linux kernel. You can use uname-R to view the version 2.6 and write it to linux26.


Configuration:

CD/usr/local/haproxy
VI haproxy. conf --> Create a configuration file. The following is a configuration file template modified on this basis:


Global
Log 127.0.0.1 local0
Maxconn 4096 # default maximum number of connections
Chroot/usr/local/haproxy # chroot running path
UID 501 # uid of the running user
GID 501 # ID of the running user group
Daemon # Run haproxy later
Nbproc 1 # Number of processes (multiple processes can be set to improve performance)
Pidfile/usr/local/haproxy/logs/haproxy. pid # Storage path of the haproxy PID. the user who starts the process must have the permission to access some files.
Debug
# Ulimit-N 65535 # ulimit data restrictions

Ults
Log 127.0.0.1 local3
Mode HTTP # type of processing (Layer 7 HTTP; Layer 4: TCP)
Option httplog # Log Type HTTP log format
Option httpclose # proactively disable the HTTP channel after each request is completed
Option dontlognull # do not record Health Check log information
Option forwardfor # If the backend server needs to obtain the parameters that need to be configured for the real IP address of the client, the Client IP address can be obtained from the HTTP header.
Option redispatch # After the server corresponding to the serverid fails, it is forcibly redirected to another healthy server.
Retries 2 # If the connection fails twice, the service is considered unavailable.
Maxconn 65535 # maximum number of connections
Balance roundrobin # Default Server Load balancer mode and polling Mode
# Balance source # default load balancing method, similar to nginx ip_hash
# Balance leastconn # default Load Balancing mode, minimum connection

Stats uri/haproxy-Stats # monitoring page url
# Stats refresh 30 # interval for refreshing statistics pages
Contimeout 5000 # connection timeout
Clitimeout 50000 # client timeout
Srvtimeout 50000 # server timeout
# Timeout Check 2000 # Heartbeat detection timeout

-------------------------------------------------------------------------------
#################### Monitoring page settings ################ #######
Listen admin_status # combination of frontend and backend, monitoring group name, custom name as needed
Bind 0.0.0.0: 65532 # listening port
Mode HTTP # Layer-7 http mode
Log 127.0.0.1 local3 err # error log record
Stats refresh 5S # automatically refresh monitoring page every 5 seconds
Stats uri/admin? Stats # monitoring page url
Stats realm itnihao \ itnihao # prompt information on the monitoring page
Stats auth admin: Admin # the user and password admin on the monitoring page. You can set multiple user names.
Stats auth admin1: admin1 # user and password on the monitoring page admin1
Stats hide-version # Hide haproxy version information on the statistics page
Stats admin if true # manually enable/disable back-end servers (haproxy-1.4.9 later)


Errorfile 403/etc/haproxy/errorfiles/403. HTTP
Errorfile 500/etc/haproxy/errorfiles/500. HTTP
Errorfile 502/etc/haproxy/errorfiles/502. HTTP
Errorfile 503/etc/haproxy/errorfiles/503. HTTP
Errorfile 504/etc/haproxy/errorfiles/504. HTTP

################# Haproxy log record content settings ################# ##
Capture Request Header host Len 40
Capture Request Header Content-Length Len 10
Capture Request Header Referer Len 200
Capture Response Header server Len 40
Capture Response Header Content-Length Len 10
Capture Response Header cache-control Len 8


--------------------------------------------------------------------------------

 

########## Do not change the preceding parameters. Modify the following parameters: --->
# Listen name (anything can be called) * (bound IP address): Port Number (external)
# Database
Listen mysql_proxy *: 3307
Mode TCP # If the website is written as HTTP
# Server Load balancer policy:
# Minimum link: leastconn
# Round Robin: roundrobin each server is round-robin by weight
# IP-Hash: the same IP address of source accesses the same server with the initial access
# The default is the polling mode.
Balance leastconn
# Server name (anything can be called) IP host: port weight (numeric value)
Server db1 192.168.2.104: 3306 weight 1
Server DB2 192.168.2.105: 3306 weight 1

# Web Server
Listen HTTP *: 80
Server web1 192.168.2.10: 80 weight 1
Server web1 192.168.2.11: 80 weight 1

# Listen web_proxy *: 80
# Server web1 192.168.2.104: 80 check inter 2000 rise 2 fall 5
# Server web2 192.168.2.105: 80 check inter 2000 rise 2 fall 5

#######################################
# Cookie serverid # server ID can be inserted into the cookie. The serverid can be defined later.
# Option httpchk get/index.html # Heartbeat detection File
# Server web1 192.168.16.2: 80 cookie web1 check inter 1500 rise 3 fall 3 weight 1
# Server definition. Cookie 1 indicates that serverid is web1 and check inter 1500 indicates the heartbeat detection frequency. Rise 3 indicates that the server is correctly considered available three times,
# Fall 3 indicates that the server is unavailable when it fails three times, and weight indicates the weight.
#######################################

Start:
Ulimit-shn 65535
/Usr/local/haproxy/sbin/haproxy-F/usr/local/haproxy. conf &

Close:
Pkill-9 haproxy

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.