实现双主模型NGINX架构

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实现双主模型NGINX架构相关的知识,希望对你有一定的参考价值。

实验拓扑图

技术分享图片

主节点配置

yum -y install nginx keepalived psmisc

#修改keepalived配置文件
vim  /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30

   router_id LVS1           #用于标识本节点的名称
   vrrp_garp_interval 0     
   vrrp_gna_interval 0
   vrrp_iptables            #关闭防火墙功能
#  vrrp_mcast_group4 224.0.0.18
}

vrrp_script chk_down {  #定义检测资源
    script "/bin/bash -c ‘[[ -f /etc/keepalived/down ]]‘ && exit 1 || exit 0"
    interval 1          #间隔检测时间
    weight -5           #检测失败后权重-5
}

vrrp_script chk_nginx { #定义检测资源
    script "killall -0 nginx && exit 0 || exit 1"
    interval 1          #间隔检测时间
    weight -5           #检测失败后权重-5
}
vrrp_instance VI_1 {    
    state MASTER        #定义状态为主或从
    interface ens34     #定义对外的网卡接口
    virtual_router_id 88    #虚拟路由ID,相同的IP为一组
    priority 100        #定义主节点的优先级
    advert_int 1
    authentication {    #定义认证信息
        auth_type PASS
        auth_pass rilegou
    }
    virtual_ipaddress { #定义虚拟IP(VIP)
        172.20.29.111
    }
    track_script {      #调用定义的检测资源脚本
        chk_nginx
        chk_down
    }
}

vrrp_instance VI_2 {
    state BUCKUP
    interface ens34
    virtual_router_id 99
    priority 95
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass centos
    }
    virtual_ipaddress {
            172.20.29.114
    }
    track_script {      #调用定义的检测资源脚本
        chk_nginx
        chk_down
    }
}

#配置NGINX服务
vim /etc/nginx/conf.d/test.conf
upstream websrvs {
    server 172.20.29.103:80;
    server 172.20.29.104:80;
}
server {
        listen 80 default_server;
        server_name node01.magedu.com;
        root /usr/share/nginx/html;
        location / {
                proxy_pass http://websrvs;
        }
}

#开启服务
systemctl nginx keepalived

从节点配置

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS2           #用于标识本节点的名称
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   vrrp_iptables            #关闭防火墙功能
#   vrrp_mcast_group4 172.20.29.77
}
vrrp_script chk_down {      #定义检测资源
    script "/bin/bash -c ‘[[ -f /etc/keepalived/down ]]‘ && exit 1 || exit 0"
    interval 1              #间隔检测时间
    weight -5               #检测失败后权重-5
}
vrrp_script chk_nginx {     #定义检测资源
    script "killall -0 nginx && exit 0 || exit 1"
    interval 1              #间隔检测时间
    weight -5               #检测失败后权重-5
}

vrrp_instance VI_1 {
    state BACKUP            #定义状态为主或从
    interface ens34         #定义外对的网卡接口
    virtual_router_id 88    #虚拟路由ID,相同的ID为一组
    priority 98             #定义从服务器的优先级
    advert_int 1
    authentication {        #认证信息
        auth_type PASS
        auth_pass rilegou
    }
    virtual_ipaddress {     #定义虚拟IP(VIP)
        172.20.29.111
    }
    track_script {          #调用定义的检测资源脚本
        chk_nginx
        chk_down
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface ens34
    virtual_router_id 99
    priority 96
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass centos
    }
    virtual_ipaddress {
        172.20.29.114
    }
    track_script {      #调用定义的检测资源脚本
        chk_nginx
        chk_down
    }
}

    #配置NGINX服务
vim /etc/nginx/conf.d/test.conf
upstream websrvs {
    server 172.20.29.103:80;
    server 172.20.29.104:80;
}
server {
        listen 80 default_server;
        server_name node01.magedu.com;
        root /usr/share/nginx/html;
        location / {
                proxy_pass http://websrvs;
        }
}

#开启服务
systemctl nginx keepalived

web服务器配置

#配置httpd页面
echo "<h1>`hostname`</h1>" > /var/www/html/index.html
#添加VIP
ip a a 172.20.29.111/32 dev lo

#配置VIP不冲突
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce

#开启服务
systemctl start httpd

以上是关于实现双主模型NGINX架构的主要内容,如果未能解决你的问题,请参考以下文章

Nginx+Keepalived双主架构实现

keepalived基于nginx实现双主模型

37yummyfood商城-集群版本-keepalived实现Nginx双主热备

keepalived高可用nginx/htttpd,双主模型

高可用集群架构Keepalived双主热备

Nginx+keepalived高可用(双主模式)