码迷,mamicode.com
首页 > 其他好文 > 详细

实现双主模型NGINX架构

时间:2018-07-18 20:26:29      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:emctl   connect   api   code   nod   lego   web服务器   技术   关闭   

实验拓扑图

技术分享图片

主节点配置

yum -y install nginx keepalived psmisc

#修改keepalived配置文件
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 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 {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   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架构

标签:emctl   connect   api   code   nod   lego   web服务器   技术   关闭   

原文地址:http://blog.51cto.com/13769014/2146906

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!