# **Keepalived & VRRP Explained (For Beginners)**
 
If you're not familiar with **Keepalived** or **VRRP**, let me break it down in simple terms.
 
---
 
## **1. What is VRRP?**
**VRRP (Virtual Router Redundancy Protocol)** is a protocol that allows multiple routers (or servers) to share the same **virtual IP (VIP)**. If the **primary router fails**, a **backup router takes 
over** automatically.
 
### **Key Concepts**
- **Master (Active Router)**: Handles traffic for the VIP.
- **Backup (Standby Router)**: Takes over if the master fails.
- **VRRP Instance**: A configuration that defines how failover works.
 
---
 
## **2. What is Keepalived?**
**Keepalived** is a software that implements **VRRP** and provides **high availability (HA)** for servers.
 
### **Why Use Keepalived?**
- **Automatic failover**: If a server crashes, another takes over.
- **Load balancing**: Distributes traffic across multiple servers.
- **Health checks**: Monitors services and switches roles if needed.
 
---
 
## **3. How Does Keepalived Work?**
1. **Multiple servers** share the same **virtual IP (VIP)**.
2. One server is the **master** (active), others are **backups** (standby).
3. If the master fails, a backup **automatically becomes the new master**.
 
### **Example Scenario**
- **Server A (Master)**: Handles traffic for `192.168.1.100`.
- **Server B (Backup)**: Waits in standby mode.
- If **Server A crashes**, **Server B takes over** the VIP.
 
---
 
## **4. When Should You Use Keepalived?**
- **Web servers (Nginx, Apache)** for failover.
- **Database clusters (MySQL, PostgreSQL)** for high availability.
- **Load balancers (HAProxy)** to avoid downtime.
 
---
 
## **5. Basic Keepalived Configuration**
Here’s a simple example of a **Keepalived config** (`/etc/keepalived/keepalived.conf`):
 
```conf
vrrp_instance VI_1 {
    state MASTER          # or BACKUP
    interface eth0        # Network interface
    virtual_router_id 51  # Unique ID for VRRP group
    priority 100          # Higher = master (100 for master, 90 for backup)
    advert_int 1          # Check interval (seconds)
 
    virtual_ipaddress {
        192.168.1.100     # Virtual IP (VIP)
    }
}
```
 
### **How to Apply the Config**
1. Save the file.
2. Restart Keepalived:
   ```sh
   sudo systemctl restart keepalived
   ```
 
---
 
## **6. Checking Keepalived Status**
- **Check VRRP state**:
  ```sh
  sudo ip addr show vrrp+
  ```
- **Check logs**:
  ```sh
  sudo tail -f /var/log/syslog | grep keepalived
  ```
- **Verify active master**:
  ```sh
  sudo keepalived -v
  ```
 
---
 
## **7. Forcing a Failover (Manual Switch)**
If you need to **manually switch the master** (e.g., for maintenance):
```sh
sudo systemctl restart keepalived  # On the current master
```
or
```sh
sudo kill -9 $(pidof keepalived)   # Force restart
```
 
---
 
## **8. Conclusion**
- **VRRP** = Protocol for failover.
- **Keepalived** = Software that implements VRRP.
- **Useful for** high availability, load balancing, and automatic failover.
 
DIRC