Install Haproxy On Centos 7

May 10, 2019  To know how to install CentOS 7 visit the following link. HAProxy is a free and opensource TCP/HTTP Load Balancer and proxy solution.It distributes the workload or traffic across multiple servers.HAProxy can be implemented using multiple algorithms. HAProxy Algorithms. Round Robin: This algorithm is the most used one.

In todays article we will install MariaDB Galera cluster with HAproxy for load balanced MariaDB and wordpress. Galera is active-active clustering technology, meaning that it can support writes on all nodes which are then replicated across cluster. There is also active-passive clustering, where only node is writable. We will also install HAproxy for load balancing on our cluster. Install is going to be done on 5 CentOS 7 servers, three for MariaDB 10.1 Galera, one for Haproxy and one for wordpress. HAproxy works in such a way that it routes requests to each node in round robin mode, while presenting itself as a front end. Here we are using tradional way of clustering by having database servers sitting in private network and only webserver is facing to public ip address.

Setting up the Galera cluster

On all servers we need to install mysql. First we will add repository

echo '[mariadb] name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1' >> /etc/yum.repos.d/MariaDB.repo

Then we install mariadb, rsync and xinetd

sudo yum install mariadb-server rsync xinetd

We need to edit /etc/hosts, any editor will do but I will use nano

nano /etc/hosts

Out there ip and names of your hosts, for example

192.168.207.241 mariadb01
192.168.209.91 mariadb02
192.168.129.168 mariadb03

Make sure you use private ip of your hosts, if you use public ones you will need to encrypt traffic between them to secure your cluster.

After install MariaDB, edit

nano /etc/my.cnf.d/server.cnf

On all servers, you need to find [galera] section in file and make it look like this, save for ip addresses which are bold and should be changed

[galera] # Mandatory settings
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so

#add your node ips here
wsrep_cluster_address='gcomm://192.168.207.241,192.168.209.91,192.168.129.168'
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
#Cluster name
wsrep_cluster_name='linoxide_cluster'
# Allow server to accept connections on all interfaces.

bind-address=0.0.0.0

# this server ip, change for each server
wsrep_node_address='192.168.207.241'
# this server name, change for each server
wsrep_node_name='mariadb01'

wsrep_sst_method=rsync

Staring the Galera Cluster

After this have been configured you need to start the cluster

First on the mariadb01 node which is the master with this command

galera_new_cluster

Then on other two nodes with normal systemctl command:

systemctl start mariadb

Next we can verify that cluster is running:

mysql -u root -p -e 'SHOW STATUS LIKE 'wsrep_cluster_size'

Next thing would be to run the mysql_secure_installation script

mysql_secure_installation

Setting up the firewalld

Now when all three nodes are connected we can bring up firewalld and configure it.

systemctl start firewalld

Open the mariadb client and galera replication ports:

firewall-cmd --permanent --add-port=3306/tcp
firewall-cmd --permanent --add-port=4567/tcp

Open rsync port that we use for replication

firewall-cmd --permanent --add-port=873/tcp

Other important ports

firewall-cmd --permanent --add-port=4444/tcp
firewall-cmd --permanent --add-port=9200/tcp

Lastly reload the firewall

firewall-cmd --reload

Using xinetd and clustercheck

Clustercheck is useful script for monitoring the cluster. Lets install it by following commands

wget https://raw.githubusercontent.com/olafz/percona-clustercheck/master/clustercheck
chmod +x clustercheck
mv clustercheck /usr/bin/

Next we need to add mysqlchk to the services list. For that we edit services file:

nano /etc/services

Since it is long file, in nano type Ctrl-W and search for 9200. All services using that port needs to be commented, and new service needs to be added, so that part needs to look like this:

mysqlchk 9200/tcp # mysqlchk
#wap-wsp 9200/tcp # WAP connectionless session service
#wap-wsp 9200/udp # WAP connectionless session service

When this is savaed and done we can start xinetd

systemctl start xinetd

All this need to be done on all nodes, and now only on master we need to add user for clustercheck.

mysql -u root -p

GRANT PROCESS ON *.* TO 'clustercheckuser'@'localhost' IDENTIFIED BY 'clustercheckpassword!';

exit;

Next we can try clustercheck script

clustercheck

Setting up HAproxy

Login to your designated haproxy server. We are now installing the load balancer

Edit your /etc/hosts file to be something like this (with your ips, of course)

192.168.207.241 mariadb01
192.168.209.91 mariadb02
192.168.129.168 mariadb03
192.168.210.252 haproxy01

Next we can install haproxy

yum install haproxy

Next we need to edit rsyslog.conf

nano /etc/rsyslog.conf

Uncomment those two lines

# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

Save and exit

echo 'local2.=info /var/log/haproxy-access.log
local2.notice /var/log/haproxy-info.log
' >> /etc/rsyslog.d/haproxy.conf

lets backup default haproxy configuration

mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bk

Next we make new file with with nano

nano /etc/haproxy/haproxy.cfg

Use this pastebin as configuration, but change the ip addresses.

It is time to setup firewall on haproxy node

systemctl start firewalld
firewall-cmd --permanent --add-port=9000/tcp
firewall-cmd --permanent --add-port=3030/tcp

Then reload the firewall

firewall-cmd --reload

Setting up access the MariaDB from HAproxy

On mariadb cluster type:

mysql -u root -p Cp341 modbus slave manual.

GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY 'verystrongpassword';

Then we go to haproxy01 server and try to access the database.

You must have the MariaDB-client installed, so lets first do that:

echo '[mariadb] name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1' >> /etc/yum.repos.d/MariaDB.repo

That was for adding repository, this command installs it

yum install MariaDB-client

And then lets test if

mysql -u root -p -h 192.168.210.252 -P 3030 -e 'select Host, User, Password from mysql.user'

Make sure you enter verystrongpassword as password and not your regular root password.

Installing Wordpress

Lastly we will setup wordpress to use on our cluster. There is work to be done on mariadb01 node, on haproxy01 and on wp01 node.

On mariadb01

mysql -u root -p

CREATE DATABASE wordpress;

CREATE USER wordpressuser@localhost IDENTIFIED BY 'password';

GRANT ALL ON wordpress.* TO wordpressuser@'192.168.210.252' IDENTIFIED BY 'password';

On wp01 node set in /etc/hosts/

192.168.220.17 wp01
192.168.210.252 haproxy01

Also on haproxy01 add

192.168.220.17 wp01

Next we need to install required packages on wp01 node

yum install httpd php php-gd php-mysqlnd rsync

From here we need to use non-root account with sudo privileges. I don't have one, so I will create it:

useradd miki
usermod miki -aG wheel

passwd miki

Then log in as my user

su miki

Change dir to home

cd

And then download the latest version of wordpress and unpack it

wget http://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz

We will use rsync to copy the wordpress to apache dir

sudo rsync -avP ~/wordpress/ /var/www/html/
mkdir /var/www/html/wp-content/uploads

We also need to change ownership to apache user

sudo chown -R apache:apache /var/www/html/*

Configuring wordpress

WordPress is configured in wp-config.php file, there we need to set parameters like host address of database server, login credentials, database name. Lets backup the config file:

cd /var/www/html

cp wp-config-sample.php wp-config.php

And then we do editing:

nano wp-config.php

Only change those lines

define('DB_NAME', 'wordpress');

/** MySQL database username */
define('DB_USER', 'wordpressuser');

/** MySQL database password */
define('DB_PASSWORD', 'password');

/** MySQL hostname */
define('DB_HOST', '192.168.210.252:3030');

Lastly, we restart the apache webserver:

sudo systemctl restart httpd

Complete the install of the WordPress from the browser

We can go to public address of wordpress node like bellow to continue install process of word-press

Now we have the cluster set up with wordpress running

Conclusion

Here we have set up wordpress on top of Galera cluster loadbalanced by HAproxy. This is resilient solution for high load sites and although it takes a while to setup, it gives your site near complete insurance from downtime. All traffic between nodes is done by private ip addresses, and only one public address for the Worpress front end is used to access the site. This would be all for this article, thank you for reading and have a good day.

Related

How To Use Cron to Automate Tasks on CentOS 8 Tutorial

Introduction

Let’s Encrypt is a new Certificate Authority (CA) that provides an easy way to obtain and install free TLS/SSL certificates, thereby enabling encrypted HTTPS on web servers. It simplifies the process by providing a software client, Certbot, that attempts to automate most of the required steps. Currently the entire process of obtaining and installing a certificate is fully automated only on Apache web servers. However, Certbot can be used to easily obtain a free SSL certificate, which can be installed manually, regardless of your choice of web server software.

In this tutorial, we will show you how to use Let’s Encrypt to obtain a free SSL certificate and use it with HAProxy on CentOS 7. We will also show you how to automatically renew your SSL certificate.

Prerequisites

Before following this tutorial, you’ll need a few things.

You should have an CentOS 7 server with a non-root user who has sudo privileges. You can learn how to set up such a user account by following steps 1-3 in our initial server setup for CentOS 7 tutorial.

You must own or control the registered domain name that you wish to use the certificate with. If you do not already have a registered domain name, you may register one with one of the many domain name registrars out there (e.g. Namecheap, GoDaddy, etc.).

If you haven’t already, be sure to create an A Record that points your domain to the public IP address of your server. This is required because of how Let’s Encrypt validates that you own the domain it is issuing a certificate for. For example, if you want to obtain a certificate for example.com, that domain must resolve to your server for the validation process to work. Our setup will use example.com and www.example.com as the domain names, so both DNS records are required.

Once you have all of the prerequisites out of the way, let’s move on to installing Certbot, the Let’s Encrypt client software.

Step 1 — Installing Certbot, the Let’s Encrypt Client

The first step to using Let’s Encrypt to obtain an SSL certificate is to install the certbot software on your server. Currently, the best way to install this is through the EPEL repository.

Enable access to the EPEL repository on your server by typing:

Once the repository has been enabled, you can obtain the certbot package by typing:

The certbot Let’s Encrypt client should now be installed and ready to use.

Step 2 — Obtaining a Certificate

Let’s Encrypt provides a variety of ways to obtain SSL certificates, through various plugins. Unlike the Apache plugin, which is covered in a different tutorial, most of the plugins will only help you with obtaining a certificate which you must manually configure your web server to use. Plugins that only obtain certificates, and don’t install them, are referred to as “authenticators” because they are used to authenticate whether a server should be issued a certificate.

We’ll show you how to use the Standalone plugin to obtain an SSL certificate.

Verify Port 80 is Open

The Standalone plugin provides a very simple way to obtain SSL certificates. It works by temporarily running a small web server, on port 80, on your server, to which the Let’s Encrypt CA can connect and validate your server’s identity before issuing a certificate. As such, this method requires that port 80 is not in use. That is, be sure to stop your normal web server, if it’s using port 80 (i.e. http), before attempting to use this plugin.

For example, if you’re using HAProxy, you can stop it by running this command:

If you’re not sure if port 80 is in use, you can run this command:

If there is no output when you run this command, you can use the Standalone plugin.

Run Certbot

Now use the Standalone plugin by running this command:

You will be prompted to enter your email address and agree to the Let’s Encrypt terms of service. Afterwards, the http challenge will run. If everything is successful, certbot will print an output message like this:

You will want to note the path and expiration date of your certificate, which was highlighted in the example output above.

Note: If your domain is routing through a DNS service like CloudFlare, you will need to temporarily disable it until you have obtained the certificate.

Certificate Files

After obtaining the cert, you will have the following PEM-encoded files:

  • cert.pem: Your domain’s certificate
  • chain.pem: The Let’s Encrypt chain certificate
  • fullchain.pem:cert.pem and chain.pem combined
  • privkey.pem: Your certificate’s private key

It’s important that you are aware of the location of the certificate files that were just created, so you can use them in your web server configuration. The files themselves are placed in a subdirectory in /etc/letsencrypt/archive. However, Certbot creates symbolic links to the most recent certificate files in the /etc/letsencrypt/live/your_domain_name directory.

You can check that the files exist by running this command (substituting in your domain name):

The output should be the four previously mentioned certificate files.

Combine fullchain.pem and privkey.pem

When configuring HAProxy to perform SSL termination, so it will encrypt traffic between itself and the end user, you must combine fullchain.pem and privkey.pem into a single file.

First, create the directory where the combined file will be placed, /etc/haproxy/certs:

Next, create the combined file with this cat command (substitute the highlighted example.com with your domain name):

Secure access to the combined file, which contains the private key, with this command:

Now we’re ready to use the SSL cert and private key with HAProxy.

Step 3 — Installing HAProxy

This step covers the installation of HAProxy. If it’s already installed on your server, skip this step.

Install HAProxy with yum:

HAProxy is now installed but needs to be configured.

Step 4 — Configuring HAProxy

This section will show you how to configure basic HAProxy with SSL setup. It also covers how to configure HAProxy to allow us to auto-renew our Let’s Encrypt certificate.

Open haproxy.cfg in a text editor:

Keep this file open as we edit it in the next several sections.

Global Section

Add this line to the global section to configure the maximum size of temporary DHE keys that are generated:

Frontend Sections

Now we’re ready to define our frontend sections.

Note: The default HAProxy configuration includes a frontend and several backends. Feel free to delete them as we will not be using them.

The first thing we want to add is a frontend to handle incoming HTTP connections, and send them to a default backend (which we’ll define later). At the end of the file, let’s add a frontend called www-http. Be sure to replace haproxy_public_IP with the public IP address of your HAProxy server:

haproxy.cfg — 2 of 5

Next, we will add a frontend to handle incoming HTTPS connections. At the end of the file, add a frontend called www-https. Be sure to replace haproxy_www_public_IP with the public IP of your HAProxy server. Also, you will need to replace example.com with your domain name (which should correspond to the certificate file you created earlier):

This frontend uses an ACL (letsencrypt-acl) to send Let’s Encrypt validation requests (for /.well-known/acme-challenge) to the letsencrypt-backend backend, which will enable us to renew the certificate without stopping the HAProxy service. All other requests will be forwarded to the www-backend, which is the backend that will serve our web application or site.

Backend Sections

After you are finished configuring the frontends, add the www-backend backend by adding the following lines. Be sure to replace the highlighted words with the respective private IP addresses of your web servers (adjust the number of server lines to match how many backend servers you have):

haproxy.cfg — 4 of 5

Any traffic that this backend receives will be balanced across its server entries, over HTTP (port 80).

Lastly, add the letsencrypt-backend backend, by adding these lines

This backend, which only handles Let’s Encrypt ACME challenges that are used for certificate requests and renewals, sends traffic to the localhost on port 54321. We’ll use this port instead of 80 and 443 when we renew our Let’s Encrypt SSL certificate.

Now we’re ready to start HAProxy:

Note: If you’re having trouble with the haproxy.cfg configuration file, check out this GitHub Gist for an example.

The Let’s Encrypt TLS/SSL certificate is now in place, and we’re ready to set up the auto-renewal script. At this point, you should test that the TLS/SSL certificate works by visiting your domain in a web browser.

Step 5 — Setting Up Auto Renewal

Let’s Encrypt certificates are valid for just 90 days, so it’s important to automate the renewal process.

A practical way to ensure your certificates won’t get outdated is to create a cron job that will automatically handle the renewal process for you. The cronjob will run certbot daily and renew the certificates if they’re within thirty days of expiring. certbot will also run a special renew-hook script after any successful renewal. We’ll use this renewal script to update our combined .pem file and reload haproxy.

Let’s create that script now, then test it.

Create a Renewal Script

Open up a new file in /usr/local/bin as root:

This will be a new blank text file. Paste in the following short script, being sure to update the highlighted domain name with your own:

Save and close the file. This script moves into the correct Let’s Encrypt directory, runs the cat command to concatenate the two .pem files into one, then reloads haproxy.

Next, make the script executable:

Then run the script:

It should run without error. Next we’ll update Certbot and configure it to run this renewal script.

Update certbot Configs

The certbot renew command that we’ll use to renew our certificates reads a config file that was created the first time we ran certbot. We need to open this file and update the port that certbot uses to run its standalone http server so it does’t conflict with haproxy (which is already listening on ports 80 and 443). Open the config file in a text editor:

We need to change the http01_port line, so it reads like this:

example.com.conf

Save and close the file. Now test the renewal process, specifying --dry-run so we don’t actually renew anything:

Certbot will listen on port 54321 for the renewal challenge, and haproxy will proxy the request from port 80 to 54321.

Create a Cron Job

Next, we will edit the crontab to create a new job that will run the certbot renew command every day. To edit the crontab for the root user, run:

Add the following to the bottom of the file:

Save and exit. This will create a new cron job that will execute the certbot renew command every day at 2:30 am. The output produced by the command will be piped to a log file located at /var/log/le-renewal.log. If the certificate is actually renewed, the --renew-hook script will run to create the combined PEM file and reload haproxy.

Conclusion

That’s it! HAProxy is now using a free Let’s Encrypt TLS/SSL certificate to securely serve HTTPS traffic.