{"id":2362,"date":"2015-12-18T18:17:43","date_gmt":"2015-12-18T16:17:43","guid":{"rendered":"https:\/\/upcloud.com\/global\/us\/resources\/tutorials\/haproxy-load-balancer-ubuntu\/"},"modified":"2015-12-18T18:17:43","modified_gmt":"2015-12-18T16:17:43","slug":"haproxy-load-balancer-ubuntu","status":"publish","type":"tutorial","link":"https:\/\/upcloud.com\/global\/resources\/tutorials\/haproxy-load-balancer-ubuntu\/","title":{"rendered":"How to install HAProxy load balancer on Ubuntu"},"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 Ubuntu 16 in this guide, but also on&nbsp;<a href=\"https:\/\/upcloud.com\/global\/resources\/tutorials\/haproxy-load-balancer-debian\/\" target=\"_blank\" rel=\"noreferrer noopener\">Debian 8<\/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 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 Ubuntu 16 to its 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 need to be running at least the basic web services such as Apache2 or Nginx to test the load balance 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\/\">Try UpCloud for free!<\/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 Ubuntu default repositories, might not be the latest release. To determine what version number is offered through the official channels, enter the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt show haproxy<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">HAProxy has always three active stable versions of the releases, two of the latest versions in development plus a third older version that is 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 third-party repository. To install HAProxy from an outside repo, you will need to add the new repository with the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo add-apt-repository ppa:vbernat\/haproxy-1.7<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Confirm adding the new PPA by pressing the <em>Enter<\/em> key.<\/p>\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 apt update<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then install HAProxy as you normally would.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt install -y 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.8-1ppa1~xenial 2017\/07\/09\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 the instructions for how to configure 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 a straightforward process. 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 at HAProxy <a href=\"https:\/\/cbonte.github.io\/haproxy-dconv\/1.7\/configuration.html\" target=\"_blank\" rel=\"noopener noreferrer\">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 to. You can check the private IPs at your <a href=\"https:\/\/my.upcloud.com\/network\" target=\"_blank\" rel=\"noopener noreferrer\">UpCloud Control Panel<\/a> and <em>Private network<\/em>&nbsp;tab under <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;http_front listening to the port number 80, which then&nbsp;directs the traffic to the default backend named&nbsp;http_back. The additional stats URI \/haproxy?stats 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 at the backend each connection is transferred to. 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 algorithm&nbsp;is dynamic, which 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. Using this algorithm is recommended with&nbsp;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 to configure 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 that connections matching the <tt>url_blog<\/tt> condition should be 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\">At the backend side, the configuration sets up two server groups, <tt>http_back<\/tt>&nbsp;like before and the new one&nbsp;called <tt>blog_back<\/tt> that servers specifically connections 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 size-large\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/stats-178.png\" alt=\"HAProxy Ubuntu 1.7.8 statistics page\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The statistics&nbsp;page contains some 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 own 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 documentation 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;redundantly protect your web service, the load balancer can still leave a single point of failure. You can improve the high availability by setting up a floating IP between multiple load balancers. You can learn 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","protected":false},"author":3,"featured_media":27365,"comment_status":"open","ping_status":"closed","template":"","community-category":[223,274],"class_list":["post-2362","tutorial","type-tutorial","status-publish","has-post-thumbnail","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2362","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=2362"}],"version-history":[{"count":0,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2362\/revisions"}],"wp:attachment":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/media?parent=2362"}],"wp:term":[{"taxonomy":"community-category","embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/community-category?post=2362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}