Linux - Keepalived + Nginx(双主)

2021/8/30 7:06:29

本文主要是介绍Linux - Keepalived + Nginx(双主),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

客户端 cli.noise.org : 10.0.0.7 - cli.noise.org

keepalived1  + nginx - 10.0.0.34 - ka1.noise.org

keepalived2  + ngixn - 10.0.0.22 - ka2.noise.org

后台web服务器1: 10.0.0.35 - rs1.noise.org

后台web服务器2: 10.0.0.36 - rs2.noise.org

 

==========================================================

ka1.noise.org - keepalived.conf

  
global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id ka1.noise.org       -》 唯一ID
   vrrp_mcast_group 224.0.100.100    -》 多播地址
}


vrrp_script check_nginx {
    script "/data/check_nginx.sh"  -》 检查脚本,nginx服务挂掉,自动重启
    interval 1
    weight -30
    fall 3
    rise 5
    timeout 2
}


vrrp_instance VI_1 {      -》 集群VI_1, ka1为主节点
    state MASTER        -》 节点状态
    interface eth0
    virtual_router_id 66    -》 唯一集群ID 
    #nopreempt
    priority 100        -》 节点优先级
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.0.0.20/24 dev eth0 label eth0:1  -》 集群漂移VIP
    }

    track_interface {
        eth0
    }

    track_script {
        check_nginx
    }
}

vrrp_instance VI_2 {  -》 集群VI_2
    state BACKUP      -> 当前节点作为子节点
    interface eth0
    virtual_router_id 88  -》 唯一集群ID
    #nopreempt
    priority 80      -》 节点优先级
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.0.0.30/24 dev eth0 label eth0:1  -》 集群VIP
    }

    track_interface {
        eth0
    }

    track_script {
        check_nginx
    }
}

 

===============================================

此为ka2.noise.org的配置,部分配置与ka1.noise.org相反

! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id ka2.noise.org
   vrrp_mcast_group 224.0.100.100
}


vrrp_script check_nginx {
    script "/data/check_nginx.sh"
    interval 1
    weight -30
    fall 3
    rise 5
    timeout 2
}


vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 66
    #nopreempt
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.0.0.20/24 dev eth0 label eth0:1
    }

    track_interface {
        eth0
    }

    track_script {
        check_nginx
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 88
    #nopreempt
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.0.0.30/24 dev eth0 label eth0:1
    }

    track_interface {
        eth0
    }

    track_script {
        check_nginx
    }
}

===================================

两个ka节点nginx配置一样,负责反向代理

http {
    upstream websrvs {
        server 10.0.0.35:80 weight=1;
        server 10.0.0.36:80 weight=1;
    }

    server {
        listen 80;
        location / {
            proxy_pass http://websrvs/;
        }
    }

}

==================================

[19:07:33 root@ka1 ~]#cat /data/check_nginx.sh 
#!/bin/bash
#
#**************************************************************************************
#Author:                                   Noise Lys
#QQ:                                       578110218
#Date:                                     2021-08-29
#Filename:                                 /data/check_nginx.sh
#URL:                                      https://www.cnblogs.com/noise/
#Description:                              The test script
#Copyright (C):                            2021 All rights reserved
#**************************************************************************************
/usr/bin/killall -0 nginx || systemctl restart nginx 

=================================

开始检查结果,访问10.0.0.20即集群VI_1的VIP时,主节点ka1.noise.org工作,ka2.noise.org不工作
但是访问10.0.0.30即集群VI_2的VIP时,主节点ka2.noise.org工作,ka1.noise.org不工作
[19:00:48 root@cli ~]#curl 10.0.0.20
rs1.noise.org
[19:00:49 root@cli ~]#curl 10.0.0.20
rs2.noise.org
[19:00:49 root@cli ~]#curl 10.0.0.20
rs1.noise.org
[19:00:49 root@cli ~]#curl 10.0.0.20
rs2.noise.org
[19:00:50 root@cli ~]#curl 10.0.0.20
rs1.noise.org
[19:00:50 root@cli ~]#curl 10.0.0.30
rs1.noise.org
[19:00:52 root@cli ~]#curl 10.0.0.30
rs2.noise.org
[19:00:53 root@cli ~]#curl 10.0.0.30
rs1.noise.org
[19:00:53 root@cli ~]#curl 10.0.0.30
rs2.noise.org

 



这篇关于Linux - Keepalived + Nginx(双主)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程