下载wget http://keepalived.org/software/keepalived-2.0.6.tar.gz
mkdir /usr/local/keepalived/
tar -zxvf keepalived-2.0.6.tar.gz -C /usr/local/keepalived
cd /usr/local/keepalived/keepalived-2.0.6
./configure
make
make install
cp -r keepalived /etc/init.d/keepalived
mkdir /etc/keepalived/
cd /usr/local/keepalived/keepalived-2.0.6/keepalived/etc/keepalived
cp keepalived.conf /etc/keepalived/
cd /usr/local/keepalived/keepalived-2.0.6/keepalived/etc/sysconfig/
cp keepalived /etc/sysconfig/keepalived
cp /usr/local/keepalived/keepalived-2.0.6/bin/keepalived /usr/sbin
vi /etc/keepalived/keepalived.conf #以下是配置的内容
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.67.100
}
}
以上修改了如下三项
1.
priority 101 #Master值要大于Bakcup
2.更改网络接口
vrrp_instance VI_1 中interface:ens33(ip addr命令中的网络)
3.
192.168.67.100 是虚拟ip
启动keepalived
service keepalived start或者systemctl start keepalived
备用调度服务器 只有一项不同
priority 100
同样配置完毕后启动keepalived
注意:
#vrrp_strict #这个注释掉,否则可能造成物理机的浏览器无法访问应用
启动keepalived
service keepalived start
设置开机自启动
systemctl enable keepalived.service
keeplived监控nginx是否死掉的应加配置
代码如下:
vrrp_script chk_nginx{
script "/root/chk_nginx.sh"
}
track_script{
chk_nginx
}
sh脚本中:
代码如下:
#!/bin/bash
if [ "$(ps aux | grep "nginx: master process"|grep -v grep)" == "" ]
then
service keepalived stop
fi
用绿线标注的是自定义的一个sh脚本路径。
修改之后需要重启keepalived
关闭:service keepalived stop
开启:service keepalived start
开机自动启动:systemctl enable keepalived.service