Keepalived is a software used for hot standby (HA). It is often used together with haproxy for Hot Standby + load balancing to achieve high availability.
Operating principle
Keepalived selects a Hot Standby server as the master machine through election (depending on the weight set by the server). The master machine will be allocated to a specified virtual IP address, external programs can access this server through this IP address. If this server fails (Network disconnection, restart, or keepalived crash on this machine ), keepalived will reselect one machine from another backup machine (or check the weight set by the server) as the master and allocate the same virtual IP address to the current master role.
Election strategy
The election policy is based on the vrrp protocol, completely based on the weight, the maximum weight (0 ~ 255) the master machine will trigger the election in the following situations
1. When keepalived is started
2. master server faults (Network disconnection, restart, or keepalived crash on the machine, but other applications on the machine are not counted as crash)
3. A new backup server is added and the weight is the maximum.
Configure an instance
Two servers and two machines need to access the LAN, and the virtual IP address will be set in their common LAN. If the two machines do not have access to the LAN, each of the virtual IP addresses will be set.
192.168.1.41 Ubuntu 10.04.1 lts (32-bit)
192.168.1.135 Red Hat Enterprise Linux Server Release 6.1 (Santiago) (64-bit)
Download the source code for installation. Both servers must be installed and configured.
Note that you may need to install the popt Library
Ubuntu
sudo apt-get install libpopt-dev
RedHat
sudo yum install popt-devel.x86_64
Unzip and install
tar -zxvf keepalived-1.2.2.tar.gzcd keepalived-1.2.2./configure --prefix=/usr/local/keepalived makemake install
The following steps are not required, but are recommended for future operations.
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
Create a path for storing configuration files for keepalived
mkdir -p /etc/etc/keepalived/
The following is the configuration file on each server. The configuration items are exactly the same. The configuration value is based on the specific situation of the local machine. I will paste the RedHat
global_defs{ notification_email { [email protected]163.com } notification_email_from [email protected]163.com smtp_server 127.0.0.1 stmp_connect_timeout 30 router_id lnmp_node1}vrrp_instance lnmp { state MASTER interface em1 virtual_router_id 100 priority 170 advert_int 5 track_interface { em1 } authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.1.200 }}
After configuration, you can start it directly.
Start in Ubuntu
/usr/local/keepalived/sbin/keepalived
In RedHat, you can start Ubuntu or as follows
sudo /etc/init.d/keepalived restart
After starting, you can use the IP address a command to check the binding status of the virtual IP address. My configuration is high in the weight of 192.168.1.135, so the VIP is bound to the 135
(ape2!1076)~(h135:135)ip a1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever2: em1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 link/ether d4:ae:52:6c:74:69 brd ff:ff:ff:ff:ff:ff inet 192.168.1.135/24 brd 192.168.1.255 scope global em1 inet 192.168.1.200/32 scope global em1 inet6 fe80::d6ae:52ff:fe6c:7469/64 scope link
At this time, you can use the IP address 192.168.1.200 to access the 135 host.
Configure master-slave backup for keepalived