Skip to content

PRATANGGA/HighAvailability

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

15 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

High Availability

What is High Availability?

High Availability (HA) refers to systems that are designed to be operational and accessible for a high percentage of time, minimizing downtime and ensuring continuous service availability. This is often achieved through redundancy, failover mechanisms, and load balancing.

Key Components in this setup

  • Apache (Web Server) Acts as the backend server that processes HTTP requests and serves web content. In this setup, multiple Apache web servers are used to serve the same content, ensuring that if one server fails, another can continue serving users without disruption.
  • Nginx (Load Balancer)
    Sits in front of the web servers and distributes incoming client requests across multiple backend Apache servers. This helps balance the load, prevent any single server from being overwhelmed, and improves the responsiveness of the web service
  • Keepalived (Failover Management) Works together with Nginx to provide fault tolerance. It manages a Virtual IP Address (VIP) that floats between two Nginx instances (Master and Backup). If the master load balancer fails, Keepalived promotes the backup node to master and assigns the VIP to it, ensuring uninterrupted access for users.

Objectives of This Module?

In this module, we will implement a High Availability Web Server Architecture that ensures reliable and uninterrupted web service delivery through the combination of load balancing, redundancy, and failover mechanisms.

Network Topology

The following diagram illustrates the high-level architecture of the High Availability Web Server setup Topology

Description

  • Client sends request to the Virtual IP (VIP) managed by Keepalived.

  • The VIP routes traffic to one of the Load Balancer nodes (Nginx):

    • If Load Balancer Master is available, it handles the traffic.

    • If the Master fails, Keepalived shifts the VIP to the Backup node.

  • Nginx forwards the request to one of the two Apache Web Servers, which serve the actual web content.

  • Keepalived ensures failover between the two Load Balancer nodes without service disruption.


Lab Environment

No Virtual Machine Spesifikasi NAT Host-Only Internal Network
1 Load Balancer Master 1 vCPU, 1 GB RAM DHCP 192.168.56.50/24 10.10.10.50/24
2 Load Balancer Slave 1 vCPU, 1 GB RAM DHCP 192.168.56.51/24 10.10.10.51/24
3 Web Server 1 1 vCPU, 1 GB RAM DHCP 192.168.56.52/24 10.10.10.52/24
4 Web Server 2 1 vCPU, 1 GB RAM DHCP 192.168.56.53/24 10.10.10.53/24

Installation and Configuration Steps

1. Configure VM Network Interfaces

Each VM must be configured with three interfaces: NAT, Host-Only, and Internal Network. Edit file:

sudo vim /etc/netplan/50-cloud-init.yaml

Example configuration for Load Balancer Master:

# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
  ethernets:
    enp0s3: # NAT Interface (DHCP)
      dhcp4: true
    enp0s8: # Host-Only Interface (Static IP)
      addresses:
        - 192.168.56.50/24
      dhcp4: false
    enp0s9: # Internal Network Interface (Static IP)
      addresses:
        - 10.10.10.50/24
      dhcp4: false
  version: 2

Apply the configuration changes

sudo netplan apply

๐Ÿ“ Repeat this step on each VM, adjusting the IP addresses based on the table above.

2. Set Hostname for Each VM

Assign a unique hostname to each virtual machine based on its role, to help easily identify them

# For Load Balancer Master
sudo hostnamectl set-hostname lb-master

# For Load Balancer Slave
sudo hostnamectl set-hostname lb-slave

# For Web Server 1
sudo hostnamectl set-hostname web1

# For Web Server 2
sudo hostnamectl set-hostname web2

๐Ÿ” Reboot each VM so the terminal prompt updates to reflect the new hostname

sudo reboot

3. Update /etc/hosts File

To allow all VMs to resolve each other's hostnames, edit the /etc/hosts file on each VM:

10.10.10.50   lb-master
10.10.10.51   lb-slave
10.10.10.52   web1
10.10.10.53   web2

Save and exit. This will allow you to ping and communicate with each node using its hostname.

4. Apache Installation on Web Servers

These steps should be executed on both Web Server 1 and Web Server 2

Update System Packages

sudo apt update -y

Install Apache Web Server

sudo apt install apache2 -y

Enable Apache to Start Apache service

sudo systemctl enable --now apache2

check Apache Service Status

sudo systemctl status apache2

5. Instal PHP (Optional for Dynamic Content)

To test the Apache server with PHP, install the following packages

sudo apt install php libapache2-mod-php -y

6. Prepare Sample Web Content

Create a new directory for the website

sudo mkdir -p /var/www/website

Create an index.php file with sample content

sudo vim /var/www/website/index.php

Example content for Web Server 1

<h1>WEB 1</h1>
<?php phpinfo(); ?>

on Web Server 2, cahnge the heading to "WEB 2" to help identify which server is responding during load balancing tests.

7. Configure Apache Virtual Host

Create a new virtual host file

sudo vim /etc/apache2/sites-available/website.conf

Add following Configuration

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/website
    ServerName dinus.local

    <Directory /var/www/website>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Disable the default configuration

sudo a2dissite 000-default.conf

Enable the custom virtual host

sudo a2ensite website.conf

Reload Apache to apply changes

sudo systemctl reload apache2

8. Install and Configure Nginx & Keepalived on Load Balancer nodes

To ensure load balancing and automatic failover, we will install Nginx and Keepalived on both Load Balancer Master and Load Balancer Slave nodes.

Install Required Packages

Update the package list

sudo apt update -y

Install Nginx

sudo apt install nginx -y

Install Keepalived

sudo apt install keepalived -y

Enable and start both services

sudo systemctl enable --now nginx && sudo systemctl enable --now keepalived

9. COnfigure Nginx as Load Balancer

Create a new configuration file on both Load Balancer (Load Balancer Master and Load Balancer Slave) nodes

sudo vim /etc/nginx/conf.d/loadbalancer.conf

Add the following configuration to set up Nginx as a load balancer

upstream web_backend {
    server 10.10.10.52; # Web Server 1
    server 10.10.10.53; # Web Server 2
}

server {
    listen 80;
    server_name dinus.local;

    location / {
        proxy_pass http://web_backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
}

Check Nginx syntax for any errors

sudo nginx -t

Reload Nginx to apply the new configuration

sudo systemctl reload nginx

10. Configure Keepalived for Failover

Now we will configure Keepalived on both Load Balancer nodes to ensure automatic failover

On the Load Balancer Master (lb-master)

edit the Keepalived configuration file

sudo vim /etc/keepalived/keepalived.conf

Paste the following configuration

vrrp_instance VI_1 {
 interface enp0s8
 state MASTER
 priority 500
 advert_int 1
 unicast_src_ip 192.168.56.50
 unicast_peer {
 192.168.56.51
 }
 virtual_router_id 33
 virtual_ipaddress {
 192.168.56.100/24
 }
 authentication {
 auth_type PASS
 auth_pass udinus
 }
}

On the Load Balancer Slave (lb-slave)

edit the Keepalived configuration file

vrrp_instance VI_1 {
 interface enp0s8
 state BACKUP
 priority 100
 advert_int 1
 unicast_src_ip 192.168.56.51
 unicast_peer {
 192.168.56.50
 }
 virtual_router_id 33
 virtual_ipaddress {
 192.168.56.100/24
 }
 authentication {
 auth_type PASS
 auth_pass udinus
 }
}

After configuring both nodes,restart the Keepalived service on both Load Balancer nodes

sudo systemctl restart keepalived

Verify that the Virtual IP (VIP) is assigned to the Master node

ip a

11. Testing the High Availability Setup

Now that everything is configured, we can test the High Availability setup

Access the Web Application

  • open a web browser and navigate to the Virtual IP address (192.168.56.100) web

  • Refresh the page several times. You should see alternating content like WEB 1 and WEB 2, which confirms that Nginx is distributing traffic between both backend web servers. web

  • try power off the Load Balancer Master node (lb-master) and refresh the page again. The VIP should automatically switch to the Load Balancer Slave node (lb-slave), and you should still be able to access the web application without interruption. Power Off

  • Or you can also test by stopping the Nginx service on the Master node and checking if the Slave node takes over.

sudo systemctl stop nginx
  • After stopping the Nginx service on the Master node, refresh the web page. The VIP should automatically switch to the Slave node, and you should still be able to access the web application without interruption. final

Conclusion

In this module, we successfully implemented a High Availability Web Server Architecture using Apache, Nginx, and Keepalived. This setup ensures that web services remain available even in the event of server failures, providing a robust solution for maintaining continuous service delivery.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors