Keepalived - High Availability Solution
Keepalived is a service high-availability solution implemented based on the VRRP [1] protocol. It has three main modules: core, check, and VRRP.
The core module is keepalived’s core, responsible for starting and maintaining the main process and loading and parsing the global configuration file. check is responsible for health checks, including various common check methods. The VRRP module implements the VRRP protocol.
Based on these modules, keepalived uses a multi-process design, with each process corresponding to one module.
Installation
apt-get install keepalived
Configuration
keepalived has only one configuration file, keepalived.conf, which mainly contains the following configuration sections: global_defs, static_ipaddress, static_routes, vrrp_script, vrrp_instance, and virtual_server. [2]
global_defs
global_defs is used to set keepalived’s notification mechanism and identity
global_defs
{
notification_email
{
lfzyx@lfzyx.org
}
notification_email_from lfzyx@lfzyx.org
smtp_server mail.lfzyx.org
smtp_connect_timeout 30
router_id hostname
}
notification_email specifies the email address used to notify you when keepalived events occur
router_id sets the machine’s identifier
vrrp_script
Tells keepalived under what conditions to lower priority; there can be multiple vrrp_script blocks
vrrp_script chk_nginx
{
script "pidof nginx"
interval 1
weight -5
fall 1
rise 2
}
script: a check script you write yourself. It can also be a one-line command such as pidof nginx
interval 2: check once every 2 seconds
weight -5: if the check fails (script returns non-zero), priority is reduced by -5
fall 2: only after 2 consecutive failed checks is it considered a real failure. weight is used to reduce priority (between 1-255)
rise 2: 2 consecutive successful checks are considered success. But priority is not modified
vrrp_instance
vrrp_instance is used to define the VIP area exposed externally and its related attributes [3]
vrrp_instance MASTER
{
state MASTER
interface eth0
mcast_src_ip 10.10.10.71
virtual_router_id 73
priority 200
advert_int 1
authentication
{
auth_type PASS
auth_pass passwd
}
virtual_ipaddress
{
10.10.10.73
}
track_script
{
chk_nginx
}
}
state: specifies the instance’s initial state (Initial), but after startup an election occurs, and the higher-priority node will take over as MASTER
interface: the network interface the instance is bound to, since the virtual IP must be added on an existing network interface
mcast_src_ip: the source IP address used when sending multicast packets; if not set, the primary IP of the bound network interface is used
virtual_router_id: this sets the VRID, which is very important — the same VRID forms one group, and it determines the multicast MAC address
priority: sets this node’s priority; the higher priority node becomes master
advert_int: the check interval, default 1 second. This is VRRP’s timer — at each such interval, MASTER sends an advertisement packet to notify other routers in the group that it’s working normally
authentication: defines the authentication method and password, which must be the same on master and backup
virtual_ipaddress: this sets the VIP, i.e. the virtual IP address; it’s added or removed as state changes — added when state is master, removed when state is backup
track_script: references a VRRP script, i.e. the name specified in the vrrp_script section. It’s run periodically to change priority, which can ultimately trigger a master/backup switch.
References