Configuring HTTP Load Balancing with HAProxy

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" ,


  1. deb http://cdn.debian.net/debian wheezy­backports main

data , HAProxy


  1. # apt­ get update
  2. # apt ­get install haproxy

in Ubuntu HAProxy


  1. # apt ­get install haproxy

in CentOS and RHEL HAProxy


  1. # 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 :


  1. log 127.0.0.1 local2

:


  1. 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 , :


  1. $ModLoad imudp
  2. $UDPServerRun 514
  3. $template Haproxy,"%msg%/n"
  4. local0.=info ­/var/log/haproxy.log;Haproxy
  5. local0.notice ­/var/log/haproxy­status.log;Haproxy
  6. local0.* ~

this configuration $template in /var/log HAProxy Log. in Restart rsyslog these .


  1. # service rsyslog restart

Debian or Ubuntu:

in Debian or Ubuntu , will


  1. log /dev/log local0
  2. log /dev/log local1 notice

:


  1. log 127.0.0.1 local0

then HAProxy configuration , Editor /etc/rsyslog.d/ haproxy.conf (in Debian can can 49-haproxy.conf) ,


  1. $ModLoad imudp
  2. $UDPServerRun 514
  3. $template Haproxy,"%msg%/n"
  4. local0.=info ­/var/log/haproxy.log;Haproxy
  5. local0.notice ­/var/log/haproxy­status.log;Haproxy
  6. local0.* ~

this configuration $template in /var/log HAProxy Log. in Restart rsyslog these .


  1. # service rsyslog restart

2. setting

is setting HAProxy . in /etc/haproxy/haproxy.cfg default , configuration :


  1. defaults
  2. log global
  3. mode http
  4. option httplog
  5. option dontlognull
  6. retries 3
  7. option redispatch
  8. maxconn 20000
  9. contimeout 5000
  10. clitimeout 50000
  11. 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 :


  1. listen webfarm *:80
  2. mode http
  3. stats enable
  4. stats uri /haproxy?stats
  5. stats realm Haproxy/ Statistics
  6. stats auth haproxy:stats
  7. balance roundrobin
  8. cookie LBN insert indirect nocache
  9. option httpclose
  10. option forwardfor
  11. server web01 192.168.100.2:80 cookie node1 check
  12. 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 .


  1. stats enable
  2. stats uri /haproxy?stats
  3. stats realm Haproxy/ Statistics
  4. stats auth haproxy:stats

setting , status information can via http:///haproxy?stats . need HTTP , user "haproxy" "stats". these setting can method . if not need status information , can .

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 .


  1. server web01 192.168.100.2:80 cookie node1 check
  2. 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


  1. # chkconfig haproxy on
  2. # service haproxy start

, need 80 ,

CentOS/RHEL 7


  1. # firewall­cmd ­­permanent ­­zone=public ­­add­port=80/tcp
  2. # firewall­cmd ­­reload

CentOS/RHEL 6

/etc/sysconfig/iptables ":OUTPUT ACCEPT"


  1. ­A INPUT ­m state ­­state NEW ­m tcp ­p tcp ­­dport 80 ­j ACCEPT

Restartiptables:


  1. # service iptables restart

in Debian HAProxy

Start HAProxy


  1. # service haproxy start

not to 80 , in /etc/iptables.up.rules :


  1. ­A INPUT ­p tcp ­­dport 80 ­j ACCEPT

in Ubuntu HAProxy

let HAProxy in /etc/default/haproxy configuration


  1. ENABLED=1

Start HAProxy:


  1. # service haproxy start

80 :


  1. # ufw allow 80

Test HAProxy

HAProxy is , we can

a test.php file , file


  1. php
  2. header('Content-Type: text/plain');
  3. echo "Server IP: ".$_SERVER['SERVER_ADDR'];
  4. echo "/nX-Forwarded-for: ".$_SERVER['HTTP_X_FORWARDED_FOR'];
  5. ?>

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


  1. $ curl http://192.168.100.4/test.php

we this command , (use ) :


  1. Server IP: 192.168.100.2
  2. XForwardedfor: 192.168.100.4


  1. Server IP: 192.168.100.3
  2. XForwardedfor: 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

Leave a Comment

Your email address will not be published.