Keepalived is an open-source software that acts as a load balancer and Virtual Router Redundancy Protocol (VRRP) on Linux, FreeBSD, OpenBSD, and NetBSD.
Keepalived works by monitoring the status of the load balancer. When one of the machines fails, Keepalived will automatically move the traffic to another load balancer machine.
Keepalived can also be used to establish VRRP, which allows the network to continue operating if the main router fails.
Features of Keepalived
- Load Balancing: Keepalived can distribute traffic to multiple instances of a service.
- VRRP: Keepalived can establish VRRP to maintain network availability.
- Status Monitoring: Keepalived can monitor the status of the load balancer.
- Dynamic Configuration: Keepalived can be configured dynamically based on conditions.
Usage of Keepalived
Keepalived can be used to:
- Distribute traffic to multiple instances of a service.
- Maintain network availability if the main router fails.
- Route traffic based on conditions.
Examples of Keepalived usage
- Use Keepalived to distribute traffic to multiple instances of a service Keepalived can be used to distribute traffic to multiple instances of a service, such as a web server or database server.
- Use Keepalived to maintain network availability Keepalived can be used to establish VRRP to maintain network availability if the main router fails.
- Use Keepalived to route traffic based on conditions Keepalived can be used to route traffic based on conditions, such as the type of HTTP request, the user’s geographic location, and others.
Installation of Keepalived
Keepalived can be installed on Linux, FreeBSD, OpenBSD, and NetBSD using the following commands:
# apt-get install keepalived
# yum install keepalived
# pkg install keepalived
# pkgsrc install keepalived
Configuration of Keepalived
Keepalived configuration can be done using the /etc/keepalived/keepalived.conf file.
The /etc/keepalived/keepalived.conf file contains configuration settings such as the IP address of the virtual router, load balancer instances, and status monitoring conditions.
Example Keepalived configuration
global_defs {
# Define the name of the virtual router
router_id VR1
# Define the IP address of the virtual router
virtual_router_id 1
# Define the Keepalive interval
vrrp_instance VR1 {
state MASTER
interface eth0
virtual_ipaddress {
192.168.1.1/24
}
# Define the Keepalive interval
priority 100
# Define the status monitoring conditions
track_interface {
eth0
}
}
# Define the load balancer instances
# Instance 1
vrrp_instance VR2 {
state BACKUP
interface eth0
virtual_ipaddress {
192.168.1.2/24
}
# Define the Keepalive interval
priority 90
# Define the status monitoring conditions
track_interface {
eth0
}
}
}
In the example configuration above, the name of the virtual router is set to VR1 and the IP address is set to 192.168.1.1.
Two load balancer instances are defined: 192.168.1.1 and 192.168.1.2.
Instance 1 is set to state MASTER and priority 100.
Instance 2 is set to state BACKUP and priority 90.
The status monitoring condition is to check the status of the eth0 interface.
When instance 1 fails, Keepalived will automatically move the traffic to instance 2.
Here are some additional details about the Keepalived configuration file:
- The
global_defs
section contains global configuration settings. - The
vrrp_instance
section defines a virtual router instance. - The
state
parameter specifies the state of the instance. - The
interface
parameter specifies the interface that the instance uses. - The
virtual_ipaddress
parameter specifies the virtual IP address of the instance. - The
priority
parameter specifies the priority of the instance. - The
track_interface
parameter specifies the interface to track for status monitoring.