A short overview of what HaProxy can do

HaProxy is a wonderful and very flexible tool for balancing traffic. It has a whole pile of options and configuration variants, and it has its own quirks. It would be very hard for me to lay out the contents of all my drafts on this topic inside one article, because it would come out big and hard to digest, but I will try to give an overview of the basic principles of the configuration.

At the moment of writing the stable release 1.5.3 came out with support for balancing SSL connections. Back in the days when I had to work with Haproxy, the delivery of SSL traffic to the final server was done with a plain forwarding of the connection to port 443.

Unfortunately the latest version is not available yet in the CentOS and Ubuntu repositories. So I am not going to look at it. There will be a separate article about installing and configuring HaProxy from source.

It is installed with the usual package manager. From here on I work with CentOS:

yum install haproxy

After the installation the config file is stored in /etc/haproxy

By default you can describe all the settings in that file. There are the general haproxy settings and the settings of the so called back-ends.

The general settings from the global section as a rule do not need any changes.

I will look at the back-end configuration in more detail. A BackEnd is a server that sits behind the load balancer, many people call them heads or web heads. There are two approaches to describing clusters of servers sitting behind the load balancer:

1. The simple listen->servers method is used when you have several web servers (say 3), their parameters are the same (CPU/RAM) and all the traffic is distributed evenly between them. There is no difference in what the servers do. Each of them can handle all the incoming requests equally well. In that case

listen listener_mane
bind <strong>ip_address</strong>:80
option &lt;option1&gt;
option &lt;option2&gt;
....................
option &lt;optionN&lt;
server server1 192.168.1.10:80 &lt;option1&gt; &lt;option2&gt; ... &lt;option N>
server server2 192.168.1.20:80 &lt;option1&gt; &lt;option2&gt; ... &lt;option N>
server server3 192.168.1.30:80 &lt;option1&gt; &lt;option2&gt; ... &lt;option N>
  1. The finer frontend->backend->servers method. In this case the servers are grouped into so called backends, which are essentially something like the listen section described in the previous point. The backends in turn are joined into a global frontend.
frontend &lt;instance_name&gt;
bind &lt;ip_address:port&gt;
mode &lt;layer mode&gt;
option &lt;option1&gt;
option &lt;option2&gt;
…
option &lt;optionN&gt;
acl &lt;acl_name1&gt; &lt;acl_type&gt; &lt;acl_definition&gt;

use_backend &lt;backend_name&gt; if &lt;acl_name1&gt; 
default_backend &lt;backend_name&gt;

backend static &lt;backend_name&gt;
balance &lt;ballance method&gt;
option &lt;option1&gt;
option &lt;option2&gt;
…
option &lt;optionN&gt;
server &lt;server_name1&gt; &lt;nod_ip_address&gt;:&lt;port&gt; &lt;option1&gt; &lt;option2&gt; … &lt;option N&gt;
server &lt;server_name2&gt; &lt;nod_ip_address&gt;:&lt;port&gt; &lt;option1&gt; &lt;option2&gt; … &lt;option N&gt;

backend web&lt;backend_name&gt; server &lt;server_name2&gt; &lt;nod_ip_address&gt;:&lt;port&gt; &lt;option1&gt; &lt;option2&gt; … &lt;option N&gt;
backend backoffice &lt;backend_name&gt; server &lt;server_name2&gt; &lt;nod_ip_address&gt;:&lt;port&gt; &lt;option1&gt; &lt;option2&gt; … &lt;option N&gt;

bind - defines the ip address and the port on which the ip socket for the incoming connections will be created.

As for the load balancing modes (**mode **), there are only two of them:

  • http - used exclusively for balancing http traffic (OSI layer 7). Lets you use the ability to manipulate headers, cookies and so on.
  • tcp - the universal mode. Can be used for balancing any traffic (https, mysql, smtp, and so on)

option - with this directive you can enable extra goodies for the section you need. All the options can be seen here.

Practical configuration examples can be read here.

The description of all the options of the server section can be found at http://haproxy.tech-notes.net/5-2-server-and-default-server-options/

Share Button