keepalived双BACKUP加nopreempt不起作用,两个机器同时拥有vip,
排查几天发现是防火墙问题,啃爹。
打开 vi /etc/sysconfig/iptables
插入一条:-A RH-Firewall-1-INPUT -i eth0 -p 112 -j ACCEPT
保存后,
然后重启防火墙,就恢复正常。
要么把防火墙关掉。
-----------------------------------
我们这里仅仅是监控了网络故障和keepalived本身进程,在网络或者keepalived进程出现问题的时候会切换,但是我的节点A里面还有很多服务呢,例如nginx,PHP,mysql进程出问题或高负载的时候相应过慢怎么办,怎么切换的呢,这时就要用到脚本了,下面我们来看看keepalived是如何控制脚本来实现对服务器的监控和切换的
写个脚本来实时监控三个服务,若有一个出现问题遍切换mkdir /root/shell/
cd /root/shell
vi keepcheck.sh
#!/bin/bash
while :
do
mysqlcheck=`/usr/local/lnmp/mysql/bin/mysqladmin -uroot ping 2>&1`
mysqlcode=`echo $?`
phpcheck=`ps -C php-fpm --no-header | wc -l`
nginxcheck=`ps -C nginx --no-header | wc -l`
keepalivedcheck=`ps -C keepalived --no-header | wc -l`
if [ $nginxcheck -eq 0 ]|| [ $phpcheck -eq 0 ]||[ $mysqlcode -ne 0 ];then
if [ $keepalivedcheck -ne 0 ];then
killall -TERM keepalived
else
echo "keepalived is stoped"
fi
else
if [ $keepalivedcheck -eq 0 ];then
/etc/init.d/keepalived start
else
echo "keepalived is running"
fi
fi
sleep 5
done
注意,用/etc/init.d/keepalived start如果起不来,可以用/usr/local/keepalived/sbin/keepalived二进制文件直接执行启动即可
启动脚本:
- chmod +x /root/shell/keepcheck.sh
- nohup sh /root/shell/keepcheck.sh &
节点B也用这个脚本
节点A和节点B 都写入/etc/rc.local开机自动启动
- echo "nohup sh /root/shell/keepcheck.sh &" >> /etc/rc.loal
更改时间,并配置时间自动同步
crontab -e
加入下面一行:
*/30 * * * * ntpdate 210.72.145.44
配置时间:
date --set "06/10/2011 13:56"
即把时间调整为2011年06月10日,13点56分(以当你前时间为准)
保存时间,即保存到coms里
clock -w
--------------------------------------
一、keepalived环境搭建
1.硬、软件需求
keepalived-1.2.12.tar.gz
两台Linux服务器(kernel版本在2.4.18以上)
172.16.0.252、172.16.0.253
关闭防火墙:
service iptables stop
chkconfig --level 235 iptables off
关闭selinux:
vi /etc/selinux/config
SELINUX=disabled
2.安装步骤
2.1解压安装软件(两台服务器)
tar -zxvf keepalived-1.2.12.tar.gz
cd keepalived-1.2.12
./configure --prefix=/usr/local/keepalived --disable-lvs(没有ipvs负载均衡时,可以disable掉) --with-kernel-dir=/usr/src/kernels/2.6.32-358.el6.x86_64/(kernel版本根据实际情况)
make && make install
2.2 复制相关文件(两台服务器)
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/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/
2.3 编辑配置文件(两台服务器)
主服务器:
vi /etc/keepalived/keepalived.conf
global_defs {
notification_email {
root@localhost.localdomain
}
notification_email_from notification_email_from root@localhost.localdomain
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_sync_group VGM {
group {
VI_1
}
}
vrrp_script chk_tomcat {
script "/root/tomcat.pid"
interval 1
#weight -20
}
}
vrrp_instance VI_1 {
state BACKUP #只在Master上修改
interface eth0 #心跳,网络监控端口
virtual_router_id 51
priority 100
advert_int 1
nopreempt #只在Master上添加
authentication {
auth_type PASS
auth_pass 1111
}
track_interface {
eth0 #需要监控的网口
eth1
}
virtual_ipaddress {
172.16.0.254/23 dev eth1 #vip绑定在业务网口上,提供用户访问的ip地址
}
track_script {
chk_tomcat
}
}
备服务器:
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost.localdomain
}
notification_email_from root@localhost.localdomain
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_sync_group VGM {
group {
VI_1
}
}
vrrp_script chk_tomcat {
script "/root/tomcat.pid"
interval 1
#weight -20
}
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_interface {
eth0
eth1
}
virtual_ipaddress {
172.16.0.254/23 dev eth1
}
track_script {
chk_tomcat
}
}
新增tomcat.pid文件(两台服务器一样)
vim /root/tomcat.pid
#!/bin/bash
JAVA_PRO=`ps -C java --no-headers|wc -l`
if [ $JAVA_PRO -eq 0 ];then
/etc/init.d/keepalived stop
fi
chmod a+x tomcat.pid
3.启动服务(两台服务器):
先启动tomcat,再启动keepalived(service keepalived start)
4.检查vip
#ip addr
#tail -f /var/log/messages
http://www.tuicool.com/articles/7NjEjm
http://www.linuxidc.com/Linux/2015-03/114981.htm
http://www.ipython.me/centos/keepalived-config-using.html
排查:
1.是否需要添加默认网关或路由
3.是否开启了iptables和selinux
4.内核参数
net.ipv4.ip_forward = 1
开启IP转发功能
net.ipv4.ip_nonlocal_bind = 1
开启允许绑定非本机的IP
如果使用LVS的DR或者TUN模式结合Keepalived需要在后端真实服务器上特别设置两个arp相关的参数。这里也设置好。
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
5.如果Keepalived所在网络不允许使用组播,可以使用VRRP单播
6.怀疑是keepalived启动脚本/etc/init.d/keepalived的问题
关键是这一行
daemon $exec $KEEPALIVED_OPTIONS
由于没有复制/etc/sysconfig/keepalived,所以将直接执行damon /data/app_platform/keepalived/sbin/keepalived
由于keepalived默认使用的是/etc/keepalived/keepalived.conf作为配置文件,而这里指定了不同的配置文件,所以要修改成为
daemon $exec -D -f $config
如果前面没有定义config变量
就把$config换成配置文件的绝对位置
7.需要注意主备的weight和priority的值,这两个值如果设置不合理可能会影响VIP的切换。
8.如果使用的配置文件不是默认的配置文件,在启动Keepalived的时候需要使用 -f 参数指定配置文件。
9.两台服务器的时间是否一致。
10.重启网络