{"id":2365,"date":"2015-12-18T18:17:13","date_gmt":"2015-12-18T16:17:13","guid":{"rendered":"https:\/\/upcloud.com\/global\/us\/resources\/tutorials\/haproxy-load-balancer-debian\/"},"modified":"2015-12-18T18:17:13","modified_gmt":"2015-12-18T16:17:13","slug":"haproxy-load-balancer-debian","status":"publish","type":"tutorial","link":"https:\/\/upcloud.com\/global\/resources\/tutorials\/haproxy-load-balancer-debian\/","title":{"rendered":"How to install HAProxy load balancer on Debian"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Try this guide out on UpCloud with our free trial!\u00a0<a href=\"https:\/\/signup.upcloud.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Get started<\/a><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Load balancing is a common solution for distributing&nbsp;web applications horizontally across multiple hosts while providing the users with a single point of access to the service.&nbsp;<a rel=\"noopener noreferrer\" href=\"http:\/\/www.haproxy.org\/\" target=\"_blank\">HAProxy<\/a> is one of the most popular&nbsp;open-source load-balancing software, which also offers high availability and proxy functionality.<\/p>\n\n\n\n\n\n<p class=\"wp-block-paragraph\">HAProxy aims to optimise resource usage, maximise throughput, minimise response time, and avoid overloading any single resource. It is available for installation on many&nbsp;Linux distributions like Debian 8 in this guide, but also on <a href=\"https:\/\/upcloud.com\/global\/resources\/tutorials\/haproxy-load-balancer-ubuntu\/\" target=\"_blank\" rel=\"noreferrer noopener\">Ubuntu 16<\/a> and <a href=\"https:\/\/upcloud.com\/global\/resources\/tutorials\/haproxy-load-balancer-centos\/\" target=\"_blank\" rel=\"noreferrer noopener\">CentOS 7<\/a> systems.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/haproxy-load-balancer-8.png\" alt=\"HAproxy load balancing\" class=\"wp-image-7166\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">HAProxy is particularly suited for very high-traffic websites and is, therefore, often used to improve web service reliability and performance for multi-server configurations. This guide lays out the steps for setting up HAProxy as a load balancer on Debian 8 to its own cloud host, which then directs the traffic to your web servers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As a pre-requirement for the best results, you should have a minimum of&nbsp;two web servers and a server for the load balancer. The web servers must be running at least the basic web service such as Apache2 or nginx to test the load balancing between them.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/signup.upcloud.com\/\">Test hosting on UpCloud!<\/a><\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Installing HAProxy 1.7<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As a fast-developing open-source application, HAProxy available for installation in the Debian default repositories might not be the latest release. To find out what version number is offered through the official channels, enter the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo aptitude show haproxy<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">HAProxy always has three active stable versions of the releases, two of the latest versions in development plus a third older version still&nbsp;receiving critical updates. You can always check the currently newest stable version listed on the HAProxy website and then decide which version you wish to go with.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">While the latest stable version, 1.7, of HAProxy is not yet available on the packet manager by default, it can be found in a backported repository. To install HAProxy from a backport repo, you will need to add the source using the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">echo \"deb http:\/\/httpredir.debian.org\/debian jessie-backports main\" | \n      sudo tee \/etc\/apt\/sources.list.d\/backports.list<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next, update your sources list.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo aptitude update<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then, install HAProxy from the backport using the command below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo aptitude install -t jessie-backports haproxy<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Afterwards, you can double-check the installed version number with the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">haproxy -v<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">HA-Proxy version 1.7.5-2~bpo8+1 2017\/05\/27\nCopyright 2000-2017 Willy Tarreau &lt;willy@haproxy.org&gt;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><a id=\"configuring-haproxy\"><\/a>The installation is then complete. Continue below with instructions on configuring the load balancer to redirect requests to your web servers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configuring the load balancer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Setting up HAProxy for load balancing is quite straightforward. All you need to do is tell HAProxy what kind of connections it should be listening for and where the connections should be relayed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a id=\"layer-4-load-balancing\"><\/a>This is done by creating a&nbsp;configuration file&nbsp;<tt>\/etc\/haproxy\/haproxy.cfg<\/tt> with the defining settings. You can read about the configuration options on the HAProxy <a rel=\"noopener noreferrer\" href=\"https:\/\/cbonte.github.io\/haproxy-dconv\/1.7\/configuration.html\" target=\"_blank\">documentation page<\/a> if you wish to find out more.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Load balancing on layer 4<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Once installed, HAProxy should have a template for configuring the load balancer.&nbsp;Open the configuration file, for example, using <tt>nano<\/tt> with the command underneath.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo nano \/etc\/haproxy\/haproxy.cfg<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following sections to the end of the file. Replace the <tt>&lt;server name&gt;<\/tt> with whatever you want to call your servers on the statistics page and the <tt>&lt;private IP&gt;<\/tt> with the private IPs for the servers you wish to direct the web traffic. You can check the private IPs in your UpCloud Control Panel and the Private Network&nbsp;tab under the <em>Network<\/em>&nbsp;menu.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">frontend http_front\n   bind *:80\n   stats uri \/haproxy?stats\n   default_backend http_back\n\nbackend http_back\n   balance roundrobin\n   server &lt;server1 name&gt; &lt;private IP 1&gt;:80 check\n   server &lt;server2 name&gt; &lt;private IP 2&gt;:80 check<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><a id=\"load-balancing-algorithms\"><\/a>This defines a layer 4 load balancer with a front-end name&nbsp;<tt>http_front<\/tt> listening to port number <tt>80<\/tt>, which then&nbsp;directs the traffic to the default backend named&nbsp;<tt>http_back<\/tt>. The additional <tt>stats URI \/haproxy?stats<\/tt> enable the statistics page at that specified address.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Different load-balancing algorithms<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Configuring the servers in the backend section allows HAProxy to use these servers for load balancing&nbsp;according to the <tt>roundrobin<\/tt>&nbsp;algorithm whenever available.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The balancing algorithms are used to decide which server each connection is transferred to at the backend. Some of the useful options include the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Roundrobin:&nbsp;Each server is used in turns according to its weights.&nbsp;This is the smoothest and fairest algorithm when the server\u2019s processing time remains equally distributed. This dynamic algorithm allows&nbsp;server weights to&nbsp;be adjusted&nbsp;on the fly.<\/li>\n\n\n\n<li>Leastconn:&nbsp;The server with the lowest number of connections is chosen. Round-robin is performed between&nbsp;servers with the same&nbsp;load. This algorithm is recommended for long sessions such as LDAP, SQL, TSE, etc., but it is not very well&nbsp;suited for short sessions such as HTTP.<\/li>\n\n\n\n<li>First:&nbsp;The first server with available connection slots receives the&nbsp;connection. The servers are chosen from the lowest numeric&nbsp;identifier to the highest, which&nbsp;defaults to the server\u2019s position on the farm. Once a server&nbsp;reaches its <tt>maxconn<\/tt> value, the next server is used.<\/li>\n\n\n\n<li><a id=\"layer-7-load-balancing\"><\/a>Source:&nbsp;The source IP address is hashed and divided by the total&nbsp;weight of the running servers to designate which server will&nbsp;receive the request. This way,&nbsp;the same client IP&nbsp;address will always reach the same server while&nbsp;the servers stay the same.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Configuring load balancing for layer 7<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Another possibility is configuring the load balancer&nbsp;to work on layer 7, which is useful when parts of your web application are located on different hosts. This can be accomplished by conditioning the connection transfer, for example, by the URL.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Open the HAProxy configuration file with a text editor.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo nano \/etc\/haproxy\/haproxy.cfg<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then, set the front and backend segments according to the example below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">frontend http_front\n   bind *:80\n   stats uri \/haproxy?stats\n   acl url_blog path_beg \/blog\n   use_backend blog_back if url_blog\n   default_backend http_back\n\nbackend http_back\n   balance roundrobin\n   server &lt;server name&gt; &lt;private IP&gt;:80 check\n   server &lt;server name&gt; &lt;private IP&gt;:80 check\n\nbackend blog_back\n   server &lt;server name&gt; &lt;private IP&gt;:80 check<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The front end declares an ACL rule named <tt>url_blog<\/tt> that applies to all connections with paths that begin with <tt>\/blog<\/tt>.&nbsp;<tt>Use_backend<\/tt> defines connections matching the url_blog condition as being served by the backend named <tt>blog_back<\/tt>, while all other requests are handled by the default backend.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On the backend side, the configuration sets up two server groups, http_back like before and the new one called blog_back, which servers connections specifically to <tt>example.com\/blog<\/tt>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After making the configurations, save the file and restart HAProxy with the next command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl restart haproxy<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><a id=\"setup-testing\"><\/a>If you get any errors or warnings at startup, check the configuration for any mistypes and then try restarting again.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Testing the&nbsp;setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With the HAProxy configured and running, open your load balancer server\u2019s public IP in a web browser and check that you connect to your backend correctly. The parameter <tt>stats uri<\/tt>&nbsp;in the configuration enables the statistics page at the defined address.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">http:\/\/&lt;load balancer public IP&gt;\/haproxy?stats<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When you load&nbsp;the statistics page and all your servers are listed in green, your configuration succeeded!<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/stats-175-1.png\" alt=\"HAProxy Debian 1.7.5 statistics page\" class=\"wp-image-4215\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The statistics&nbsp;page contains helpful information to keep track of your web hosts, including up and down times and session counts. If a server is listed in red, check that it is powered on and that you can ping it from the load balancer machine.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a id=\"statistics-password\"><\/a>In case your load balancer does not reply, check that HTTP connections are not getting blocked by a&nbsp;firewall. Also, confirm that HAProxy is running using the command below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl status haproxy<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Password protecting the statistics page<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">However, Having the statistics page listed at the front end is publicly open for anyone to view, which might not be such a good idea. Instead, you can set it up to its port number by adding the example below to the end of your&nbsp;<tt>haproxy.cfg<\/tt>&nbsp;file. Replace the&nbsp;<tt>username<\/tt>&nbsp;and&nbsp;<tt>password<\/tt>&nbsp;with something secure.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">listen stats\n   bind *:8181\n   stats enable\n   stats uri \/\n   stats realm Haproxy Statistics\n   stats auth username:password<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After adding the new <tt>listen<\/tt>&nbsp;group, remove the old reference to the <tt>stats uri<\/tt> from the <tt>frontend<\/tt>&nbsp;group. When done, save the file and restart HAProxy again.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl restart haproxy<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then open the load balancer again with the new port number, and log in with the username and password you set in the configuration file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">http:\/\/&lt;load balancer public IP&gt;:8181<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Check that your servers are still reporting all green, and then open just the load balancer IP without any port numbers on your web browser.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">http:\/\/&lt;load balancer public IP&gt;\/<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If your backend servers have at least slightly different landing pages, you will notice that each time you reload the page, you get a reply from a different host. You can try different balancing algorithms in the configuration section or the full <a href=\"https:\/\/cbonte.github.io\/haproxy-dconv\/\" target=\"_blank\" rel=\"noopener noreferrer\">documentation<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Congratulations on successfully configuring HAProxy! A basic load balancer setup can considerably increase your web application performance and availability. This guide is, however, just an introduction to load balancing with HAProxy, which is capable of much more than what could be covered in first-time setup instructions. We recommend experimenting with different configurations with the help of the extensive <a href=\"https:\/\/cbonte.github.io\/haproxy-dconv\/1.7\/configuration.html\" target=\"_blank\" rel=\"noopener noreferrer\">documentation<\/a> available for HAProxy and then start planning the load balancing for your production environment.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">While using multiple hosts to&nbsp;protect your web service with redundancy, the load balancer itself can still leave a single point of failure. You can further improve the high availability by setting up a floating IP between multiple load balancers. You can find out more about this in our article for <a href=\"https:\/\/upcloud.com\/global\/docs\/guides\/floating-ip-addresses\/\" target=\"_blank\" rel=\"noreferrer noopener\">floating IPs on UpCloud<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"author":3,"featured_media":27365,"comment_status":"open","ping_status":"closed","template":"","community-category":[223,274],"class_list":["post-2365","tutorial","type-tutorial","status-publish","has-post-thumbnail","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial"}],"about":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/types\/tutorial"}],"author":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/comments?post=2365"}],"version-history":[{"count":0,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2365\/revisions"}],"wp:attachment":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/media?parent=2365"}],"wp:term":[{"taxonomy":"community-category","embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/community-category?post=2365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}