Scaling Your Linux Infrastructure – Load Balancing and High Availability

In today’s fast-paced world, businesses need their Linux infrastructure to be reliable, scalable, and highly available. Load balancing and high availability are two essential techniques used to achieve this goal. In this article, we’ll explore what load balancing and high availability mean and how they work. We’ll also discuss how you can implement load balancing and high availability in your Linux infrastructure.

Load balancing is the process of distributing incoming traffic across multiple servers. By using load balancing, you can ensure that no single server becomes overwhelmed with traffic, which can cause slow response times or even downtime. High availability, on the other hand, is the ability of a system to remain operational even if one or more components fail. By combining load balancing and high availability, you can ensure that your applications stay up and running, even in the face of hardware or software failures.

So, how can you implement load balancing and high availability in your Linux infrastructure? Here are some steps to get you started.

Step 1: Set Up Your Load Balancer

The first step in implementing load balancing is to set up your load balancer. A load balancer acts as a mediator between incoming client requests and your application servers. It distributes incoming traffic to multiple servers based on a set of rules, such as round-robin or least connections.

One popular open-source load balancer for Linux is HAProxy. To install HAProxy on your Linux server, you can use the following commands:

sudo apt-get update
sudo apt-get install haproxy

Once installed, you can configure HAProxy by editing its configuration file, located at /etc/haproxy/haproxy.cfg. Here’s an example configuration file that sets up round-robin load balancing between two servers:

frontend http-in
  bind *:80
  default_backend servers

backend servers
  balance roundrobin
  server server1 192.168.0.1:80 check
  server server2 192.168.0.2:80 check

This configuration file sets up a frontend that listens on port 80 for incoming HTTP traffic. The default_backend directive specifies that incoming traffic should be forwarded to the servers backend, which is configured to use round-robin load balancing between two servers, server1 and server2.

Step 2: Configure High Availability

Now that you have your load balancer set up, it’s time to configure high availability. One way to achieve high availability is to use a cluster of servers with shared storage. In this setup, if one server fails, the other servers can take over its workload and maintain service availability.

One popular open-source software for creating Linux clusters is Pacemaker. Pacemaker is a cluster resource manager that can monitor the health of your servers and take action in the event of a failure. To install Pacemaker on your servers, you can use the following commands:

sudo apt-get update
sudo apt-get install pacemaker corosync

Once installed, you can use the crm command to configure Pacemaker. Here’s an example configuration that sets up a two-node cluster with a shared file system:

$ sudo crm configure property stonith-enabled=false
$ sudo crm configure primitive nfs_server ocf:heartbeat:nfs \
        params path="/export" nfs_shared_infra="yes" nfs_no_notify="true"
$ sudo crm configure primitive virtual_ip ocf:heartbeat:IPaddr2 \
        params ip="192.168.1.100" cidr_netmask="24" nic="eth0"
$ sudo crm configure group nfs_cluster nfs_server virtual_ip
$ sudo crm configure colocation nfs_cluster_colocation inf: nfs_cluster virtual_ip
$ sudo crm configure order nfs_cluster_order inf: virtual_ip nfs_cluster

Verify the configuration:

$ sudo crm configure show

Start the cluster on all servers:

$ sudo systemctl start corosync
$ sudo systemctl start pacemaker

Step 3: Test Your Setup

With your load balancer and high availability configured, it’s time to test your setup. One way to do this is to simulate a failure of one of your servers and see if the other servers can take over its workload.

Also, to simulate a server failure, you can use the crm node standby command to put one of your servers in standby mode. This will cause Pacemaker to migrate the resources on that server to the other servers in the cluster.

Here’s an example of how to test your setup:

  • Verify that all servers are up and running:
$ crm status
============
Last updated: Tue Mar 14 12:38:05 2023
Stack: corosync
Current DC: node1 (version 1.1.24-1) - partition with quorum
2 nodes and 3 resources configured

Online: [ node1 node2 ]

Full list of resources:

 nfs_server       (ocf::heartbeat:nfs):       Started node1
 virtual_ip       (ocf::heartbeat:IPaddr2):  Started node1

Failed actions:
    nfs_server_start_0 on node2 'not running' (6): call=33, status=complete, exitreason='none',
        last-rc-change='Tue Mar 14 12:36:14 2023', queued=0ms, exec=56ms

Daemon Status:
  corosync: active/enabled
  pacemaker: active/enabled
  pcsd: active/enabled
  • Put one of the servers in standby mode:
$ crm node standby node2
  • Verify that the resources have migrated to the other servers:
$ crm status
============
Last updated: Tue Mar 14 12:39:53 2023
Stack: corosync
Current DC: node1 (version 1.1.24-1) - partition with quorum
2 nodes and 3 resources configured

Online: [ node1 ]
OFFLINE: [ node2 ]

Full list of resources:

 nfs_server       (ocf::heartbeat:nfs):       Started node1
 virtual_ip       (ocf::heartbeat:IPaddr2):  Started node1

Failed actions:
    nfs_server_start_0 on node2 'not running' (6): call=33, status=complete, exitreason='none',
        last-rc-change='Tue Mar 14 12:36:14 2023', queued=0ms, exec=56ms

Daemon Status:
  corosync: active/enabled
  pacemaker: active/enabled
  pcsd: active/enabled

As you can see, the resources have migrated to the remaining server, node1, and the virtual IP address is still reachable.

So, implementing load balancing and high availability in your Linux infrastructure is a complex task that requires careful planning and configuration. However, by following the steps outlined in this article, you can set up a robust and scalable infrastructure that can handle high traffic and maintain service availability.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top