HaProxy configuration examples


Right after the first article I’d like to describe couple of examples of the HaProxy configuration in this one. It is recommended to read the first article before proceeding with this one.

By default the configuration of the HaProxy is located in the following file:
/etc/haproxy/haproxy.cfg

The default config file has multiple examples of different sections such as backend, frontend and listen.

Also it contains the definition of the “Global” section, that contains the global options.
Everything is simple and user-friendly. Please check the spoiler below:

Example of global section

global
    log         127.0.0.1 local2 
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

Please refer to this article to configure HapRoxy logs.

Next comes the “defaults” section. This sections includes the configuration variables that are common for all listeners.

Example of defaults section

defaults
    log         global
    option      dontlognull
    option      forwardfor
    option      redispatch
    timeout connect 10000 # default 10 second time out if a backend is not found
    timeout client 300000
    timeout server 300000
    maxconn     60000
    retries     3

I’m going to start from more complicated configuration which is “Frontend-Backend”. This approach will be useful is you have multiple server’s groups which are responsible for the separate tasks. Let’s say that there are 2 websites, that reside on two pares of servers. (1st website – web01-1 and web01-2, 2nd website is on web02-1 and web02-2). In this case multiple servers can be combined in 1 backend. We are going to have 2 backends – 1 backend for each website. The frontend will have the ACLs with conditions to deliver traffic to the backend.

####
# HTTP section
####
frontend http-proxy
 bind *:80
 mode http
 acl web1 hdr_beg(host) www.website1.com
 acl web2 hdr_beg(host) www.website2.com
 use_backend http-web2 if web2
 default_backend http-web1

####
# Description of the web1 servers
####
backend http-web1
 mode http
 balance roundrobin
 option httpclose
 option forwardfor
 option httpchk OPTIONS * HTTP/1.1rnHost: www
server web01-1 192.168.10.20:80 check inter 2000 fall 3
server web01-2 192.168.10.16:80 check inter 2000 fall 3

####
# Description of the web2 servers
####
backend http-web2
 balance roundrobin
 mode http
 option httpclose
 option forwardfor
 option httpchk OPTIONS * HTTP/1.1rnHost: www
server web02-1 192.168.10.13:80 check inter 2000 fall 3
server web02-2 192.168.10.14:80 check inter 2000 fall 3

I’ll use the load balancing of the https traffic in tcp mode to describe the simple listen section. According to the previous example you can describe multiple servers with “frontend-backend” approach.

Running in tcp mode, Haproxy delivers all incoming traffic to the backend servers without modifications.

####
# HTTPs section
####
listen https-proxy *:443
 mode tcp
 balance source
 option httpclose
 option forwardfor
server web01 192.168.10.20:443 check port 443
server web02 192.168.10.16:443 check port 443

You can configure HaProxy to load balance all MySQL connections in tcp mode if you have master-master replication in place. If you have master-slave replication you might consider using Mysql-Proxy to distribute select, update, insert requests between server. Any way, selects can be load balanced with HaProxy.

Definition of mysql servers
####
# Description of the sql servers
####

listen mysql-proxy *:3306
 mode tcp
 balance roundrobin
 option tcplog
server SQL01 192.168.10.21:3306 check port 3306
server SQL02 192.168.10.22:3306 check port 3306
server SQL02 192.168.10.23:3306 check port 3306

Share Button

Leave a Reply

You must be logged in to post a comment.