The keepalived will have three modules, core, check, and VRRP, respectively. The core module is keepalived, responsible for the start of the main process, maintenance and global configuration file loading and parsing, check module is responsible for health check, VRRP module is to implement VRRP protocol. Three keepalived configuration High availabilityPrepare two machines, Ens33 NIC IP for 192.168.226.131 and 192.168.226.132,131 as master,132 as backup
Both machines are installed keepalived
Performyum install -y keepalived
Both machines are pre-installed Nginx, using the source package installation, of course, can be installed directly yum
yum install -y epel-release yum install -y nginx
1) operation on Master
Edit the keepalived configuration file and modify it to the following content
vim /etc/keepalived/keepalived.confglobal_defs {notification_email { [email protected] //接收通知邮件的邮箱}notification_email_from [email protected] //发送通知邮件的邮箱smtp_server 127.0.0.1smtp_connect_timeout 30router_id LVS_DEVEL}vrrp_script chk_nginx {script "/usr/local/sbin/check_ng.sh" // 定义检查nginx运行状况的脚本interval 3}vrrp_instance VI_1 {state MASTERinterface ens33 // 定义vip 绑定的网卡virtual_router_id 51 priority 100 // vrrp 协议中各角色的权重advert_int 1authentication { auth_type PASS auth_pass lvlinux}virtual_ipaddress { 192.168.226.100 // 定义vip}track_script { chk_nginx}}
Create Nginx Check Script
vim /usr/local/sbin/check_ng.sh#!/bin/bash#时间变量,用于记录日志d=`date --date today +%Y%m%d_%H:%M:%S`#计算nginx进程数量n=`ps -C nginx --no-heading|wc -l`#如果进程为0,则启动nginx,并且再次检测nginx进程数量,#如果还为0,说明nginx无法启动,此时需要关闭keepalivedif [ $n -eq "0" ]; then /etc/init.d/nginx start n2=`ps -C nginx --no-heading|wc -l` if [ $n2 -eq "0" ]; then echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log systemctl stop keepalived fifi
Note If it is a Yum installation, the Nginx Start command in the script changes tosystemctl start nginx
Change Script Permissions
chmod 755 /usr/local/sbin/check_ng.sh
When the above operation is complete, start the keepalived service
systemctl start keepalived
2) operation on backup
Basically consistent with the Lord's operation, edit the keepalived configuration file and create an nginx check script.
The difference is that the keepalived configuration file to make the corresponding changes, the state by the master should be prepared, the weight of a bit. Only part of the code is attached
state BACKUPinterface ens33virtual_router_id 51priority 90
Also create Nginx check script after giving permission and then start the keepalived service
Quad Test High Availability
1 First look at the VIP tied to where, the two machines on the implementation of IP addr, found VIP tied to the Lord
2 Turn off Nginx service on Master
After turning off Nginx, PS viewing process found that Nginx is still in, this is because keepalived called Nginx Check script, and it started up, if the nginx problem, unable to start, the error message will be output to the script defined in the log file, while the main backup switch occurs.
3 intentionally edit the configuration file of the master Nginx error, and then close the Nginx service. See what's going to change again
Found the VIP bound to the standby machine