Nginx是如何实现七层的负载均衡的
admin
2023-03-24 05:20:02
0次

下文给大家带来Nginx是如何实现七层的负载均衡的,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用在行业内累计的经验来做一个解答。

 Nginx是如何实现七层的负载均衡的

Nginx实现七层的负载均衡

调度到不同组后端云服务器
1. 动静分离
2. 网站进行分区
=================================================================================

拓扑结构

                [vip: 20.20.20.20]

            [LB1 Nginx]      [LB2 Nginx]
            192.168.1.2       192.168.1.3

[index]      [milis]     [videos]     [p_w_picpaths]      [news]
1.11         1.21         1.31         1.41         1.51
1.12          1.22         1.32         1.42         1.52
1.13          1.23         1.33         1.43         1.53
...            ...          ...          ...          ...
/web       /web/milis    /web/videos   /web/p_w_picpaths   /web/news
index.html   index.html   index.html                 index.html


一、实施过程
方案一 根据站点分区进行调度
http {
   upstream index {
       server 192.168.1.11:80 weight=1 max_fails=2 fail_timeout=2;
       server 192.168.1.12:80 weight=2 max_fails=2 fail_timeout=2;
       server 192.168.1.13:80 weight=2 max_fails=2 fail_timeout=2;
      }
     
   upstream milis {
       server 192.168.1.21:80 weight=1 max_fails=2 fail_timeout=2;
       server 192.168.1.22:80 weight=2 max_fails=2 fail_timeout=2;
       server 192.168.1.23:80 weight=2 max_fails=2 fail_timeout=2;
      }
     
    upstream videos {
       server 192.168.1.31:80 weight=1 max_fails=2 fail_timeout=2;
       server 192.168.1.32:80 weight=2 max_fails=2 fail_timeout=2;
       server 192.168.1.33:80 weight=2 max_fails=2 fail_timeout=2;
      }
     
    upstream p_w_picpaths {
       server 192.168.1.41:80 weight=1 max_fails=2 fail_timeout=2;
       server 192.168.1.42:80 weight=2 max_fails=2 fail_timeout=2;
       server 192.168.1.43:80 weight=2 max_fails=2 fail_timeout=2;
      }
     
     upstream news {
       server 192.168.1.51:80 weight=1 max_fails=2 fail_timeout=2;
       server 192.168.1.52:80 weight=2 max_fails=2 fail_timeout=2;
       server 192.168.1.53:80 weight=2 max_fails=2 fail_timeout=2;
      }
     
    server {
          location / {
      proxy_pass http://index;
      }
     
      location  /news {
      proxy_pass http://news;
      }
     
      location /milis {
      proxy_pass http://milis;
      }
     
      location ~* \.(wmv|mp4|rmvb)$ {
      proxy_pass http://videos;
      }
     
      location ~* \.(png|gif|jpg)$ {
      proxy_pass http://p_w_picpaths;
      }
}


方案二 根据动静分离进行调度
http {
    upstream htmlservers {
       server 192.168.1.1:80 weight=1 max_fails=2 fail_timeout=2;
       server 192.168.1.2:80 weight=2 max_fails=2 fail_timeout=2;
        }
       
upstream phpservers {
       server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2;
       server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2;
        }
       
     server {
      location ~* \.html$ {
      proxy_pass http://htmlservers;
      }
     
      location ~* \.php$ {
      proxy_pass http://phpservers;
      }
     }
}


二、Keepalived实现调度器HA
注:主/备调度器均能够实现正常调度
1. 主/备调度器安装软件
[root@master ~]# yum -y install keepalived
[root@backup ~]# yum -y install keepalived

2. Keepalived
BACKUP1
[root@uplook ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
  router_id director1 //辅助改为director2
}

vrrp_instance VI_1 {
   state BACKUP
   nopreempt    
   interface eth0 //心跳接口,尽量单独连接心跳
   virtual_router_id 80//整个集群的调度器一致
   priority 100 //辅助改为50
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass 1111
   }
   virtual_ipaddress {
       20.20.20.20
   }
}

BACKUP2


3. 启动KeepAlived(主备均启动)
[root@uplook ~]# chkconfig keepalived on
[root@uplook ~]# service keepalived start
[root@uplook ~]# ip addr

到此:
可以解决心跳故障keepalived
不能解决Nginx服务故障



4. 扩展对调度器Nginx健康检查(可选)
思路:
让Keepalived以一定时间间隔执行一个外部脚本,脚本的功能是当Nginx失败,则关闭本机的Keepalived

a. script
[root@master ~]# cat /etc/keepalived/check_nginx_status.sh
#!/bin/bash
/usr/bin/curl -I http://localhost &>/dev/null
if [ $? -ne 0 ];then
/etc/init.d/keepalived stop
fi

[root@master ~]# chmod a+x /etc/keepalived/check_nginx_status.sh

b. keepalived使用script
! Configuration File for keepalived

global_defs {
  router_id director1
}

vrrp_script check_nginx {
  script "/etc/keepalived/check_nginx_status.sh"
  interval 5
}


vrrp_instance VI_1 {
   state BACKUP
   interface eth0
   nopreempt
   virtual_router_id 90
   priority 100
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass uplook
   }
   
   virtual_ipaddress {
       192.168.1.80
   }

   track_script {
       
check_nginx
   }

}

注:必须先启动nginx,再启动keepalived




     调度到同一组后端服务器
网站没按业务/版块拆分,所有后端服务器提供整站代码。
=================================================================================

拓扑结构

              [LB Nginx]
              20.20.20.20
              192.168.1.2

[httpd]         [httpd]      [httpd]
192.168.1.3    192.168.1.4    192.168.1.5


实施过程
1. nginx
http {
   upstream httpservers {
       server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2;
       server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2;
       server 192.168.1.5:80 weight=2 max_fails=2 fail_timeout=2;
       server 192.168.1.100:80 backup;         等3、4、5 挂掉100上线
   }

   server {
      location  / {
               proxy_pass  http://httpservers;
               proxy_set_header X-Real-IP $remote_addr;
      }
    }    
}

2. Apache LogFormat 可选
LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

3. Nginx LogFormat

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

看了以上关于Nginx是如何实现七层的负载均衡的,如果大家还有什么地方需要了解的可以在行业资讯里查找自己感兴趣的或者找我们的专业技术工程师解答的,技术工程师在行业内拥有十几年的经验了。官网链接www.yisu.com

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...