If you have contributed to open-source project ever, you may be aware about GitHub. GitHub is source version control where contributor uploads the code and users download the code. This GitHub can be very much loaded considering people throughout the world are accessing it. However, still we are able to access GitHub at reasonably fast speed. How this is possible? This is possible using software which balances load. HAProxy is such open source software..
HAProxy is free, open source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spreads requests across multiple servers.
HAProxy can be deployed in Linux platform. For ubuntu, its easier to deploy using apt-get
Moreover, it can be deployed as Linux container (docker).
Install haproxy using apt-get:
Install HAproxy
apt-get install haproxy
We need to enable HAProxy to be started by the init script /etc/default/haproxy. Set ENABLED option to 1 as:
ENABLED=1
To verify if this change is done properly, execute the init script of HAProxy without any parameters. You should see the following:
$ service haproxy <press_tab_key>
reload restart start status stop
HAProxy is now installed.
Configuring HAProxy
/etc/haproxy/haproxy.cfg carries the configuration for haproxy. Note that in below sample, two backend servers are reachable via 10.102.53.233 and 10.102.169.145
Sample haproxy.cfg
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
user haproxy
group haproxy
daemon
defaults
log global
mode http
option httplog
option dontlognull
contimeout 5000
clitimeout 50000
srvtimeout 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
frontend haproxy_in
bind *:80
default_backend haproxy_http
backend haproxy_http
balance roundrobin
mode http
server vm1 10.102.53.233:80 check
server vm2 10.102.169.145:80 check
To apply configuration, restart HAProxy daemon
Apply config
nsroot@ubuntu:~$ sudo service haproxy restart
* Restarting haproxy haproxy [ OK ]
nsroot@ubuntu:~$ service haproxy status
haproxy is running.
Testing HAProxy is straightforward. Here you need to perform http via web browser or using linux curl tool
from within the host ubuntu as shown in below example
Test output
nsroot@ubuntu:~$ curl http://localhost | less
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 11510 100 11510 0 0 1027k 0 --:--:-- --:--:-- --:--:-- 1124k
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
Modified from the Debian original for Ubuntu
Last updated: 2014-03-19
See: https://launchpad.net/bugs/1288690
-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Apache2 Ubuntu Default Page: It works</title>
<style type="text/css" media="screen">
* {
Pull HaProxy docker image
I assumed that docker is installed in your ubuntu box. If not, first deploy docker
Install HAProxy
root@ubuntu:~# docker images haproxy
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy latest 93e74258a04f 21 hours ago 139.1 MB
root@ubuntu:~# docker pull haproxy
Using default tag: latest
latest: Pulling from library/haproxy
8b87079b7a06: Already exists
a3ed95caeb02: Already exists
58acbe27a2eb: Already exists
0df18106ec05: Already exists
c7652a33f7e1: Already exists
Digest: sha256:4991df855175444e6ef3e8f04d093e8208bca65ca7b3ba75b6e3cfd44a8fe563
Status: Image is up to date for haproxy:latest
root@ubuntu:~# docker images haproxy
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy latest 93e74258a04f 21 hours ago 139.1 MB
Create HAProxy configuration file and host ubuntu machine (say, /home/haproxy/haproxy.cfg). This file carries the configuration for haproxy. Note that in below sample, two backend servers are reachable via 10.102.53.233 and 10.102.169.145
Sample haproxy.cfg
global
daemon
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend haproxy_in
bind *:80
default_backend haproxy_http
backend haproxy_http
balance roundrobin
mode http
server vm1 10.102.53.233:80 check
server vm2 10.102.169.145:80 check
Create HAProxy docker instance
HAProxy docker in host mode
[root@ubuntu /personal/]# docker run -dt -p 80 --net=host -v /home/haproxy/:/usr/local/etc/haproxy/:ro --name deepakk_haproxy_host haproxy
823490cb4def4601f11508fcc3f8a21cc5a0ae8edf29c63bdcc8d2b9ad5f1240
[root@ubuntu /personal/]# docker ps | grep deepakk_haproxy_host
823490cb4def haproxy "/docker-entrypoint.s" 9 seconds ago Up 8 seconds deepakk_haproxy_host
HAProxy docker in bridge mode
root@ubuntu:~# docker run -dt -p 1133:80 -v /home/haproxy/:/usr/local/etc/haproxy/:ro --name deepakk_haproxy haproxy
1d039a2239e4002d2c6fd43bb091b72bba595959e76d2b18a1819378c1057369
root@ubuntu:~# docker ps | grep deepakk_haproxy
1d039a2239e4 haproxy "/docker-entrypoint.s" 15 seconds ago Up 15 seconds 0.0.0.0:1133->80/tcp deepakk_haproxy
To see running haproxy
Check haproxy running
root@ubuntu:~# docker exec -it deepakk_haproxy /bin/bash
root@1d039a2239e4:/# ps -aef | grep haproxy
root 1 0 0 14:54 ? 00:00:00 /usr/local/sbin/haproxy-systemd-wrapper -p /run/haproxy.pid -f /usr/local/etc/haproxy/haproxy.cfg
root 12 1 0 14:54 ? 00:00:00 /usr/local/sbin/haproxy -p /run/haproxy.pid -f /usr/local/etc/haproxy/haproxy.cfg -Ds
root 13 12 0 14:54 ? 00:00:00 /usr/local/sbin/haproxy -p /run/haproxy.pid -f /usr/local/etc/haproxy/haproxy.cfg -Ds
root 25 14 0 14:56 ? 00:00:00 grep haproxy
Testing HAProxy is straightforward. Here you need to perform http via web browser or using linux curl tool
from within the host ubuntu as shown in below example
Sample test output
root@ubuntu:~# curl http://localhost:1133 | less
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
Modified from the Debian original for Ubuntu
Last updated: 2014-03-19
See: https://launchpad.net/bugs/1288690
-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Apache2 Ubuntu Default Page: It works</title>
<style type="text/css" media="screen">
* {
margin: 0px 0px 0px 0px;
padding: 0px 0px 0px 0px;
}
body, html {
padding: 3px 3px 3px 3px;
background-color: #D8DBE2;
font-family: Verdana, sans-serif;
font-size: 11pt;
text-align: center;
}
div.main_page {
position: relative;
display: table;
width: 800px;
To refresh config, docker kill -s SIGHUP <HAProxy ID> is used
HAProxy example with load balancing
root@ubuntu:~/docker-configs/haproxy/deepak# docker inspect 6646feac4834 | grep IP
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"IPAMConfig": {
"IPv4Address": "172.18.0.23"
"IPAddress": "172.18.0.23",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
root@ubuntu:~/docker-configs/haproxy/deepak# cat config
defaults
mode http
timeout server 5s
timeout connect 5s
timeout client 5s
frontend frontend
bind *:1234
mode http
use_backend backend
backend backend
server s1 172.18.0.23:80 check
frontend monitoring
bind *:1235
no log
stats uri /
stats enable
root@ubuntu:~/docker-configs/haproxy/deepak# docker ps | grep 6646feac4834
6646feac4834 ubuntu:v1.2 "/bin/bash" 15 hours ago Up 15 hours 22/tcp, 80/tcp, 443/tcp, 161/udp, 0.0.0.0:3333->3333/tcp SRV
root@ubuntu:~/docker-configs/haproxy/deepak# curl -k http://172.18.0.23:80
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
Modified from the Debian original for Ubuntu
Last updated: 2014-03-19
See: https://launchpad.net/bugs/1288690
-->
....
root@ubuntu:~/docker-configs/haproxy/deepak# curl -k http://localhost:1234
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
https://en.wikipedia.org/wiki/HAProxy
https://docs.docker.com/samples/haproxy/
https://www.digitalocean.com/community/tutorials/how-to-use-haproxy-to-set-up-http-load-balancing-on-an-ubuntu-vps
https://www.howtoforge.com/tutorial/ubuntu-load-balancer-haproxy/
https://hub.docker.com/_/haproxy/