With increasing Web-based apps and services, IT admins bear more responsibility.to not can to , or or , how to , Web all to can . in devops and (CD) can can to Web service can and can .
not can , not can is . is we these ? a Solutioncan this Issues. Introductionhow to use HAProxy configuration HTTP .

is HTTP Load Balancing?
HTTP is a network Solution, will HTTP or HTTPs provide Web server . via will in server perform , can server , can can and . can let can via or server perform or , for perform .
, need use Load Balancing?
can server use can and can , server can use Load Balancing. or in a , in use is a . need has .
What is HAProxy?
HAProxy is a GNU/Linux TCP/HTTP server Load Balancing and . HAProxy is , , can 10 Gbps , in use . function status , , HTTPS/SSL support , .
this Tutorial to implement Load Balancing
in this , we HTTP Web server configuration a HAProxy Load Balancing.
Prerequisites
to has , or is Web server Load Balancing function . we HTTP Web server already configuration can Run.
in Linux HAProxy
for , we can use manage HAProxy.
in Debian HAProxy
in Debian Wheezy we need , in /etc/apt/sources.list.d a file "backports.list" ,
- deb http://cdn.debian.net/debian wheezybackports main
data , HAProxy
- # apt get update
- # apt get install haproxy
in Ubuntu HAProxy
- # apt get install haproxy
in CentOS and RHEL HAProxy
- # yum install haproxy
Configure HAProxy
has HTTP Web server , IP Addressis 192.168.100.2 and 192.168.100.3. we will configuration in 192.168.100.4 server .
let HAProxy , need /etc/haproxy/haproxy.cfg some . we in these . some configuration can can GNU/Linux not , these .
1. configuration function
to is HAProxy configuration function , in will has . configuration can in /etc/haproxy/haproxy.cfg global to . is for not Linux HAProxy configuration .
CentOS or RHEL:
in CentOS/RHEL, will :
- log 127.0.0.1 local2
:
- log 127.0.0.1 local0
then Configure HAProxy in /var/log , we need rsyslog configuration . and , we in /etc/rsyslog.d a haproxy.conf file , :
- $ModLoad imudp
- $UDPServerRun 514
- $template Haproxy,"%msg%/n"
- local0.=info /var/log/haproxy.log;Haproxy
- local0.notice /var/log/haproxystatus.log;Haproxy
- local0.* ~
this configuration $template in /var/log HAProxy Log. in Restart rsyslog these .
- # service rsyslog restart
Debian or Ubuntu:
in Debian or Ubuntu , will
- log /dev/log local0
- log /dev/log local1 notice
:
- log 127.0.0.1 local0
then HAProxy configuration , Editor /etc/rsyslog.d/ haproxy.conf (in Debian can can 49-haproxy.conf) ,
- $ModLoad imudp
- $UDPServerRun 514
- $template Haproxy,"%msg%/n"
- local0.=info /var/log/haproxy.log;Haproxy
- local0.notice /var/log/haproxystatus.log;Haproxy
- local0.* ~
this configuration $template in /var/log HAProxy Log. in Restart rsyslog these .
- # service rsyslog restart
2. setting
is setting HAProxy . in /etc/haproxy/haproxy.cfg default , configuration :
- defaults
- log global
- mode http
- option httplog
- option dontlognull
- retries 3
- option redispatch
- maxconn 20000
- contimeout 5000
- clitimeout 50000
- srvtimeout 50000
configuration is HAProxy HTTP use , is not is Solution. can HAProxy configuration .
3. Web configuration
Web configuration can HTTP server . we setting all in . in we some configuration , we node . will configuration file from frontend :
- listen webfarm *:80
- mode http
- stats enable
- stats uri /haproxy?stats
- stats realm Haproxy/ Statistics
- stats auth haproxy:stats
- balance roundrobin
- cookie LBN insert indirect nocache
- option httpclose
- option forwardfor
- server web01 192.168.100.2:80 cookie node1 check
- server web02 192.168.100.3:80 cookie node2 check
"listen webfarm *:80" Address and . need , setting "*" in all . in , setting can can not , can from internet .
- stats enable
- stats uri /haproxy?stats
- stats realm Haproxy/ Statistics
- stats auth haproxy:stats
setting , status information can via http://
is a HAProxy information

"balance roundrobin" we use . this , we use , can HTTP Load Balancing need . HAProxy provide :
- leastconn: will server
- source: for Client IP Addressperform , according to and server will server .
- uri: for URI part ( part ) perform , according to and server for perform
- url_param: according to each HTTP GET URL perform , use will server
- hdr(name): according to HTTP
perform
"cookie LBN insert indirect nocache" we cookie information , can will server node and . node cookie a . , we use is "LBN", can . node this cookie .
- server web01 192.168.100.2:80 cookie node1 check
- server web02 192.168.100.3:80 cookie node2 check
is we Web server node . server has (web01, web02) , IP Address and cookie . cookie can , use is node1, node2 … node(n)
Start HAProxy
if configuration , in Start HAProxy is .
in Centos/RHEL HAProxy
let HAProxy , use command
- # chkconfig haproxy on
- # service haproxy start
, need 80 ,
CentOS/RHEL 7
- # firewallcmd permanent zone=public addport=80/tcp
- # firewallcmd reload
CentOS/RHEL 6
/etc/sysconfig/iptables ":OUTPUT ACCEPT"
- A INPUT m state state NEW m tcp p tcp dport 80 j ACCEPT
Restartiptables:
- # service iptables restart
in Debian HAProxy
Start HAProxy
- # service haproxy start
not to 80 , in /etc/iptables.up.rules :
- A INPUT p tcp dport 80 j ACCEPT
in Ubuntu HAProxy
let HAProxy in /etc/default/haproxy configuration
- ENABLED=1
Start HAProxy:
- # service haproxy start
80 :
- # ufw allow 80
Test HAProxy
HAProxy is , we can
a test.php file , file
- php
- header('Content-Type: text/plain');
- echo "Server IP: ".$_SERVER['SERVER_ADDR'];
- echo "/nX-Forwarded-for: ".$_SERVER['HTTP_X_FORWARDED_FOR'];
- ?>
this PHP file we server () , Web server .
will this PHP file to Web server Web directory . then curl command via (192.168.100.4) this file
- $ curl http://192.168.100.4/test.php
we this command , (use ) :
- Server IP: 192.168.100.2
- X–Forwarded–for: 192.168.100.4
- Server IP: 192.168.100.3
- X–Forwarded–for: 192.168.100.4
if we Web service , curl command , can Web server .
Summary
in has a can , for Web node perform Load Balancing. can configuration . this can Help Web has can .
can can already , this setting . we has Issues. in , 2 or 3 , not is .
if has Issues or , in Comment, .
via: http://xmodulo.com/haproxy-http-load-balancer-linux.html
Author: Jaroslav Štěpánek Translator: Liao for : wxy