一、简介 

上一篇博文介绍了keepalived的高可用集群,其实使用nginx做前端代理,负载均衡照样可以实现双主或主备模式的高可用集群比起keepalived基于lvs的dr配置更为简单易学,下面是本人所理解的nginx高可用集群。此实验拓扑图类似keepalived拓扑,把图中keepalived字样换成nginx即可,我就不画了,有意者可重画。 

实验拓扑图:

NGINX 集中管理平台_后端

 

    二、nginx主备模式高可用

    配置nginx的upstream模块,让其反代到后端主机,后端主机配置和keepalived中雷同即可。



upstream websrvs {
        server 10.1.10.5:80;    #后端主机real server01
        server 10.1.10.6:80;     #后端主机real server02
        server 10.1.10.2:80 backup; #当后端主机均不能响应时此主机响应应急页面
    }



    配置keepalived配置文件,实现VIP地址漂移



! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node1
vrrp_mcast_group4 224.0.100.18
}
vrrp_script chk_down {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1
weight -5
}
vrrp_script chk_nginx {
script "killall -0 nginx && exit 0 || exit 1"
interval 1
weight -5
}
vrrp_instance VI_1 {
state MASTER
interface eno16777736
virtual_router_id 57
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 98181111
}
virtual_ipaddress {
10.1.10.7/16 dev eno16777736
}
track_script {
chk_down
chk_httpd
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}




实现图:

模拟单台后端主机无法提供服务及其全部宕机实验结果如下:

NGINX 集中管理平台_前端_02

    上诉结果可看出,此实验成功的展示了主备模式下的nginx反代,实现高可用。此实验课检测nginx的健康状态及其主备模式的切换时发邮件给管理员。


    三、nginx主主模式高可用

    配置keepalived配置文件,实现VIP地址漂移,和上诉主备相似



! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node1
vrrp_mcast_group4 224.0.120.18
}
vrrp_script chk_down { #检测此文件下是否存在down这个文件,灰度模式下上线时可使用此
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1
weight -5
}
vrrp_script chk_nginx { #检测nginx的健康状态信息
script "killall -0 nginx && exit 0 || exit 1"
interval 1
weight -5
}
vrrp_instance VI_1 {
state MASTER
interface eno16777736
virtual_router_id 53
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 9818sss1
}
virtual_ipaddress {
10.1.10.7/16 dev eno16777736
}
track_script {
chk_down
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
vrrp_instance VI_2 {
state BACKUP
interface eno16777736
virtual_router_id 54
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass 9818rss1
}
virtual_ipaddress {
10.1.10.77/16 dev eno16777736
}
track_script {
chk_down
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}



此时,nginx上keepalived均启动结果如图:

NGINX 集中管理平台_NGINX 集中管理平台_03

模拟后端主机宕机及其恢复正常结果类似keepalived如图所示:

NGINX 集中管理平台_NGINX 集中管理平台_04

   


 到此,nginx的高可用集群均已实现,做实验的方法和keepalived有很多相似之处。




转载于:https://blog.51cto.com/purify/1867871