Step-by-Step Guide: Setting Up a High-Availability Cluster for Nginx, PHP-FPM, and MySQL with Pacemaker and DRBD

0 1
Read Time:9 Minute, 4 Second

Introduction

In this tutorial, we will show you how to set up a highly available cluster for web services using Pacemaker and DRBD. The cluster will consist of two nodes, with both nodes running nginx, PHP-FPM, and MySQL. Pacemaker will be used to manage the resources and ensure that they are highly available, while DRBD will be used to replicate data between the nodes.

High availability is critical for modern web applications. If one node fails, the other node will take over the load, ensuring that the web services remain available. Pacemaker is an open-source cluster resource manager that can be used to manage highly available services. DRBD is a distributed replicated block device that can be used to replicate data between nodes.

This tutorial assumes that you have some experience with Linux servers, have root access to the servers, and have a basic understanding of command line and network configuration. Additionally, we assume that you are familiar with installing and configuring web services like nginx, PHP-FPM, and MySQL.

Let’s get started!

2. Prerequisites

Before we begin, make sure you have the following prerequisites:

  • Two Linux servers with at least 2 CPUs, 4GB RAM, and 10GB of free disk space.
  • Root access to both servers.
  • A basic understanding of command line and network configuration.
  • Familiarity with installing and configuring web services like nginx, PHP-FPM, and MySQL.
  • Network connectivity between the two servers, either through a private network or over the internet.

You will need to install the following software on both servers:

  • Pacemaker: Pacemaker is an open-source cluster resource manager that will be used to manage the resources in the cluster.
  • DRBD: DRBD is a distributed replicated block device that will be used to replicate data between the nodes.
  • nginx: nginx is a popular open-source web server that will be used to serve the website content.
  • PHP-FPM: PHP-FPM is a popular open-source PHP FastCGI Process Manager that will be used to process PHP scripts.
  • MySQL: MySQL is a popular open-source relational database management system that will be used to store website data.

You will also need to configure nginx, PHP-FPM, and MySQL to work with virtual IP addresses.

In the next step, we will install and configure the necessary software.

3. Install and Configure Software

In this step, we will install and configure the necessary software on both nodes.

Install Pacemaker and DRBD

On each node, run the following commands to install Pacemaker and DRBD:

sudo apt-get update
sudo apt-get install -y pacemaker drbd-utils

Install nginx, PHP-FPM, and MySQL

On each node, run the following commands to install nginx, PHP-FPM, and MySQL:

sudo apt-get install -y nginx php-fpm mysql-server

Configure nginx, PHP-FPM, and MySQL to Work with Virtual IP Addresses

To ensure high availability, we need to configure nginx, PHP-FPM, and MySQL to work with virtual IP addresses.

Open the nginx configuration file /etc/nginx/sites-available/default and replace the listen directive with the following line:

listen 10.0.0.10:80;

This sets the IP address for nginx to the virtual IP address we will create in the next step.

Open the PHP-FPM configuration file /etc/php/7.4/fpm/pool.d/www.conf and replace the listen directive with the following line:

listen = 10.0.0.11:9000

This sets the IP address for PHP-FPM to the virtual IP address we will create in the next step.

Open the MySQL configuration file /etc/mysql/mysql.conf.d/mysqld.cnf and replace the bind-address directive with the following line:

bind-address            = 10.0.0.12

This sets the IP address for MySQL to the virtual IP address we will create in the next step.

Once you have made these changes, restart nginx, PHP-FPM, and MySQL:

sudo systemctl restart nginx php7.4-fpm mysql

In the next step, we will set up DRBD filesystems for MySQL and website content.

Step 4: Set up DRBD Filesystems for MySQL and Website Content

To ensure high availability for MySQL and website content, we need to create two DRBD devices: one for MySQL and the other for website content.

On each node, we need to perform the following steps:

1: Create a new DRBD device:

drbdadm create-md r1
drbdadm create-md r2

2: Set up DRBD to replicate data between the nodes:

drbdadm -- --overwrite-data-of-peer primary r1
drbdadm -- --overwrite-data-of-peer primary r2
drbdadm primary --force r1
drbdadm primary --force r2

3: Format the DRBD devices:

mkfs.ext4 /dev/drbd1
mkfs.ext4 /dev/drbd2

4: Mount the DRBD devices:

mkdir /mnt/mysql
mkdir /mnt/nginx
mount /dev/drbd1 /mnt/mysql
mount /dev/drbd2 /mnt/nginx

In the first step, we create a new DRBD device using the drbdadm create-md command. We need to create two DRBD devices, one for MySQL and the other for website content.

In the second step, we set up DRBD to replicate data between the nodes. The --overwrite-data-of-peer option is used to ensure that the data on the peer node is overwritten with the local node’s data. The primary command is used to set the local node as the primary node for the device.

In the third step, we format the DRBD devices using the mkfs.ext4 command. We need to format both devices, one for MySQL and the other for website content.

In the fourth step, we mount the DRBD devices to the local file system using the mount command. We create two directories, one for MySQL and the other for website content, and mount the corresponding DRBD devices to these directories.

Once you have completed these steps, the DRBD devices should be ready to use for MySQL and website content. In the next step, we will create resources for each service.

Step 5: Create Resources for Each Service

To ensure high availability for each service, we need to create resources for each service that depend on their respective DRBD filesystems being mounted on the node.

On each node, we need to perform the following steps:

1: Create a resource for MySQL that depends on the DRBD device being mounted on the node:

pcs resource create mysql_fs Filesystem device="/dev/drbd1" directory="/var/lib/mysql" fstype="ext4" --group g_mysql --requires drbd1

2: Create a resource for nginx that depends on the DRBD filesystem being mounted on the node:

pcs resource create nginx_fs Filesystem device="/dev/drbd2" directory="/var/www/html" fstype="ext4" --group g_web --requires drbd2

3: Create a resource for PHP-FPM that depends on the nginx resource being started on the node:

pcs resource create php-fpm systemd:php-fpm op start timeout=90s op stop timeout=90s --group g_web --requires nginx_fs

In the first step, we create a resource for MySQL that depends on the DRBD filesystem being mounted on the node. The Filesystem resource agent is used to manage the DRBD filesystem. The device parameter specifies the device used by the DRBD filesystem, the directory parameter specifies the mount point of the filesystem, and the fstype parameter specifies the filesystem type.

In the second step, we create a resource for nginx that depends on the DRBD filesystem being mounted on the node. The Filesystem resource agent is used to manage the DRBD filesystem, with the same parameters as for MySQL.

In the third step, we create a resource for PHP-FPM that depends on the nginx resource being started on the node. This resource is set up to start a systemd service, with a timeout of 90 seconds for both the start and stop operations.

Once you have created the resources, you can check their status by running the following command:

pcs status

This will display the status of all resources and nodes in the cluster, including the resources for MySQL, nginx, and PHP-FPM.

Step 6: Set up Virtual IP Addresses

In this step, we will set up virtual IP addresses for each service on each node.

On each node, we need to perform the following steps:

1: Create a virtual IP address for MySQL on each node:

pcs resource create mysql_vip ocf:heartbeat:IPaddr2 ip=10.0.0.12 cidr_netmask=24 --group g_mysql

2: Create a virtual IP address for nginx on each node:

pcs resource create nginx_vip ocf:heartbeat:IPaddr2 ip=10.0.0.10 cidr_netmask=24 --group g_web

3: Create a virtual IP address for PHP-FPM on each node:

pcs resource create php-fpm_vip ocf:heartbeat:IPaddr2 ip=10.0.0.11 cidr_netmask=24 --group g_web

In the first step, we create a virtual IP address for MySQL on each node using the IPaddr2 resource agent. This virtual IP address will be used to access the MySQL service.

In the second step, we create a virtual IP address for nginx on each node using the IPaddr2 resource agent. This virtual IP address will be used to access the website content.

In the third step, we create a virtual IP address for PHP-FPM on each node using the IPaddr2 resource agent. This virtual IP address will be used to access the PHP-FPM service.

Once you have created the virtual IP addresses, you can check their status by running the following command:

pcs status

This will display the status of all resources and nodes in the cluster, including the virtual IP addresses for MySQL, nginx, and PHP-FPM.

Step 7: Test High Availability

In this step, we will test the high availability of the cluster by simulating a node failure.

On one of the nodes, run the following command to stop the Pacemaker service:

sudo systemctl stop pacemaker

This will simulate a node failure.

After a few moments, run the following command on the other node to check the status of the cluster:

pcs status

You should see that the resources for MySQL, nginx, and PHP-FPM have been migrated to the remaining node. The virtual IP addresses for each service should also have been moved to the remaining node.

Once you have confirmed that the resources have been migrated and the virtual IP addresses are accessible, you can bring the failed node back online by starting the Pacemaker service:

sudo systemctl start pacemaker

After a few moments, run the following command to check the status of the cluster:

pcs status

You should see that the resources have been migrated back to their original node.

Congratulations! You have successfully set up a highly available cluster for web services using Pacemaker and DRBD. Next consider reading “A Complete Beginner’s Guide to Managing a Pacemaker Cluster” for further information on how to manage your new Cluster! or visit https://clusterlabs.org/pacemaker/doc/2.1/Clusters_from_Scratch/html/ for a more in-depth explanation.

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

One thought on “Step-by-Step Guide: Setting Up a High-Availability Cluster for Nginx, PHP-FPM, and MySQL with Pacemaker and DRBD

Leave a comment