实战案例: 实现双主模式Nginx反向代理的高可用

实战案例:实现双主模式Nginx反向代理的高可用
#在两个节点都配置nginx反向代理
[root@ka1-centos8 ~]vim /etc/nginx/nginx.conf
http {
    upstream websrvs {
        server 10.0.0.7:80 weight=1;
        server 10.0.0.17:80 weight-1;
    }
    upstream websrvs2 {
        server 10.0.0.27:80 weight=1;
        server 10.0.0.37:80 weight-1;
    }

    server {
        listen 80;
        server_name www.a.com;
        location /{
            proxy_pass http://webservs/;
        }
    }
    server {
        listen 80;
        server_name www.b.com;
        location /{
            proxy_pass http://webservs2/;
        }
    }

}

#在两个节点都配置实现双主模式的nginx反向代理高可用
[root@ka1-centos8 ~]#cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
    global_defs {
        notification_email {
        root@localhost
        }
    notification_email_from kaadmin@localhost
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id ka1.magedu.org            #在另一个节点为ka2.magedu.org
    vrrp_mcast_group4 224.100.100.100
}
vrrp_script check_nginx {
    script "/etc/keepalived/check_nginx.sh"
    #script "/usr/bin/killall -0 nginx"
    interval 1
    weight -30
    fall 3
    rise 5
   timeout 2
}

vrrp_instance VI_1 {
    state MASTER                            #在另一个节点为BACKUP
    interface eth0
    virtual_router_id 66
    priority 100                            #在另一个节点为80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.0.0.10/24 dev eth0 label eth0:1
    }
    track_interface {
        eth0
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
    track_script {
        chk_nginx
    }
}

vrrp_instance VI_2 {
    state BACKUP                        #在另一个节点为MASTER
    interface eth0
    virtual_router_id 88
    priority 80                         #在另一个节点为100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.0.0.20/24 dev eth0 label eth0:2
    }
    track_interface {
        eth0
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
    track_script {
        chk_nginx
    }
}
[root@ka1-centos8 ~]# yum install psmisc -y
[root@ka1-centos8 ~]# cat /etc/keepalived/check_nginx.sh
#!/bin/bash
/usr/bin/killall -0 nginx
[root@ka1-centos8 ~]# chmod a+x /etc/keepalived/check_nginx.sh 

本文链接:http://www.yunweipai.com/35393.html

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/52725.html

(0)
上一篇 2021年8月6日
下一篇 2021年8月6日

相关推荐

发表回复

登录后才能评论