How to Build a Two-Node Nginx HA Cluster on CentOS 7 with Corosync & Pacemaker
This guide walks through setting up a highly available two‑node Nginx service on CentOS 7 using Corosync and Pacemaker, covering topology, environment preparation, host synchronization, HA component installation, NFS sharing, resource configuration with crmsh, and testing the failover behavior.
Outline
Preface
Experiment Introduction
Experiment Topology
Experiment Environment
Experiment Steps
Conclusion
Preface
These days I am studying high‑availability clusters and will share several experiments; this series will contain about five articles.
Experiment Introduction
This experiment is simple: implement two‑node Nginx high availability on CentOS 7 using corosync + pacemaker.
Experiment Topology
Experiment Environment
Host IP Purpose node1.anyisalin.com 172.16.1.2 Web service, HA node node2.anyisalin.com 172.16.1.3 Web service, HA node nfs.anyisalin.com 172.16.1.4 Provides NFS service for website files
Note: SELinux and iptables are disabled on all hosts in this experiment.Experiment Steps
Preparation
All HA nodes must trust each other, have consistent hostname resolution, and synchronized time.
Configure hosts file synchronizationConfigure trustTime synchronizationInstall HA Cluster Components
Since RHEL 6.4, the pcs tool can manage the full lifecycle of a cluster. First install pcs on both nodes and generate configuration files.
[root@node1 ~]# yum install pcs -y # node1 install pcs
[root@node1 ~]# ssh node2.anyisalin.com 'yum install pcs -y' # node2 install pcs
[root@node1 ~]# echo passwd | passwd --stdin hacluster # set hacluster password
[root@node1 ~]# ssh node2.anyisalin.com "echo passwd | passwd --stdin hacluster" # set password on node2
[root@node1 ~]# systemctl start pcsd ; ssh node2.anyisalin.com "systemctl start pcsd" # start pcsd
[root@node1 ~]# pcs cluster auth node1.anyisalin.com node2.anyisalin.com -u hacluster # authenticate
Password:
node1.anyisalin.com: Authorized
node2.anyisalin.com: Authorized
[root@node1 ~]# pcs cluster setup --name mycluster node1.anyisalin.com node2.anyisalin.com # create cluster
[root@node1 ~]# pcs cluster start --all # start clusterInstall Nginx and Configure NFS
# Install nginx
[root@node1 ~]# yum install nginx -y # note: requires epel repo
[root@node1 ~]# ssh node2.anyisalin.com "yum install nginx -y"
[root@node1 ~]# systemctl enable nginx # enable autostart
[root@node1 ~]# ssh node2.anyisalin.com "systemctl enable nginx"
# Configure NFS
[root@nfs ~]# mkdir /www/htdocs -pv
[root@nfs ~]# echo "<h1>This is NFS on 172.16.1.4</h1>" > /www/htdocs/index.html
[root@nfs ~]# vim /etc/exports
/www/htdocs 172.16.1.0/24(ro)
[root@nfs ~]# systemctl start rpcbind.service
[root@nfs ~]# systemctl start nfs-server.service
# Test NFS
[root@node1 ~]# mount -t nfs 172.16.1.4:/www/htdocs /usr/share/nginx/html/
[root@node1 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
172.16.1.4:/www/htdocs 52403200 1134336 51268864 3% /usr/share/nginx/html
[root@node1 ~]# umount /usr/share/nginx/html/Configure Cluster Resources with crmsh
Although pcs can install the cluster, configuring resources with pcs is cumbersome; crmsh offers a more convenient syntax.
# Install crmsh
[root@node1 ~]# yum install crmsh-2.1.4-1.1.x86_64.rpm
# Configure resources
[root@node1 ~]# crm configure
crm(live)configure# primitive webip ocf:heartbeat:IPaddr params ip=172.16.1.8
crm(live)configure# primitive nginx systemd:nginx
crm(live)configure# primitive webstore ocf:heartbeat:Filesystem params device="172.16.1.4:/www/htdocs" directory="/usr/share/nginx/html" fstype="nfs" op start timeout=60s op stop timeout=60s op monitor interval=20s timeout=40s
# Define resource group
crm(live)configure# group webservice webip webstore nginx
crm(live)configure# verify
crm(live)configure# property stonith-enabled=false
crm(live)configure# commitTesting
Conclusion
This article demonstrates a simple HA cluster implementation; the key is understanding HA principles, which are covered in a separate blog post.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
