Nginx+Keepalived双机热备(主主模式)(2)

[root@centos-2 ~]#cat /etc/keepalived/keepalived.conf
! Configuration Filefor keepalived 
   
global_defs { 
  notification_email { 
    acassen@firewall.loc 
    #failover@firewall.loc 
    #sysadmin@firewall.loc 
  } 
  router_id LVS_DEVEL 

 vrrp_scriptchk_http_port {       
    script "/opt/check_nginx.sh" 
    interval 2                     
    weight -5                     
    fall 2                 
    rise 1                 
}
   
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.5.200 
    } 

 
vrrp_instance VI_2{ 
    state MASTER
    interface eth0
    virtual_router_id 50
    priority 100
    advert_int 1 
    authentication { 
        auth_type PASS
        auth_pass 1111 
    }
    virtual_ipaddress {
        192.168.5.210
}
track_script {                     
    chk_http_port                 
}
}

编写一个监控nginx的脚本:
需要注意的是,要判断本机nginx是否正常,如果发现nginx不正常,重启之后,等待三秒在校验,任然失败则不尝试,关闭keepalived,发送邮件,其他主机此时接管VIP;
[root@centos-4~]# cat /opt/check_nginx.sh
#!/bin/bash
check=$(ps-C nginx --no-heading|wc -l)
IP=`ipadd | grep eth0 | awk  'NR==2{print $2}'| awk -F '/' '{print $1}'`
if ["${check}" = "0" ]; then
    /usr/local/nginx/sbin/nginx
    sleep 2
    counter=$(ps -C nginx --no-heading|wc -l)
    if [ "${check}" = "0"]; then
        /etc/init.d/keepalived stop
      echo "check $IP nginx is down"| mail -s "check keepalived nginx" *********@qq.com
    fi
fi

(KA1一样的监控脚本)
2、在两台前端服务器上启动keepalived服务,对于192.168.5.200的vip centos-1是master/192.168.5.210的vip centos-1是backup。
[root@centos-1 ~]#service keepalived start
[root@centos-4 ~]# service keepalived start
查看日志文件:
[root@centos-1 ~]# cat /var/log/messages
Oct 19 22:00:22 centos-1 Keepalived_vrrp[46184]: VRRP_Instance(VI_2)Sending gratuitous ARPs on eth0 for 192.168.5.210
Oct 19 22:00:22 centos-1 Keepalived_healthcheckers[46183]: Netlinkreflector reports IP 192.168.5.210 added
Oct 19 22:00:24 centos-1 Keepalived_vrrp[46184]: VRRP_Instance(VI_1)Sending gratuitous ARPs on eth0 for 192.168.5.200
Oct 19 22:00:27 centos-1 Keepalived_vrrp[46184]: VRRP_Instance(VI_2)Sending gratuitous ARPs on eth0 for 192.168.5.210
(因为KA1先启动keepalived服务所以两个vip都会在KA1上,但第二台keepaliver服务起来后vip2就会被KA2抢占回来。)
[root@centos-4 ~]# cat /var/log/messages
Oct 19 22:01:38 centos-4 Keepalived_healthcheckers[15009]: Netlinkreflector reports IP 192.168.5.210 added
Oct 19 22:01:38 centos-4 avahi-daemon[1513]: Registering new addressrecord for 192.168.5.210 on eth0.IPv4.
Oct 19 22:01:38 centos-4 Keepalived_vrrp[15010]: VRRP_Instance(VI_2)Sending gratuitous ARPs on eth0 for 192.168.5.210
Oct 19 22:01:43 centos-4 Keepalived_vrrp[15010]: VRRP_Instance(VI_2)Sending gratuitous ARPs on eth0 for 192.168.5.210
查看ip addr:
[root@centos-1 keepalived]# ip add
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_faststate UP qlen 1000
    link/ether00:0c:29:0d:f3:5d brd ff:ff:ff:ff:ff:ff
    inet 192.168.5.129/24 brd192.168.5.255 scope global eth0
    inet 192.168.5.200/32scope global eth0
 
 [root@centos-4 keepalived]#ip addr
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdiscpfifo_fast state UP qlen 1000
    link/ether00:50:56:3a:84:30 brd ff:ff:ff:ff:ff:ff
    inet 192.168.5.128/24 brd192.168.5.255 scope global eth0
        inet 192.168.5.210/32 scope global eth0
3、配置nginx的反向代理
(在web1和web2服务器上配置两个web服务(可以http或者nginx)用来测试使用,这里就不一一演示了。)
[root@centos-2 ~]# curl localhost
2

[root@centos-3 ~]# curl localhost
3

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/3d4bdc06ad502d072290b0e698c19d2b.html