keepalive+nginx/mysql自动切换

一:环境准备

master服务器ip地址:192.168.1.99

slave服务器ip地址:192.168.1.70

两台机器分别安装nginx和keepalived,192.168.1.99作为master,192.168.1.70作为slave

二、keepalived安装

2.1 yum方式

安装依赖包
[root@localhost ~]# yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel
[root@localhost ~]# yum install -y keepalived
配置文件路径:/etc/keepalived/keepalived.conf
2、初始化及启动
[root@localhost ~]# systemctl start keepalived   //启动keepalived
[root@localhost ~]# systemctl enable keepalived  //加入开机启动keepalived
[root@localhost ~]# systemctl restart keepalived  //重新启动keepalived
[root@localhost ~]# systemctl status keepalived   //查看keepalived状态

2.2 软件包方式

[root@master src]# pwd
/usr/local/src
[root@master src]# wget http://www.keepalived.org/software/keepalived-2.0.7.tar.gz
[root@master src]# tar xvf keepalived-2.0.7.tar.gz
[root@master src]# cd keepalived-2.0.7
[root@master keepalived-2.0.7]# ./configure --prefix=/usr/local/keepalived
[root@master keepalived-2.0.7]# make && make install
安装完成后会在以下路径生成
/usr/local/keepalived/etc/keepalived/keepalived.conf
/usr/local/keepalived/etc/sysconfig/keepalived
/usr/local/keepalived/etc/sbin/keepalive

初始化及启动

## keepalived启动脚本变量引用文件,默认文件路径是/etc/sysconfig/,也可以不做软链接,直接修改启动脚本中文件路径即可(安装目录下)
cp /usr/local/keepalived/etc/sysconfig/keepalived  /etc/sysconfig/keepalived 
#将keepalived主程序加入到环境变量(安装目录下)
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
# keepalived启动脚本(源码目录下,就是解压后的文件位置),放到/etc/init.d/目录下就可以使用service命令便捷调用
 cp /usr/local/src/keepalived-2.0.7/keepalived/etc/init.d/keepalived  /etc/init.d/keepalived
# 将配置文件放到默认路径下
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

加为系统服务:chkconfig --add keepalived

开机启动:chkconfig keepalived on

查看开机启动的服务:chkconfig --list

启动、关闭、重启service keepalived start|stop|restart

三、编辑keepalived.conf配置文件

 cat check_nginx_.sh

chmod +x check_nginx_.sh

#!/bin/bash
 
A=`ps -C nginx --no-header |wc -l`
#判断nginx是否宕机,如果宕机了,尝试重启
if [ $A -eq 0 ];then
        # nginx的启动目录
        /usr/local/nginx/sbin/nginx
        # 等待3秒再次检查nginx,如果没有重启成功,则停止keepalived,使其启动备用机
        sleep 3
        if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
                service keepalived stop
        fi
fi

cat check_mysql.sh

chmod +x check_mysql_.sh

#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
    service keepalived stop
fi

主 keepalived.conf

! Configuration File for keepalived

global_defs {
    router_id LVS_97 #(重点参数)局域网keppalived主机身份标识信息(每台唯一)
    vrrp_mcast_group4 224.0.0.209
}

vrrp_script chk_mysql {
    script "/etc/keepalived/check_mysql.sh"
    interval 15 # 间隔15秒执行一次
    timeout 10 # 10秒超时
    fall 3 # 失败3次才算失败
}


vrrp_script chk_nginx {
    script "/etc/keepalived/check_nginx.sh"
    interval 15 # 间隔15秒执行一次
    timeout 10 # 10秒超时
    fall 3 # 失败3次才算失败
}

vrrp_instance VI_1 {
    interface ens192
    state BACKUP # 主从均设置为BACKUP
    priority 200 # 主从权重设置一致
    advert_int 1
    nopreempt
    virtual_router_id 227 #路由ID,可通过#tcpdump vrrp查看。
    garp_master_delay 1 #主从切换时间,单位为秒。

    authentication {
        auth_type PASS
        auth_pass 000000
    }
    track_interface {
       ens192
    }
    virtual_ipaddress {
        192.168.207.96
    }
    #virtual_ipaddress_excluded {
    #    2409:8c60:2400:2:0:1:3xxxx/112
    #}
    track_script {
        chk_mysql
    }
}

从 keepalived.conf

! Configuration File for keepalived

global_defs {
    router_id LVS_227#(重点参数)局域网keppalived主机身份标识信息(每台唯一)
    vrrp_mcast_group4 224.0.0.209
}

vrrp_script chk_mysql {
    script "/etc/keepalived/check_mysql.sh"
    interval 15 # 间隔15秒执行一次
    timeout 10 # 10秒超时
    fall 3 # 失败3次才算失败
}


vrrp_script chk_nginx {
    script "/etc/keepalived/check_nginx.sh"
    interval 15 # 间隔15秒执行一次
    timeout 10 # 10秒超时
    fall 3 # 失败3次才算失败
}

vrrp_instance VI_1 {
    interface ens192
    state BACKUP # 主从均设置为BACKUP
    priority 100 # 主从权重设置一致
    advert_int 1
    nopreempt
    virtual_router_id 227 #路由ID,可通过#tcpdump vrrp查看。
    garp_master_delay 1 #主从切换时间,单位为秒。

    authentication {
        auth_type PASS
        auth_pass 000000
    }
    track_interface {
       ens192
    }
    virtual_ipaddress {
        192.168.207.96
    }
    #virtual_ipaddress_excluded {
    #    2409:8c60:2400:2:0:1:xxxx/112
    #}
    track_script {
        chk_mysql
    }
}

四、测试

把keepalived停了,查看vip、访问nginx

当然你也可以自己测试,不断输入 ./nginx -s stop 把nginx关闭,看是否能正常访问

五、

问题:两台机器上面都有VIP的情况

排查:

1.检查防火墙,发现已经是关闭状态。

2. keepalived.conf配置问题。

3.可能是上联交换机禁用了arp的广播限制,造成keepalive无法通过广播通信,两台服务器抢占vip,出现同时都有vip的情况。

解决方案:

①设置非抢占模式,然后通过priority权重来区分谁的优先级更高(如上述配置文件)

②单播(定向广播)的方式 (一些特定环境禁用了组播方式)(下方)

[root@test-15]# vim /etc/keepalived.conf

    priority 50

    unicast_src_ip  172.19.1.15   #本机ip

    unicast_peer {              

        172.19.1.14      #对端ip

    }

[root@test-14]# vim /etc/keepalived.conf

    priority 100

    unicast_src_ip  172.19.1.14   #本机ip

    unicast_peer {              

        172.19.1.15      #对端ip

    }

③时间不同步 服务器时间校时

 tcpdump -i ens192 vrrp -n   检查发现 14和15都在对224.0.0.18发送消息。但是在正常情况下,备节点如果收到主节点的心跳消息时,优先级高于自己,就不会主动对外发送消息。

配置完成后恢复正常,查看:  tcpdump -i ens192 vrrp -n

你可能感兴趣的:(nginx,服务器,运维)