Keepalived实现单主模式的Nginx反向代理的高可用

Posted y_zilong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Keepalived实现单主模式的Nginx反向代理的高可用相关的知识,希望对你有一定的参考价值。

在两个节点都配置nginx反向代理

[root@ka1 ~]# vim /etc/nginx/nginx.conf
http {
     upstream websrvs {
        server 10.0.0.40:80 weight=1;
	    server 10.0.0.50:80 weight=1;
     }
     server {
        listen 80;
	 location / {
	     proxy_pass http://websrvs/;
     }
     }
}

在两个节点都配置实现nginx反向代理高可用

[root@ka1 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
     calm_yzl@163.com
   }
   notification_email_from 295574227@qq.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id ka1.example.com             #另一个节点为ka2.example.com
   vrrp_mcast_group4 224.0.100.100
}
vrrp_script check_nginx {
    script "/etc/keepalived/check_nginx.sh"
    interval 1
    weight -30
    fall 3
    rise 5
    timeout 2
}

include /etc/keepalived/conf.d/*.conf
[root@ka1 ~]# 
[root@ka1 ~]# cat /etc/keepalived/conf.d/m1.conf 
vrrp_instance m1 {
    state MASTER                        #另一个节点为BACKUP
    interface ens33
    virtual_router_id 66
    priority 100                         #另一个节点为80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
      10.0.0.100/24 dev ens33 label ens33:1
    }
    track_interface {
      ens33
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault  "/etc/keepalived/notify.sh fault"
    track_script {
       check_nginx
    }
}
[root@ka1 ~]# 
[root@ka1 conf.d]# yum install -y psmisc

[root@ka1 ~]# vi /etc/keepalived/check_nginx.sh 
#!/bin/bash
/usr/bin/killall -0 nginx || systemctl restart nginx

[root@ka1 ~]# chmod a+x /etc/keepalived/check_nginx.sh 

[root@ka1 ~]# scp /etc/keepalived/check_nginx.sh 10.0.0.20:/etc/keepalived/check_nginx.sh 
check_nginx.sh                                                            100%   65    29.8KB/s   00:00  

 查看IP、测试

[root@rs1 ~]# yum install -y nginx
[root@rs1 ~]# echo 'Nginx to rs1' > /usr/share/nginx/html/index.html 
[root@rs1 ~]# systemctl start nginx

[root@rs2 ~]# yum isntall -y nginx
[root@rs2 ~]# echo 'Nginx to rs2' > /usr/share/nginx/html/index.html 
[root@rs2 ~]# systemctl start nginx

[root@ka1 ~]# hostname -I
10.0.0.10 10.0.0.100 
[root@ka1 ~]# 
[root@ka2 ~]# hostname -I
10.0.0.20 
[root@ka2 ~]# 


[root@client ~]# curl 10.0.0.100
Nginx to rs2
[root@client ~]# curl 10.0.0.100
Nginx to rs1
[root@client ~]# 

利用通知脚本,实现切换时,自动重服务

[root@ka1 ~]# vi /etc/keepalived/notify.sh  
#!/bin/bash
contact='calm_yzl@163.com'

notify() {
   mailsubject="$(hostname) to be $1,vip floating"
   mailbody="$(date + '%F %T'):vrrp transition,$(hostname) changed to be $1"
   echo "$mailbody" | mail -s "$mailsubject" $contact
}

case $1 in
master)
   notify master
   ;;
backup)
   notify backup
   ;;
fault)
   notify fault
   ;;
*)
   echo "Usage: $(basename $0) {master|backup|fault}"
   exit 1
   ;;
esac

[root@ka1 ~]# chmod a+x /etc/keepalived/notify.sh 
[root@ka1 ~]# scp /etc/keepalived/notify.sh 10.0.0.20:/etc/keepalived/notify.sh 
notify.sh                                                                 100%  416   189.8KB/s   00:00    
[root@ka1 ~]# 

以上是关于Keepalived实现单主模式的Nginx反向代理的高可用的主要内容,如果未能解决你的问题,请参考以下文章

Nginx + Keepalived(主备模式)实现负载均衡高可用浅析

单主模型的IPVS高可用和nginx+keepalived的双主高可用模型

Keepalived单主模式

keepalived实现高可用nginx反向代理

实现master/slave的Keepalived单主架构

Keepalived实现高可用Nginx反向代理