{"id":3603,"date":"2019-07-13T00:06:14","date_gmt":"2019-07-12T21:06:14","guid":{"rendered":"https:\/\/kifarunix.com\/?p=3603"},"modified":"2020-06-06T09:55:11","modified_gmt":"2020-06-06T06:55:11","slug":"configure-haproxy-with-ssl-on-ubuntu-18-04-debian-10-9","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/configure-haproxy-with-ssl-on-ubuntu-18-04-debian-10-9\/","title":{"rendered":"Configure HAProxy Load Balancer with SSL on Ubuntu 18.04\/Debian 10\/9"},"content":{"rendered":"\n
In this guide, we are going to learn how to configure HAProxy load balancer with SSL on Ubuntu 18.04\/Debian 10\/9. HAProxy<\/a> is the de-factor opensource solution providing very fast and reliable high availability, load balancing and proxying for TCP and HTTP-based applications. As such, HAProxy is suited for very high traffic web sites.<\/p>\n\n\n\n In this guide, we are going to demonstrate how HAProxy performs load balancing using three web servers serving simple html pages. Our architecture looks like in the diagram below;<\/p>\n\n\n\n Run system update.<\/p>\n\n\n\n Once the update is done, proceed to install HAProxy on your Ubuntu\/Debian systems.<\/p>\n\n\n\n There are different HAProxy packages for each system. Hence, you need to create dedicated repository for each system.<\/p>\n\n\n\n Before you can create the repos, install the APT signing Key.<\/p>\n\n\n\n Next, create HAProxy repositories. On Debian 10 Buster\/Debian 9 Stretch, run the command below to create the repo.<\/p>\n\n\n\n On Ubuntu 18.04, you need to add the vbernat haproxy PPA repos as shown below;<\/p>\n\n\n\n Once the repos are created on each system, perform system update and install HAProxy.<\/p>\n\n\n\n To check the version of installed HAProxy, run the command below;<\/p>\n\n\n\n Configuration of HAProxy is all about where it is listening for connection requests and where to forward such requests to. It is therefore made up of frontend system and one or more back-end systems. The front-end system defines the IP address and port on which the proxy listens as well as the back-end systems to use for a particular proxy.<\/p>\n\n\n\n The main configuration file for HAProxy is \/etc\/haproxy\/haproxy.cfg<\/strong>.<\/p>\n\n\n\n The HAProxy configuration file is made up of four sections;<\/p>\n\n\n\n The To read more on the explanation of HAProxy configuration sections, check here<\/a>.<\/p>\n\n\n\n Before we can begin any configuration, let us create a backup of the configuration file.<\/p>\n\n\n\n The default HAProxy configuration contain the default configuration settings for the We are going to leave this settings just the way they are and add the our configurations for the <\/p>\n\n\n\n As stated above, Since we are configuring HAProxy with SSL, you need to generate the SSL\/TLS certificates. This guide uses self-signed certificates. You can get your own from a trusted CA.<\/p>\n\n\n\n Begin with generating private key.<\/p>\n\n\n\n Next, generate the Certificate signing request (CSR).<\/p>\n\n\n\n Create the Self Signed Certificate (CRT)<\/p>\n\n\n\n Create SSL pem file by containing both the key and the certificate.<\/p>\n\n\n\n Open the HAProxy configuration file and configure the frontend settings as shown below;<\/p>\n\n\n\n This is our frontend configuration basic settings.<\/p>\n\n\n\n In its basic configuration, out backend<\/strong> settings are defined below;<\/p>\n\n\n\n Once the configuration is done, you can run the command below to check the HAProxy configuration for any error.<\/p>\n\n\n\n When installed, HAProxy is set to run by default. To restart and enable HAProxy to run on system boot;<\/p>\n\n\n\n To check the status;<\/p>\n\n\n\n If UFW is running, Open port 443,<\/p>\n\n\n\n Now that the configuration is done, you can access your HAProxy from web to see if it performs any round-robin load balancing using the address, https:\/\/<haproxy-IP><\/strong>.<\/p>\n\n\n\n The first page the loads shows content server from webapp_01<\/strong>. For this demo, we have three test html pages. When refreshed, content from other servers is shown.<\/p>\n\n\n\n Well, as you can realize, the pages are served in order, one after the other. That is it on our basic tutorial on how to configure HAProxy Load Balancer with Self-Signed certificate on Ubuntu 18.04\/Debian 10\/Debian 9.<\/p>\n\n\n\n You can read more about HAProxy and configuration options in the documentation page below;<\/p>\n\n\n\n HAProxy Configuration Manual<\/a><\/p>\n\n\n\n Install and Setup HAProxy on Ubuntu 20.04<\/a><\/p>\n\n\n\n Setup HAProxy Load Balancer on Fedora 30\/Fedora 29<\/a><\/p>\n\n\n\nConfigure HAProxy Load Balancer with SSL on Ubuntu 18.04\/Debian 10\/9<\/h2>\n\n\n\n
<\/a><\/figure><\/div>\n\n\n\n
Install HAProxy on Ubuntu 18.04\/Debian 10\/Debian 9<\/h3>\n\n\n\n
apt update\napt upgrade<\/code><\/pre>\n\n\n\n
Create HAProxy Repository<\/h3>\n\n\n\n
apt install curl -y\ncurl https:\/\/haproxy.debian.net\/bernat.debian.org.gpg | apt-key add -<\/code><\/pre>\n\n\n\n
echo \"deb http:\/\/haproxy.debian.net $(lsb_release -cs)-backports-2.0 main\" | tee \/etc\/apt\/sources.list.d\/haproxy.list<\/code><\/pre>\n\n\n\n
apt install software-properties-common\nadd-apt-repository ppa:vbernat\/haproxy-2.0<\/code><\/pre>\n\n\n\n
apt update\napt install haproxy=2.0.\\*<\/code><\/pre>\n\n\n\n
haproxy -v<\/code><\/pre>\n\n\n\n
HA-Proxy version 2.0.1-1~bpo9+1 2019\/06\/27 - https:\/\/haproxy.org\/<\/code><\/pre>\n\n\n\n
Configure HAProxy Load Balancer on Ubuntu 18.04\/Debian 10\/9<\/h3>\n\n\n\n
global<\/code>
The global section defines process-wide security and performance tunings that affect HAProxy at a low level.
<\/li>defaults<\/code>
The global section defines the configuration settings that are applied to all of the frontend<\/code> and
backend<\/code> sections. You can define multiple default sections but the sub-sequent defaults sections override that came before it.
<\/li>frontend<\/code>
When HAProxy is placed as a reverse proxy, the frontend<\/code> section defines the IP addresses and ports that clients can connect to.
<\/li>backend<\/code>
The backend section defines the group of servers that will be load balanced and assigned to handle requests.<\/li><\/ul>\n\n\n\nfrontend<\/code> and
backend<\/code> sections can be combined using the
listen<\/code> section. It can also be used to server HAProxy statistics page<\/a>.<\/p>\n\n\n\n
cp \/etc\/haproxy\/haproxy.cfg{,.bak}<\/code><\/pre>\n\n\n\n
global<\/code> and
default<\/code> section.<\/p>\n\n\n\n
global\n log \/dev\/log local0\n log \/dev\/log local1 notice\n chroot \/var\/lib\/haproxy\n stats socket \/run\/haproxy\/admin.sock mode 660 level admin expose-fd listeners\n stats timeout 30s\n user haproxy\n group haproxy\n daemon\n\n # Default SSL material locations\n ca-base \/etc\/ssl\/certs\n crt-base \/etc\/ssl\/private\n\n # Default ciphers to use on SSL-enabled listening sockets.\n # For more information, see ciphers(1SSL). This list is from:\n # https:\/\/hynek.me\/articles\/hardening-your-web-servers-ssl-ciphers\/\n # An alternative list with additional directives can be obtained from\n # https:\/\/mozilla.github.io\/server-side-tls\/ssl-config-generator\/?server=haproxy\n ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS\n ssl-default-bind-options no-sslv3\n ## Add the line below\n tune.ssl.default-dh-param 2048\n\ndefaults\n log global\n mode http\n option httplog\n option dontlognull\n timeout connect 5000\n timeout client 50000\n timeout client 50000\n timeout server 50000\n errorfile 400 \/etc\/haproxy\/errors\/400.http\n errorfile 403 \/etc\/haproxy\/errors\/403.http\n errorfile 408 \/etc\/haproxy\/errors\/408.http\n errorfile 500 \/etc\/haproxy\/errors\/500.http\n errorfile 502 \/etc\/haproxy\/errors\/502.http\n errorfile 503 \/etc\/haproxy\/errors\/503.http\n errorfile 504 \/etc\/haproxy\/errors\/504.http<\/code><\/pre>\n\n\n\n
frontend<\/code> and
backend<\/code> sections. However, you can add the line
tune.ssl.default-dh-param 2048<\/code> to the global<\/strong> section which sets the maximum size of the Diffie-Hellman parameters used for generating the ephemeral\/temporary Diffie-Hellman key in case of DHE key exchange.<\/p>\n\n\n\n
Configure HAProxy with SSL on Ubuntu 18.04\/Debian 10\/9<\/h3>\n\n\n\n
frontend<\/code> section defines the IP address on which to listen for connection requests.<\/p>\n\n\n\n
Generating Self-Signed SSL Certificates for HAProxy<\/h4>\n\n\n\n
openssl genrsa -out \/etc\/ssl\/private\/haproxy.key 2048<\/code><\/pre>\n\n\n\n
openssl req -new -key \/etc\/ssl\/private\/haproxy.key -out \/etc\/ssl\/certs\/haproxy.csr<\/code><\/pre>\n\n\n\n
openssl x509 -req -days 365 -in \/etc\/ssl\/certs\/haproxy.csr -signkey \/etc\/ssl\/private\/haproxy.key -out \/etc\/ssl\/certs\/haproxy.crt<\/code><\/pre>\n\n\n\n
cat \/etc\/ssl\/private\/haproxy.key \/etc\/ssl\/certs\/haproxy.crt >> \/etc\/ssl\/certs\/haproxy.pem<\/code><\/pre>\n\n\n\n
Define HAProxy Frontend Configuration Settings<\/h4>\n\n\n\n
vim \/etc\/haproxy\/haproxy.cfg<\/code><\/pre>\n\n\n\n
frontend lb_01\n bind 192.168.43.62:443 ssl crt \/etc\/ssl\/certs\/haproxy.pem\n default_backend webapp_backends<\/code><\/pre>\n\n\n\n
default_backend<\/strong><\/code> gives the name of a
backend<\/code> to send traffic to.<\/li><\/ul>\n\n\n\n
Define HAProxy Backend Configuration Settings<\/h4>\n\n\n\n
backend webapp_backends\n balance roundrobin\n server webapp_01.example.com 192.168.43.252:80 check\n server webapp_02.example.com 192.168.43.174:80 check\n server webapp_03.example.com 192.168.43.21:80 check<\/code><\/pre>\n\n\n\n
Enable HAProxy Statistics via Web <\/h4>\n\n\n\n
listen stats\n bind 192.168.43.62:443 ssl crt \/etc\/ssl\/certs\/haproxy.pem\n stats enable # enable statistics reports \n stats hide-version # Hide the version of HAProxy\n stats refresh 30s # HAProxy refresh time\n stats show-node # Shows the hostname of the node\n stats auth admin:P@ssword # Authentication for Stats page\n stats uri \/lb_stats # Statistics URL<\/code><\/pre>\n\n\n\n
Verify HAProxy Configuration<\/h4>\n\n\n\n
haproxy -c -f \/etc\/haproxy\/haproxy.cfg<\/code><\/pre>\n\n\n\n
Configuration file is valid<\/code><\/pre>\n\n\n\n
Running HAProxy<\/h4>\n\n\n\n
systemctl restart haproxy\nsystemctl enable haproxy<\/code><\/pre>\n\n\n\n
systemctl status haproxy\n\u25cf haproxy.service - HAProxy Load Balancer\n Loaded: loaded (\/lib\/systemd\/system\/haproxy.service; enabled; vendor preset: enabled)\n Active: active (running) since Fri 2019-07-12 23:22:58 EAT; 5s ago\n Docs: man:haproxy(1)\n file:\/usr\/share\/doc\/haproxy\/configuration.txt.gz\n Process: 10686 ExecStartPre=\/usr\/sbin\/haproxy -f $CONFIG -c -q $EXTRAOPTS (code=exited, status=0\/SUCCESS)\n Main PID: 10687 (haproxy)\n Tasks: 2 (limit: 2340)\n CGroup: \/system.slice\/haproxy.service\n \u251c\u250010687 \/usr\/sbin\/haproxy -Ws -f \/etc\/haproxy\/haproxy.cfg -p \/run\/haproxy.pid -S \/run\/haproxy-master.sock\n \u2514\u250010688 \/usr\/sbin\/haproxy -Ws -f \/etc\/haproxy\/haproxy.cfg -p \/run\/haproxy.pid -S \/run\/haproxy-master.sock<\/code><\/pre>\n\n\n\n
ufw allow 443<\/code><\/pre>\n\n\n\n
Verify HAProxy Load Balancing<\/h4>\n\n\n\n
<\/a><\/figure><\/div>\n\n\n\n
<\/a><\/figure><\/div>\n\n\n\n
<\/a><\/figure><\/div>\n\n\n\n
Related Tutorials<\/h4>\n\n\n\n