High availability is crucial for critical systems that should remain accessible even in the face of hardware or software failures. Pacemaker and Corosync are open-source tools that allow you to create a high-availability cluster on your Ubuntu servers. In this tutorial, I will guide you through the process of setting up a high-availability cluster using Pacemaker and Corosync on Ubuntu, ensuring that your services remain available with minimal downtime.
Section 1: Understanding High Availability Clustering
Before diving into the configuration, let’s briefly understand what high availability clustering is:
High availability clustering involves grouping multiple servers (nodes) together to provide redundancy for critical services. If one node fails, another takes over seamlessly, ensuring continuous service availability.
Section 2: Setting Up Pacemaker and Corosync
Step 1: Install Pacemaker and Corosync On each node, install the Pacemaker and Corosync packages:
sudo apt update
sudo apt install pacemaker corosync
Step 2: Configure Corosync Edit the Corosync configuration file on each node:
sudo nano /etc/corosync/corosync.conf
Here’s a basic configuration example for a two-node cluster:
totem {
version: 2
secauth: off
cluster_name: my_cluster
transport: udpu
}
nodelist {
node {
ring0_addr: node1_IP
nodeid: 1
}
node {
ring0_addr: node2_IP
nodeid: 2
}
}
quorum {
provider: corosync_votequorum
}
Replace node1_IP
and node2_IP
with the actual IP addresses of your nodes.
Step 3: Start Corosync Start the Corosync service on each node:
sudo systemctl start corosync
Step 4: Enable Corosync at Boot Ensure Corosync starts automatically at boot:
sudo systemctl enable corosync
Section 3: Configuring Pacemaker
Step 5: Start Pacemaker Start the Pacemaker service on each node:
sudo systemctl start pacemaker
Step 6: Enable Pacemaker at Boot Enable Pacemaker to start automatically at boot:
sudo systemctl enable pacemaker
Section 4: Creating a Resource
Step 7: Create a Resource Agent Pacemaker manages resources using resource agents. To create a simple resource agent for a virtual IP (VIP) address, create a file like vip.sh
:
sudo nano /usr/local/bin/vip.sh
Add the following content and make the script executable:
#!/bin/bash
/sbin/ifconfig eth0:0 $1 netmask 255.255.255.0 up
sudo chmod +x /usr/local/bin/vip.sh
Step 8: Create a Resource Now, create a Pacemaker resource for the VIP. On one of the nodes, run:
sudo crm configure primitive vip ocf:heartbeat:IPaddr2 params ip="VIP_IP" nic="eth0" cidr_netmask="24" op monitor interval="10s"
Replace VIP_IP
with the virtual IP address you want to use.
Step 9: Create a Resource Group Create a resource group that includes the VIP resource:
sudo crm configure group vip_group vip
Section 5: Testing Failover
Step 10: Simulate Node Failure To test the cluster, simulate a node failure by stopping the Corosync service on one of the nodes:
sudo systemctl stop corosync
Check the status of the cluster on the remaining node:
sudo crm status
You should see that the VIP has moved to the surviving node.
Section 6: Additional Configuration
To configure more resources, fencing, or complex constraints, refer to the Pacemaker documentation and tutorials. Pacemaker and Corosync offer a wide range of features for building highly available systems.
WooHoo! You’ve successfully set up a high-availability cluster on Ubuntu using Pacemaker and Corosync. Your services are now resilient to node failures, providing uninterrupted availability for critical applications.
I hope you have found this article useful. If so, please consider following me here and on social media. Also, if you need 24 x 7 linux help, check out my company: https://techguys2go.com
Cheers!
First, it is always a good idea to update and upgrade all system packages to the latest version. Update all of them by running the following command on all servers. Once all the packages are updated, restart all servers to apply the changes. Before you start, install Apache web server package on all three servers. Do it by running the following command. After installing the Apache server package, you need to start and enable the Apache service on all servers. Then verify the status of Apache service using the following command. You see the Apache status on the following screen. At this point, Apache web server package is installed on all servers. Now proceed to configure the backend servers. Next, you need to create a sample HTML page and Apache virtual host configuration file on the first Apache server. First, create a sample HTML file with the following command. Add the following code. Save and close the file after you are done. Next, create an Apache virtual host configuration file. Add the following configurations. Save and close the file then restart the Apache service to apply the changes. Now, go to the second server and create a sample HTML file using the following command. Add the following HTML code. Save and close the file when you are finished. Next, create an Apache virtual host configuration file. Add the following configurations. Save and close the file then restart the Apache service to implement the changes. At this point, both Apache backend server is configured to server sample HTML page. Now, you need to configure the third server as a load balancing server to forward all traffic to both backend web servers. Firstly, enable proxy modules to the load balancing server. You enable all of them with the following command. Next, restart the Apache service to apply the changes. Now verify all proxy modules using the following command. You see all modules in the following screen. Next, create an Apache configuration file for load balancing. Add the following configurations. Save and close the file then restart the Apache service to apply the configuration. At this point, your Apache load balancing server is installed and configured to forward all traffic to both backend servers. Now, its time to verify the load balancer. Open your web browser and access the load balancer using the URL http://balancer.example.com. You should see your first backend server page. Now wait for some time and refresh the page. This time, you should see the sample HTML page of your second backend server. Thank you for reading Apache Load Balancing: How to Use Apache for Load Balancing. We shall conclude. In this load balancing guide, we showed you what is load balancing, and we have created two backend web servers. We have then setup a load balancing using the Apache web server to forward traffic to both backend web servers. Load Balancers (hardware and software-based) are mostly used to evenly distribute the workload across different servers. The three different kinds of load balancers are DNS Round Robin, L3/L4 (suited for the IP and TCP layers), as well as L7 Load Balancer. These help maximize server capacity, improve user experience, and save time.Update System Packages
apt update -y
apt upgrade -y
reboot
Install Apache Web Server
apt install apache2 -y
systemctl start apache2
systemctl enable apache2
systemctl status apache2
Configure First Apache Backend Server
nano /var/www/html/web1.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<title>Apache Web Server1</title>
<h2>This is Apache Web Server 1 Page!</h2>
</html>
nano /etc/apache2/sites-enabled/web1.conf
<VirtualHost *:80>
ServerName web1.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
DirectoryIndex web1.html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
systemctl restart apache2
Configure Second Apache Backend Server
nano /var/www/html/web2.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<title>Apache Web Server2</title>
<h2>This is Apache Web Server 2 Page!</h2>
</html>
nano /etc/apache2/sites-enabled/web2.conf
<VirtualHost *:80>
ServerName web2.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
DirectoryIndex web2.html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
systemctl restart apache2
Create an Apache Load Balancer
a2enmod proxy
a2enmod proxy_http
a2enmod proxy_balancer
a2enmod lbmethod_byrequests
systemctl restart apache2
apachectl -M | grep proxy
nano /etc/apache2/sites-enabled/loadbalancer.conf
<VirtualHost *:80>
ServerName balancer.example.com
<Proxy balancer://webserver>
BalancerMember http://web1.example.com
BalancerMember http://web2.example.com
ProxySet stickysession=ROUTEID
</Proxy>
ProxyPreserveHost On
ProxyPass / balancer://webserver/
ProxyPassReverse / balancer://webserver/
</VirtualHost>
systemctl restart apache2
Verify Apache Load Balancing
Apache Load Balancing: How to Use Apache for Load Balancing Conclusion