{"id":7240,"date":"2018-03-01T15:45:04","date_gmt":"2018-03-01T07:45:04","guid":{"rendered":"http:\/\/rmohan.com\/?p=7240"},"modified":"2018-03-01T15:45:04","modified_gmt":"2018-03-01T07:45:04","slug":"how-to-set-up-nginx-high-availability-with-pacemaker-and-corosync-on-centos-7","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7240","title":{"rendered":"How to Set up Nginx High Availability with Pacemaker and Corosync on CentOS 7"},"content":{"rendered":"<p><strong>How to Set up Nginx High Availability with Pacemaker and Corosync on CentOS 7<\/strong><\/p>\n<p>We will create the Active-Passive Cluster or Failover-cluster Nginx web server using Pacemaker on a CentOS 7 system.<br \/>\nPacemaker is an open source cluster manager software that achieves maximum high availability of your services. It&#8217;s an advanced and scalable HA cluster manager distributed by ClusterLabs.<br \/>\nCorosync Cluster Engine is an open source project derived from the OpenAIS project under new BSD License. It&#8217;s a group communication system with additional features for implementing High Availability within applications.<br \/>\nThere are some applications for the Pacemaker interfaces. Pcsd is one of the Pacemaker command line interface and GUI for managing the Pacemaker. We can create, configure, or add a new node to the cluster with the pcsd command pcs.<br \/>\nPrerequisites<\/p>\n<p>[root@clusterserver1 ~]# cat \/etc\/hosts<br \/>\n127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4<br \/>\n::1 localhost localhost.localdomain localhost6 localhost6.localdomain6<br \/>\n192.168.1.20 clusterserver1.rmohan.com clusterserver1<br \/>\n192.168.1.21 clusterserver2.rmohan.com clusterserver2<br \/>\n192.168.1.22 clusterserver3.rmohan.com clusterserver3<br \/>\n[root@clusterserver1 ~]#<\/p>\n<p>Floating IP Address 192.168.1.25<br \/>\nRoot Privileges<\/p>\n<p>Now test the hosts&#8217; mapping configuration.<\/p>\n<p>ping -c 3 clusterserver1<br \/>\nping -c 3 clusterserver2<\/p>\n<p>Install Epel Repository and Nginx<br \/>\nIn this step, we will install the epel repository and then install the Nginx web server. EPEL or Extra Packages for Enterprise Linux repository is needed for installing Nginx packages.<\/p>\n<p>Install EPEL Repository using the following\u00a0yum command.<\/p>\n<p>yum -y install epel-release<\/p>\n<p>Now install Nginx web server from the EPEL repository.<\/p>\n<p>yum -y install nginx<\/p>\n<p>systemctl start nginx<br \/>\nsystemctl enable nginx<br \/>\nsystemctl status nginx<\/p>\n<p>#Run Command on &#8216;clusterserver1&#8217;<br \/>\necho &#8216;&lt;h1&gt;clusterserver1 &#8211; TEST SERVER1&lt;\/h1&gt;&#8217; &gt; \/usr\/share\/nginx\/html\/index.html<\/p>\n<p>#Run Command on &#8216;clusterserver2&#8217;<br \/>\necho &#8216;&lt;h1&gt;clusterserver2 &#8211; TEST SERVER2&lt;\/h1&gt;&#8217; &gt; \/usr\/share\/nginx\/html\/index.html<\/p>\n<p>#Run Command on &#8216;clusterserver3&#8217;<br \/>\necho &#8216;&lt;h1&gt;clusterserver3 &#8211; TEST SERVER3&lt;\/h1&gt;&#8217; &gt; \/usr\/share\/nginx\/html\/index.html<\/p>\n<p>Install and configure Pacemaker, Corosync, and Pcsd<br \/>\nPacemaker, Corosync, and Pcsd are available in\u00a0the default system repository. So they\u00a0all can be installed from the CentOS repository using the following\u00a0yum command.<br \/>\nyum -y install corosync pacemaker pcs<br \/>\nAfter\u00a0the installation has been completed, enable all services to launch automatically at system boot\u00a0using the systemctl commands below.<br \/>\nsystemctl enable pcsd<br \/>\nsystemctl enable corosync<br \/>\nsystemctl enable pacemaker<br \/>\nNow start the pcsd Pacemaker command line interface on all servers.<br \/>\nsystemctl start pcsd<br \/>\nNext, create a new password for &#8216;hacluster&#8217; user and use the same password for all servers. This user has been created automatically during software installation.<br \/>\nHere&#8217;s how you configure a password for the &#8216;hacluster&#8217; user.<br \/>\npasswd hacluster<br \/>\nEnter new password:<br \/>\nHigh Availability software stack Pacemaker, Corosync, and Pcsd are installed on to the system.<\/p>\n<p>Create and Configure the Cluster<\/p>\n<p>In this step, we will create a new cluster with 3 centos servers. Then configure\u00a0the Floating IP address and add\u00a0new Nginx resources.<br \/>\nTo create the cluster, we need to authorize all servers using the pcs command and the hacluster user.<br \/>\nAuthorize all servers with the pcs command and hacluster user and password.<br \/>\npcs cluster auth clusterserver1 clusterserver2 clusterserver3<br \/>\nUsername: hacluster<br \/>\nPassword: test123<\/p>\n<p>[root@clusterserver1 ~]# pcs cluster auth clusterserver1 clusterserver2 clusterserver3<br \/>\nUsername: hacluster<br \/>\nPassword:<br \/>\nclusterserver3: Authorized<br \/>\nclusterserver2: Authorized<br \/>\nclusterserver1: Authorized<br \/>\n[root@clusterserver1 ~]#<\/p>\n<p>Now it&#8217;s time set up the cluster. Define the cluster name and all servers that will be part of the cluster.<\/p>\n<p>pcs cluster setup &#8211;name mohan_cluster clusterserver1 clusterserver2 clusterserver3<\/p>\n<p>[root@clusterserver1 ~]# pcs cluster setup &#8211;name mohan_cluster clusterserver1 clusterserver2 clusterserver3<br \/>\nDestroying cluster on nodes: clusterserver1, clusterserver2, clusterserver3&#8230;<br \/>\nclusterserver1: Stopping Cluster (pacemaker)&#8230;<br \/>\nclusterserver2: Stopping Cluster (pacemaker)&#8230;<br \/>\nclusterserver3: Stopping Cluster (pacemaker)&#8230;<br \/>\nclusterserver1: Successfully destroyed cluster<br \/>\nclusterserver3: Successfully destroyed cluster<br \/>\nclusterserver2: Successfully destroyed cluster<\/p>\n<p>Sending &#8216;pacemaker_remote authkey&#8217; to &#8216;clusterserver1&#8217;, &#8216;clusterserver2&#8217;, &#8216;clusterserver3&#8217;<br \/>\nclusterserver3: successful distribution of the file &#8216;pacemaker_remote authkey&#8217;<br \/>\nclusterserver1: successful distribution of the file &#8216;pacemaker_remote authkey&#8217;<br \/>\nclusterserver2: successful distribution of the file &#8216;pacemaker_remote authkey&#8217;<br \/>\nSending cluster config files to the nodes&#8230;<br \/>\nclusterserver1: Succeeded<br \/>\nclusterserver2: Succeeded<br \/>\nclusterserver3: Succeeded<\/p>\n<p>Synchronizing pcsd certificates on nodes clusterserver1, clusterserver2, clusterserver3&#8230;<br \/>\nclusterserver3: Success<br \/>\nclusterserver2: Success<br \/>\nclusterserver1: Success<br \/>\nRestarting pcsd on the nodes in order to reload the certificates&#8230;<br \/>\nclusterserver3: Success<br \/>\nclusterserver2: Success<\/p>\n<p>[root@clusterserver1 ~]# pcs cluster start &#8211;all<br \/>\nclusterserver3: Starting Cluster&#8230;<br \/>\nclusterserver1: Starting Cluster&#8230;<br \/>\nclusterserver2: Starting Cluster&#8230;<br \/>\n[root@clusterserver1 ~]#<\/p>\n<p>[root@clusterserver1 ~]# pcs status cluster<br \/>\nCluster Status:<br \/>\nStack: unknown<br \/>\nCurrent DC: NONE<br \/>\nLast updated: Fri Mar 10 03:56:45 2017<br \/>\nLast change: Fri Mar 10 03:56:26 2017 by hacluster via crmd on clusterserver1<br \/>\n3 nodes configured<br \/>\n0 resources configured<\/p>\n<p>PCSD Status:<br \/>\nclusterserver1: Online<br \/>\nclusterserver2: Online<br \/>\nclusterserver3: Online<br \/>\n[root@clusterserver1 ~]#<\/p>\n<p>Disable STONITH and Ignore the Quorum Policy<br \/>\nSince we&#8217;re not using the fencing device, we will disable the STONITH. STONITH or Shoot The Other Node<br \/>\nIn The Head is the fencing implementation on Pacemaker. If you&#8217;re in production, it&#8217;s better to enable STONITH.<br \/>\nDisable STONITH with the following pcs command.<\/p>\n<p>pcs property set stonith-enabled=false<br \/>\nNext, for the Quorum policy, ignore it.<br \/>\npcs property set no-quorum-policy=ignore<br \/>\nCheck the property list and make sure stonith and the quorum policy are disabled.<\/p>\n<p>pcs property list<\/p>\n<p>[root@clusterserver1 ~]# pcs property list<br \/>\nCluster Properties:<br \/>\ncluster-infrastructure: corosync<br \/>\ncluster-name: mohan_cluster<br \/>\ndc-version: 1.1.16-12.el7_4.5-94ff4df<br \/>\nhave-watchdog: false<br \/>\nno-quorum-policy: ignore<br \/>\nstonith-enabled: false<br \/>\n[root@clusterserver1 ~]#<\/p>\n<p>The STONITH and Quorum Policy is disabled.<\/p>\n<p>Add the Floating-IP and Resources<br \/>\nFloating IP is the IP address that can be migrated\/moved automatically from one server to another server in\u00a0the same Data Center. And we&#8217;ve already defined the floating IP address for the Pacemaker High-Availability to be &#8216;10.0.15.15&#8217;. Now we want to add two resources, the Floating IP address resource with the name &#8216;virtual_ip&#8217; and a new resource for the Nginx web server named &#8216;webserver&#8217;.<br \/>\nAdd the new resource floating IP address &#8216;virtual_ip&#8217; using the\u00a0pcs command as shown below.<\/p>\n<p>pcs resource create virtual_ip ocf:heartbeat:IPaddr2 ip=192.168.1.25 cidr_netmask=32 op monitor interval=30s<\/p>\n<p>Next, add a new resource for the Nginx &#8216;webserver&#8217;.<\/p>\n<p>pcs resource create webserver ocf:heartbeat:nginx configfile=\/etc\/nginx\/nginx.conf op monitor timeout=&#8221;5s&#8221; interval=&#8221;5s&#8221;<\/p>\n<p>Make sure you got no error result, then check the resources available.<\/p>\n<p>pcs status<br \/>\npcs status resources<\/p>\n<p>You will see two resources &#8216;virtual_ip&#8217; and a &#8216;webserver&#8217;. New resources for the Floating IP and Nginx web server have been added.<\/p>\n<p>Add Constraint Rules to the Cluster<\/p>\n<p>In this step, we will setup High Availability Rules, and will setup resource constraint with the pcs command line interface.<br \/>\nSet the collation constraint for webserver and virtual_ip resources with score &#8216;INFINITY&#8217;.<br \/>\nAlso, setup the webserver and virtual_ip resources as\u00a0same on all server nodes.<\/p>\n<p>pcs constraint colocation add webserver virtual_ip INFINITY<\/p>\n<p>Set the &#8216;virtual_ip&#8217; and &#8216;webserver&#8217; resources always on same node servers.<\/p>\n<p>pcs constraint order virtual_ip then the webserver<\/p>\n<p>pcs constraint colocation add webserver virtual_ip INFINITY<\/p>\n<p>Next, stop the cluster and then start again.<\/p>\n<p>pcs cluster stop &#8211;all<br \/>\npcs cluster start &#8211;all<\/p>\n<p>[root@clusterserver1 ~]# pcs cluster stop &#8211;all<br \/>\nclusterserver1: Stopping Cluster (pacemaker)&#8230;<br \/>\nclusterserver3: Stopping Cluster (pacemaker)&#8230;<br \/>\nclusterserver2: Stopping Cluster (pacemaker)&#8230;<br \/>\nclusterserver3: Stopping Cluster (corosync)&#8230;<br \/>\nclusterserver1: Stopping Cluster (corosync)&#8230;<br \/>\nclusterserver2: Stopping Cluster (corosync)&#8230;<br \/>\n[root@clusterserver1 ~]# pcs cluster start &#8211;all<br \/>\nclusterserver1: Starting Cluster&#8230;<br \/>\nclusterserver2: Starting Cluster&#8230;<br \/>\nclusterserver3: Starting Cluster&#8230;<br \/>\n[root@clusterserver1 ~]#<br \/>\n[root@clusterserver1 ~]#<br \/>\n[root@clusterserver1 ~]#<\/p>\n<p>Testing<br \/>\nIn this step, we&#8217;re gonna do some test for the cluster. Test the node status (&#8216;Online&#8217; or &#8216;Offline&#8217;), test the corosync members and status, and then test the high-availability of the Nginx webserver by accessing the Floating IP address.<br \/>\nTest node status with the following command.<\/p>\n<p>[root@clusterserver1 ~]# pcs status nodes<br \/>\nPacemaker Nodes:<br \/>\nOnline: clusterserver1<br \/>\nStandby:<br \/>\nMaintenance:<br \/>\nOffline: clusterserver2 clusterserver3<br \/>\nPacemaker Remote Nodes:<br \/>\nOnline:<br \/>\nStandby:<br \/>\nMaintenance:<br \/>\nOffline:<br \/>\n[root@clusterserver1 ~]#<\/p>\n<p>Test the corosync members.<br \/>\ncorosync-cmapctl | grep members<br \/>\nYou will get Corosync members IP address<\/p>\n<p>[root@clusterserver1 ~]# corosync-cmapctl | grep members<br \/>\nruntime.totem.pg.mrp.srp.members.1.config_version (u64) = 0<br \/>\nruntime.totem.pg.mrp.srp.members.1.ip (str) = r(0) ip(192.168.1.20)<br \/>\nruntime.totem.pg.mrp.srp.members.1.join_count (u32) = 1<br \/>\nruntime.totem.pg.mrp.srp.members.1.status (str) = joined<\/p>\n<p>[root@clusterserver1 ~]# pcs status<br \/>\nCluster name: mohan_cluster<br \/>\nStack: corosync<br \/>\nCurrent DC: clusterserver1 (version 1.1.16-12.el7_4.5-94ff4df) &#8211; partition WITHOUT quorum<br \/>\nLast updated: Fri Mar 10 04:12:59 2017<br \/>\nLast change: Fri Mar 10 04:11:59 2017 by root via cibadmin on clusterserver1<\/p>\n<p>3 nodes configured<br \/>\n2 resources configured<\/p>\n<p>Online: [ clusterserver1 ]<br \/>\nOFFLINE: [ clusterserver2 clusterserver3 ]<\/p>\n<p>Full list of resources:<\/p>\n<p>virtual_ip (ocf::heartbeat:IPaddr2): Started clusterserver1<br \/>\nwebserver (ocf::heartbeat:nginx): Started clusterserver1<\/p>\n<p>Daemon Status:<br \/>\ncorosync: active\/enabled<br \/>\npacemaker: active\/enabled<br \/>\npcsd: active\/enabled<br \/>\n[root@clusterserver1 ~]#<br \/>\n[root@clusterserver1 ~]#<br \/>\n[root@clusterserver1 ~]#<br \/>\n[root@clusterserver1 ~]#<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Set up Nginx High Availability with Pacemaker and Corosync on CentOS 7<\/p>\n<p>We will create the Active-Passive Cluster or Failover-cluster 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. It&#8217;s an advanced and scalable HA cluster [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73,70],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7240"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7240"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7240\/revisions"}],"predecessor-version":[{"id":7241,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7240\/revisions\/7241"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}