6 C
Texas
Muhammad Nabeel
Network and System Administrator

Create Nginx High Availability with Pacemaker and Corosync on CentOS 7

In this article, you will learn that how to create a high availability Nginx Web Server Cluster with Pacemaker, Corosync, and Pcsd. We will create Failover-cluster for Nginx web server using Pacemaker on a CentOS 7 system.

Pacemaker is an open source cluster manager software that achieves maximum high availability of your services.

Corosync Cluster Engine is an open source project derived from the OpenAIS project under new BSD License. It’s a group communication system with additional features for implementing High Availability within applications.

Prerequisites

  1. Two or more CentOS Servers
  2. Each server must have unique IP address
  3. At leat one Floating IP Address
  4. Root Privileges
- Advertisement -

Our Working Environment
host1 192.168.130.141
host2 192.168.130.142
host3 192.168.130.143
Floating IP Address 192.168.130.222

Run step 1 to step 3 on all servers host1, host2 and host3

Step 1: Configure Host File

Edit the host file on each server in order to map the hostname of all servers. login to each server (host1, host2 and host3) one by one and run below commands.

vi /etc/hosts

and add below configurations.

192.168.130.141 host1
192.168.130.142 host2
192.168.130.141 host3

Save changes and exit

Now test the hosts’ configuration using below commands.

ping -c 2 host1
ping -c 2 host2
ping -c 2 host3

Make sure ‘host1’, ‘host2’ and ‘host3’ are mapped to correct IP addresses.

Step 2: Install Nginx

Run below commands on host1, host2 and host3 to install nginx web server.

yum install epel-release -y
yum install nginx -y

After installation, set the default index.html page on each server.

##Run below Command on host1
echo '<h1>host1 - osradar.com</h1>' > /usr/share/nginx/html/index.html

##Run below Command on host2
echo '<h1>host2 - osradar.com</h1>' > /usr/share/nginx/html/index.html

##Run below Command on host3
echo '<h1>host3 - osradar.com</h1>' > /usr/share/nginx/html/index.html

Step 3: Installation and configuration of Pacemaker, Corosync, and Pcsd

Run below commands on each server to install Pacemaker, Corosync, and Pcsd

yum install corosync pacemaker pcs -y

Then run below commands on each server to enable all services to launch automatically at system boot.

systemctl enable pacemaker
systemctl enable corosync
systemctl enable pcsd

Now start the pcsd Pacemaker command line interface on all servers.

systemctl start pcsd

Now set a new password for ‘hacluster’ user password must be same for all servers. This user has been created automatically during software installation.

RUn below command to all servers and set same password.

passwd hacluster

High Availability software have been installed on to the system.

Step 4: Create and Configure Cluster

Note: Run steps 4 to step 7 on “host1” only.

We will create a new cluster with 3 servers.

we need to authorize all servers to create new cluster using below command. username will be “hacluster”

pcs cluster auth host1 host2 host3

Now create the cluster named osradar_cluster for all servers using below command.

pcs cluster setup --name osradar_cluster host1 host2 host3

Now start and enable all cluster services using below command.

pcs cluster start --all
pcs cluster enable --all

Now verify the cluster status.

pcs status cluster

Step 5: Disable STONITH and Ignore the Quorum Policy

Since we are running three node cluster and don’t have a fencing device. So we have to disable the STONITH setting but it is not recommended for production environment.

Disable STONITH and ignore Quorum policy using below commands.

pcs property set stonith-enabled=false
pcs property set no-quorum-policy=ignore

Then make sure stonith and the quorum policy are disabled.

pcs property list

Step 6: Add the Floating IP and Resources

Floating IP is the IP address that wil be migrated/moved automatically from one server to another server. Our floating ip address wil be 192.168.130.222 then we will add two resources, the Floating IP address resource with the name ‘virtual_ip’ and a new resource for the Nginx web server named ‘webserver’

Run below command to add floating IP address ‘virtual_ip’

pcs resource create virtual_ip ocf:heartbeat:IPaddr2 ip=192.168.130.222 cidr_netmask=32 op monitor interval=30s

Then run below command to add a new resource for the Nginx ‘webserver’

pcs resource create webserver ocf:heartbeat:nginx configfile=/etc/nginx/nginx.conf op monitor timeout="5s" interval="5s"

Now verify resources status and check that there is no error.

pcs status resources

Step 7: Add Constraint Rules to the Cluster

Now will configure High Availability Rules and resource constraint.

Set the collation constraint for webserver and virtual_ip resources with the score ‘INFINITY’ using below command.

pcs constraint colocation add webserver virtual_ip INFINITY

Now Set the ‘virtual_ip’ and ‘webserver’ resources always on same node servers using below command.

pcs constraint order virtual_ip then webserver

Next, stop and start the cluster

pcs cluster stop --all
pcs cluster start --all

Now again verify resources status.

pcs status resources

You can sse that virtual_ip and webserver resources started on the same server/node ‘host2’

Step 8: Configure Firewalld

Run below commands to add necessary services in firewalld.

firewall-cmd --add-service=high-availability --permanent 
firewall-cmd --add-service=http --permanent 
firewall-cmd --add-service=https --permanent

Now reload the firewalld

firewall-cmd --reload

Step 9: Testing

In this step, we will Test the node status, corosync members and status and then test the high-availability of the Nginx webserver by accessing the Floating IP address.

Test node status corosync members and corosync status using below command.

pcs status nodes
corosync-cmapctl | grep members
pcs status corosync

Finally, check the webserver High Availability. Open your web browser and type the Floating IP address 192.168.130.222

You will see the web page from the ‘host2’ server.

Now, stop the cluster on the ‘host2’ server using below command.

pcs cluster stop host2 --force

And check again the page and this time, you will see the page from the ‘host1’ server as below.

You have installed Nginx webserver High Availability with Pacemaker, Corosync, and Pcsd on CentOS 7 server successfully.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article