NginxLB单点问题之VIP漂移

root
233
文章
0
评论
2020年5月24日19:42:34 评论 3010字阅读10分2秒

NginxLB单点问题之VIP漂移

 

当前拓扑存在的问题:  Nginx具备单点故障

解决方案: 使用VIP漂移

原理就是两台负载均衡服务器使用相同的子网卡配置信息虚拟IP地址,通过shell脚本来检测nginx服务是否正常,一方服务dang掉后,另一台自动补充网卡信息
当前三台设备基本网卡信息:
LB:
LAMP1 & LAMP2:
观察: netstat -lnt ,目前Nginx监听的80在0.0.0.0上
           如果我们为LB增加一个VIP,且让80监听0.0.0.0,当真实网卡断掉,我们将VIP迁移至LB2 ,即可解决单点故障

在LB01上操作

在本地进行创建ens32:1网卡,并设置IP-192.168.1.110

[root@LB ~]# cd /etc/sysconfig/network-scripts/
[root@LB network-scripts]# cp -a ifcfg-ens32 ifcfg-ens32:1
[root@LB network-scripts]# vim ifcfg-ens32:1
TYPE=Ethernet
BOOTPROTO=static
DEVICE=ens32:1
ONBOOT=yes
IPADDR=192.168.1.110
NETMASK=255.255.255.0

[root@LB network-scripts]# ifup  ifcfg-ens32:1
[root@LB network-scripts]# ifconfig
ens32:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.110  netmask 255.255.255.0  broadcast 192.168.1.255
        ether 00:0c:29:c3:65:3e  txqueuelen 1000  (Ethernet)

关闭本地网卡,修改windows-hosts文件,测试bbs|blog.hewenfu.com访问结果

  • 解决本地服务器宕机导致单点故障
  • 在LB2上创建网卡和Nginx服务

在LB02上操作

[root@LB2 ~]# yum install -y vim gcc gcc-c++ lrzsz wget pcre-devel zlib-devel
[root@LB2 ~]# wget -c http://nginx.org/download/nginx-1.14.2.tar.gz
[root@LB2 ~]# useradd www -s /sbin/nologin -M
[root@LB2 ~]# tar xf nginx-1.14.2.tar.gz
[root@LB2 ~]# cd nginx-1.14.2
[root@LB2 nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --user=www --group=www
[root@LB2 nginx-1.14.2]# make && make install

[root@LB2 ~]# mkdir -p /usr/local/domain


[root@LB2 ~]# cd /etc/sysconfig/network-scripts/
[root@LB2 network-scripts]# cp -a ifcfg-ens32 ifcfg-ens32:1
[root@LB2 network-scripts]# vim ifcfg-ens32:1
TYPE=Ethernet
BOOTPROTO=static
DEVICE=ens32:1
ONBOOT=yes
IPADDR=192.168.1.110
NETMASK=255.255.255.0

[root@LB2 network-scripts]# ifup  ifcfg-ens32:1
[root@LB2 network-scripts]# ifconfig
ens32:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.110  netmask 255.255.255.0  broadcast 192.168.1.255
        ether 00:0c:29:c3:65:3e  txqueuelen 1000  (Ethernet)

将LB01上Nginx相关文件scp到LB2

[root@LB ~]# scp -r /usr/local/domain/* root@192.168.1.240:/usr/local/domain/
[root@LB ~]# scp /usr/local/nginx/conf/nginx.conf root@192.168.1.240:/usr/local/nginx/conf/

开发脚本,用于监听ens网卡,当ens宕机,VIP漂移

grep -v check 这个check,就是排除脚本,因为名字里带有check的信息,排除这个运行脚本

[root@LB2 ~]# vim auto_check_nginx_vip.sh
#!/bin/bash
#auto change service VIP
#########################################
NGINX_NUM=`ps -ef | grep nginx | grep -v grep | grep -v check|wc -l`
if [ $NGINX_NUM -eq 0 ]; then
   ifdown ens32:1 > /dev/null 2>&1
    rm -rf /etc/sysconfig/network-scripts/ifcfg-ens32:1
else
   ping -c 2 192.168.1.110 > /dev/null 2>&1
   if  [ $? -ne 0  ]; then
cat > /etc/sysconfig/network-scripts/ifcfg-ens32:1 <<EOF
TYPE=Ethernet
BOOTPROTO=static
DEVICE=ens32:1
ONBOOT=yes
IPADDR=192.168.1.110
NETMASK=255.255.255.0
EOF
        ifup ens32:1 >/dev/null 2>&1
   fi
fi

这个脚本负载均衡服务器上都要放到后台运行,两端同时运行

[root@LB2 ~]# scp auto_check_nginx_vip.sh 192.168.1.100:/root/
[root@LB2 ~]# while sleep 5 ;do bash auto_check_nginx_vip.sh ;done &
[root@LB ~]# while sleep 5 ;do bash auto_check_nginx_vip.sh ;done &

运行的方式

[root@LB2 ~]# while true ;do bash auto_check_nginx_vip.sh ;done &
或者
[root@LB2 ~]# while sleep 5 ;do bash auto_check_nginx_vip.sh ;done &

测试VIP是否漂移

运行shell脚本后,就是你任意停止一台nginx的负载均衡服务器的nginx服务,看数据能不能马上恢复

 

继续阅读
weinxin
我的微信
这是我的微信扫一扫
  • 文本由 发表于 2020年5月24日19:42:34
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
Nginx 日志分析 Nginx

Nginx 日志分析

Nginx web日志分析 Nginx是一款Web软件,生产环境应用场景: 1、做为web服务器,发布网站代码(处理静态); 2、做为负载均衡软件,可以均衡后端业务 3、做为会话保持软件,将某个会话固...
Nginx 性能调优 Nginx

Nginx 性能调优

Nginx 性能调优 Nginx.conf配置文件常用参数详解 #定义Nginx运行的用户和用户组 user  www  www; #启动进程,通常设置成和cpu的数量相等 worker_proces...
Nginx Rewrite Nginx

Nginx Rewrite

Nginx Rewrite Rewirte规则也称为规则重写,主要功能是实现浏览器访问HTTP URL的跳转(当访问不存在的路径时 -- 跳转到首页  通常而言,几乎所有的WEB服务器均可以支持URL...
NginxLB,LB动静分离 Nginx

NginxLB,LB动静分离

Nginx负载均衡 Nginx WEB默认发布静态页面,也可以均衡后端动态网站,用户发起HTTP请求,如果请求静态页面,Nginx直接处理并返回,如果请求的是动态页面,Nginx收到请求之后会进行判断...
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: