What we usually call dual-machine hot standby is different from load balancing in the general sense. Both can have two machines running, but the former does not use two servers to provide services at the same time, and the latter is to provide services from two servers at the same time. Dual-machine hot standby means that one of the machines is down or the application is abnormal and will switch to the standby machine, and there will be no switching problems in load balancing.
The working principle of keepalived is VRRP (Virtual Router Redundancy Protocol) virtual routing redundancy protocol.
There are two important concepts in VRRP: VRRP routers and virtual routers, master routers and backup routers.
A VRRP router is a router that runs VRRP and is a physical entity. A virtual router is created by the VRRP protocol and is a logical concept. A group of VRRP routers work together to form a virtual router. There is an election mechanism in Vrrp, which is used to elect the route that provides services, that is, the master route, and the others become backup routes. When the master route fails, a master route will be re-elected from the backup route to continue working to ensure uninterrupted service.
The following describes the installation and testing of Keepalived
test environment
OS : CentOS 6.5
Host: 192.168.83.132
Standby machine: 192.168.83.133
Virtual IP: 192.168.83.120
1. Download Keepalived-1.2.7.tar.gz or other stable version, download address: http://www.keepalived.org/download.html
2. Install dependencies
yum install -y pcre-devel openssl-devel popt-devel gcc
3. Install Keepalived-1.2.7
tar -zxvf keepalived-1.2.7.tar.gz -C /opt/
./configure --prefix=/usr/local/keepalived
make && make install
4. Configure Keepalived as system service
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived/
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
5. Modify the configuration file
Master configuration
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0 #The name of the network card where the ip to be monitored is located
virtual_router_id 51
priority 100 #Bind the priority of the virtual ip
nopreempt #Do not actively preempt resources
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.83.120/24
}
}
The Backup configuration is the same as the Master configuration, you only need to modify the following configuration
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90 # This is changed to 90, the master priority is 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.83.120/24
}
8. Test the availability of keepalived
Deploy tomcat on the main and standby machines respectively, start the keepalived service, and access port 8080 of 192.168.83.120 through the virtual ip. If you can access it normally, the configuration is successful. Stop the master machine to see if you can switch to the backup machine.
This article only introduces the keepalived dual-system hot backup solution. If you want to apply it to the application service, you need to use the script to monitor the status of the application. I won't go into details again. If you need it, you can leave a message and introduce it in the follow-up document.