配置nginx + keepalived双主模式(双机互为主备)


前言

此前已经写过一篇使用keepalived实现nginx的高可用,这种方式有一台机器一直作backup使用,有50%的资源被浪费。

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


环境介绍

两台服务器

Server1:192.168.30.61
Server2:192.168.30.62

要设置的两个虚拟IP

Virtual IP1:192.168.30.51
Virtual IP2:192.168.30.52

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


网络扑拓图

这里写图片描述


配置第一台服务器


第一台服务器ip为192.168.30.61

vi /etc/keepalived/keepalived.conf

配置内容如下:

global_defs
{
    notification_email
    {
       123456@qq.com
    }
    notification_email_from 123456@qq.com
    smtp_server 127.0.0.1
    stmp_connect_timeout 30
    router_id LVS_DEVEL
}

vrrp_script Monitor_Nginx {
    script "/usr/local/keepalived/scripts/monitor_nginx.sh"
    interval 2
    weight 2
}

# 虚拟IP1, 本机作为Master
vrrp_instance VI_1 {
    state MASTER
    interface enp0s3
    virtual_router_id 51
    priority 100
    advert_int 1
    track_interface {
        enp0s3
    }

    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.30.51
    }i
    track_script {
        Monitor_Nginx
    }

}

# 虚拟IP2, 本机作为Backup
vrrp_instance VI_2 {
    state BACKUP
    interface enp0s3
    virtual_router_id 52
    priority 100
    advert_int 1
    track_interface {
        enp0s3
    }

    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.30.52
    }
    track_script {
        Monitor_Nginx
    }

}


配置第二台服务器


第二台服务器ip为192.168.30.62

vi /etc/keepalived/keepalived.conf

配置内容如下:

global_defs
{
    notification_email
    {
       123456@qq.com
    }
    notification_email_from 123456@qq.com
    smtp_server 127.0.0.1
    stmp_connect_timeout 30
    router_id LVS_DEVEL
}

vrrp_script Monitor_Nginx {
    script "/usr/local/keepalived/scripts/monitor_nginx.sh"
    interval 2
    weight 2
}

# 虚拟IP1, 本机作为BACKUP
vrrp_instance VI_1 {
    state BACKUP
    interface enp0s3
    virtual_router_id 51
    priority 100
    advert_int 1
    track_interface {
        enp0s3
    }

    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.30.51
    }
    track_script {
        Monitor_Nginx
    }
}

# 虚拟IP2, 本机作为Master
vrrp_instance VI_2 {
    state MASTER
    interface enp0s3
    virtual_router_id 52
    priority 100
    advert_int 1
    track_interface {
        enp0s3
    }

    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.30.52
    }
    track_script {
        Monitor_Nginx
    }

}

监控脚本


和之前的监控脚本一样

# 监控nginx进程,若nginx主进程不存在则启动nginx
# 若5s后nginx进程还是不存在的话kill掉keepalived进程,防止nginx没运行该主机的keepalived还接管虚拟IP
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
        then
        /usr/local/nginx/sbin/nginx
        sleep 5
        if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
        then
                  killall keepalived
        fi
fi

测试


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

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

修改server1的页,加入IP: 192.168.30.61

<!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! 192.168.30.61 </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>
</body>
</html>

server2同理


两个机器都启动keepalived和nginx

访问虚拟ip1

在浏览器输入192.168.30.51进入访问页面
这里写图片描述

访问虚拟ip2

在浏览器输入192.168.30.52进入访问页面
这里写图片描述

证明两个VIP都起效果了


Kill掉server2的keepalived

pkill keepalived
  • 访问ip2

这里写图片描述

证明了server1为server2的备机,server2 down掉后server1接管server1的nginx访问


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

  • 访问ip1

这里写图片描述

证明了server2为server1的备机,server1 down掉后server1接管server1的nginx访问


有关nginx和keepalived的安装可参考


  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值