keepalived
虚拟IP
主Nginx(MASTER)IP
备Nginx(BACKUP)IP
安装:yum install keepalived -y
测试是否安装成功:rpm -qa keepalived
配置文件:/etc/keepalived/keepalived.conf
日志存放位置:/var/log/messages
global_defs {
notification_email {}
}
vrrp_script chk_http_port{}
vrrp_instance VI_1 {
authentication {}
virtual_ipaddress {}
}
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
router_id LVS_DEVEL
值可以是IP
在/etc/hosts中127.0.0.1 xxx
xxx就是这个id
vrrp_script chk_http_port{
script "/user/local/src/nginx_check.sh"
interval 2
weight 2
}
script 脚本地址
interval 2 脚本检测时间,2秒
weight 权重改变,当脚本中的条件成立,当前服务器权重-20
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.16
192.168.200.17
192.168.200.18
}
}
state MASTER/BACKUP 主设备MASTER还是备设备BACKUP
interface ens33 网卡号
终端:ifconfig
第一行第一个英文单词
virtual_router_id 51 主设备和备设备id要相同
priority 100 优先级,主机值大,备机值小
advert_int 1 每隔一秒测试有没有活着
authentication 权限调用方式
virtual_ipaddress 虚拟IP地址
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.16
192.168.200.17
192.168.200.18
}
}
state MASTER/BACKUP 主设备MASTER还是备设备BACKUP
interface ens33 网卡号
终端:ifconfig
第一行第一个英文单词
virtual_router_id 51 主设备和备设备id要相同
priority 100 优先级,主机值大,备机值小
advert_int 1 每隔一秒测试有没有活着
authentication 权限调用方式
virtual_ipaddress 虚拟IP地址
/user/local/src/nginx_check.sh
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
systemctl start keepalived.service
ps -ef | grep keepalived