Nginx+keepalived双机热备(双主模式)

Nginx+keepalived双机热备(双主模式)

此前已经写过一篇使用keepalived实现nginx的高可用,这种方式有一台机器一直作backup使用,有50%的资源被浪费。
Nginx+keepalived双机热备(主从模式)
keepalived安装请查看
Nginx+keepalived双机热备(主从模式)

下面来配置nginx+keepalived的双主机双机热备,这种配置下有两个Virtual IP,两个机器互为主备,最后我们把域名DNS服务器解析至两个Virtual IP即可。

环境介绍

两台服务器

Server1:192.168.70.132
Server2:192.168.70.133

要设置的两个虚拟IP

Virtual IP1:192.168.70.130
Virtual IP2:192.168.70.129

将DNS域名解析至两个虚拟IP: 192.168.30.51、192.168.30.52

网络扑拓图

这里写图片描述

配置第一台服务器

第一台服务器ip为192.168.70.132

vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

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 NodeA
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_nginx {   #检查nginx进程脚本此处暂未启动
   script "/etc/keepalived/nginx_check.sh"  
   interval 2  
   weight -20  
} 

#虚拟IP1, 本机作为Master
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {  # 执行上面检测nginx进程脚本
       chk_nginx  
    } 
    virtual_ipaddress {
        192.168.70.130
    }
}


#虚拟IP2, 本机作为Backup
vrrp_instance VI_2 {
    state BACKUP
    interface ens33
    virtual_router_id 52
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {  # 执行上面检测nginx进程脚本
       chk_nginx  
    } 
    virtual_ipaddress {
        192.168.70.129
    }
}

配置第二台服务器

vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

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 NodeB #自定义主机名
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_nginx {   #检查nginx进程脚本此处暂未启动
   script "/etc/keepalived/nginx_check.sh"  
   interval 2  
   weight -20  
} 

#虚拟IP1, 本机作为BACKUP
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {  # 执行上面检测nginx进程脚本
       chk_nginx  
    } 
    virtual_ipaddress {  #虚拟ip
        192.168.70.130
    }
}


#虚拟IP2, 本机作为Master
vrrp_instance VI_2 {
    state MASTER
    interface ens33
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {  # 执行上面检测nginx进程脚本
       chk_nginx  
    } 
    virtual_ipaddress {  #虚拟ip
        192.168.70.129
    }
}

监控脚本

#!/bin/sh  
A=`ps -C nginx --no-header |wc -l`  
if [ $A -eq 0 ];then  
    nginx  
    sleep 2  
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then  
        killall keepalived  
    fi  
fi  

测试

修改nginx默认访问页nginx的默认页面

注意:测试过程中请重启iptables.

# nginx 默认访问页面
# $NGINX_HOME/html/index.html
vim /usr/share/nginx/html/index.html

修改server1的页,加入IP: 192.168.70.132

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
<p>IP:192.168.70.132</p>
</body>
</html>

修改server2的页,加入IP: 192.168.70.133

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
<p>IP:192.168.70.133</p>
</body>
</html>

两个机器都启动keepalived和nginx

访问虚拟ip1
在浏览器输入192.168.30.130进入访问页面
这里写图片描述

访问虚拟ip2
在浏览器输入192.168.30.129进入访问页面
这里写图片描述

证明两个VIP都起效果了


Kill掉server2的keepalived

killall keepalived

访问ip2
这里写图片描述
证明了server1为server2的备机,server2 down掉后server1接管server2的nginx访问


Kill掉server1机器的keepalived,启动server2机器的keepalived

Kill掉server1

killall keepalived

启动server2

service keepalived start

访问ip1
这里写图片描述
证明了server2为server1的备机,server1 down掉后server2接管server1的nginx访问

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宋发元

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值