dabai-wang09

第50章 Nginx七层负载均衡

一、Nginx负载均衡基本概述

1)为什么要使用负载均衡

当我们的Web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷。使用多台Web服务器组成集群,前端使用Nginx负载均衡,将请求分散地打到后端服务器集群中,实现负载的分发。可以大大提升系统的吞吐率、请求性能、高容灾能力。

 

往往我们接触的最多的是SLB(Server Load Balance)负载均衡,实现最多的也是SLB,那么SLB它的调度节点和服务节点通常是在一个地域里面。它在这个小的逻辑地域里面决定了他对部分服务的实时性、响应性是非常好的。

因此,当海量用户请求过来以后,它同样是请求调度节点,调度节点将用户的请求转发给后端对应的服务节点,服务节点处理完请求后再转发给调度节点,调度节点最后响应给用户节点。这样就能实现一个均衡的作用。Nginx就是一个典型的SLB

2)负载均衡的叫法

2.1)负载均衡的叫法有很多:

负载均衡

负载

Load Balance

LB

 

2.2)公有云中叫法有:

阿里云负载均衡——SLB

青云负载均衡——QLB

腾讯云负载均衡——CLB

ucloud负载均衡——ULB

 

2.3)常见的负载均衡的软件有:

Nginx、Haproxy、LVS

 

 

2.4)负载均衡能实现的应用场景一: 四层负载均衡

所谓四层负载均衡指的是OSI七层模型中的传输层,那么传输层Nginx已经能支持TCP/IP的控制,所以只需要对客户端的请求进行TCP/IP协议的包转发就可以实现负载均衡,那么它的好处是性能非常快、只需要底层进行应用处理,而不需要进行一些复杂的逻辑。

 

 

2.5)负载均衡能实现的应用场景二:七层负载均衡

七层负载均衡是在应用层,它可以完成很多应用方面的协议请求。比如我们说的http应用的负载均衡,它可以实现http信息的改写、头信息的改写、安全应用规则控制、URL匹配规则控制、以及转发、rewrite等等的规则,所以在应用层的服务里面,我们可以做的内容就更多,那么Nginx则是一个典型的七层负载均衡SLB。

 

 

2.6)四层负载均衡与七层负载均衡区别

四层负载均衡数据包在底层就进行了分发,而七层负载均衡数据包则是在最顶层进行分发,由此可以看出,七层负载均衡效率没有四负载均衡高。

但七层负载均衡更贴近于服务,如:http协议就是七层协议,我们可以用Nginx可以做会话保持,URL路径规则匹配、head头改写等等,这些是四层负载均衡无法实现的。

注意:四层负载均衡不识别域名,七层负载均衡识别域名。

 

二、Nginx负载均衡配置场景

Nginx要实现负载均衡需要用到proxy_pass代理模块配置。

Nginx负载均衡与Nginx代理不同地方在于,Nginx的一个location仅能代理一台服务器,而Nginx负载均衡则是将客户端请求代理转发至一组upstream虚拟服务池。

 

 

 

1)Nginx upstream虚拟配置语法

Syntax: upstream name { ... }
Default: -
Context: http


# upstream例
upstream backend {
  server backend1.example.com       weight=5;
  server backend2.example.com:8080;
  server unix:/tmp/backend3;
  server backup1.example.com:8080   backup;
}
server {
  location / {
      proxy_pass http://backend;
  }
}

2)Nginx负载均衡示例

2.1)环境准备

角色 外网IP(NAT) 内网IP(LAN) 主机名
lb01 eth0:10.0.0.5 eth1:172.16.1.5 lb01
Web01 eth0:10.0.0.7 eth1:172.16.1.7 web01
Web02 eth0:10.0.0.8 eth1:172.16.1.8 web02

2.2)Web01服务器上配置nginx


[root@web01 images]# cd ~
[root@web01 ~]# cd /etc/nginx/conf.d/
#配置nginx
[root@web01 conf.d]# vim node.conf
server{
      listen 80;
      server_name node.drz.com;
      location / {
              root /node;
              index index.html;
      }
}

[root@web01 ~]# cd /node
-bash: cd: /node: No such file or directory
#创建node目录
[root@web01 ~]# mkdir /node
#新建index.html
[root@web01 ~]# echo "Web01..." > /node/index.html
#重启nginx
[root@web01 ~]# systemctl restart nginx

​=========================================================

 

2.3)Web02服务器上配置nginx


[root@web02 ROOT]# cd ~
[root@web02 ~]# cd /etc/nginx/conf.d/
#配置nginx
[root@web02 conf.d]# vim node.conf
server {
  listen 80;
  server_name node.drz.com;
  location / {
      root /node;
      index index.html;
  }
}

[root@web02 conf.d]# cd ~
#创建node目录
[root@web02 ~]# mkdir /node
#新建index.html
[root@web02 ~]# echo "Web02..." > /node/index.html
#重启nginx
[root@web02 ~]# systemctl restart nginx

=================================================​

 

2.4)配置Nginx负载均衡

[root@lb01 code]# cd /etc/nginx/conf.d/

#配置nginx负载均衡
[root@lb01 conf.d]# vim node_proxy.conf
upstream node {
  server 172.16.1.7:80;
  server 172.16.1.8:80;
}
server {
  listen 80;
  server_name node.drz.com;

  location / {
      proxy_pass http://node;
      include proxy_params;
  }
}

#准备Nginx负载均衡调度使用的proxy_params
[root@lb01 conf.d]# vim /etc/nginx/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;

proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;

window键+R——drivers——etc——hosts, 修改内容为 10.0.0.5   node.drz.com

#重启nginx
[root@lb01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 conf.d]# systemctl restart nginx

打开浏览器,输入 node.drz.com

 

 

2.5)负载均衡常见典型故障

如果后台服务连接超时,Nginx是本身是有机制的,如果出现一个节点down掉的时候,Nginx会更据你具体负载均衡的设置,将请求转移到其他的节点上,但是,如果后台服务连接没有down掉,但是返回错误异常码了,如:504、502、500,此时你需要加一个负载均衡的设置,如下:proxy_next_upstream http_500 | http_502 | http_503 | http_504 |http_404;意思是,当其中一台返回错误码404,500...等错误时,可以分配到下一台服务器程序继续处理,提高平台访问成功率。

[root@lb01 conf.d]# vim node_proxy.conf 

upstream node {
  server 172.16.1.7:80;
  server 172.16.1.8:80;
}
server {
  listen 80;
  server_name node.drz.com;

  location / {
      proxy_pass http://node;
      proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
      include proxy_params;
  }
}

 

三、Nginx负载均衡调度算法

调度算法 概述
轮询 按时间顺序逐一分配到不同的后端服务器(默认)
weight 加权轮询,weight值越大,分配到的访问几率越高
ip_hash 每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器
url_hash 按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器
least_conn 最少链接数,那个机器链接数少就分发

 

Nginx负载均衡[rr]权重轮询具体配置

upstream load_pass {
  server 10.0.0.7:80;
  server 10.0.0.8:80;
}

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

 

Nginx负载均衡[wrr]权重轮询具体配置

upstream load_pass {
  server 10.0.0.7:80 weight=5;
  server 10.0.0.8:80;
}

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

 

Nginx负载均衡ip_hash

具体配置不能和weight一起使用。

#如果客户端都走相同代理, 会导致某一台服务器连接过多
upstream load_pass {
  ip_hash;
  server 10.0.0.7:80 weight=5;
  server 10.0.0.8:80;
}

 

 

四、Nginx负载均衡后端状态

 

后端Web服务器在前端Nginx负载均衡调度中的状态

状态 概述
down 当前的server暂时不参与负载均衡
backup 预留的备份服务器
max_fails 允许请求失败的次数
fail_timeout 经过max_fails失败后,服务暂停时间
max_conns 限制最大的接收连接数

 

测试down状态测试该Server不参与负载均衡的调度

upstream load_pass {
  #不参与任何调度, 一般用于停机维护
  server 10.0.0.7:80 down;
}

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

测试backup以及down状态

upstream load_pass {
  server 10.0.0.7:80 down;
  server 10.0.0.8:80 backup;
  server 10.0.0.9:80 max_fails=1 fail_timeout=10s;
}

location / {
  proxy_pass http://load_pass;
  include proxy_params;
}

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

 

测试max_fails失败次数和fail_timeout多少时间内失败多少次则标记down

upstream load_pass {
  server 10.0.0.7:80;
  server 10.0.0.8:80 max_fails=2 fail_timeout=10s;
}

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

 

测试max_conns最大TCP连接数

upstream load_pass {
  server 10.0.0.7:80;
  server 10.0.0.8:80 max_conns=1;
}

 

五、Nginx负载均衡健康检查

在Nginx官方模块提供的模块中,没有对负载均衡后端节点的健康检查模块,但可以使用第三方模块。 nginx_upstream_check_module来检测后端服务的健康状态。

1)安装依赖包

[root@lb02 ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch

 

2)下载nginx源码包以及nginx_upstream_check模块第三方模块

[root@lb02 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@lb02 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip

 

3)解压nginx源码包以及第三方模块

[root@lb02 ~]# tar xf nginx-1.14.2.tar.gz
[root@lb02 ~]# unzip master.zip

 

4)进入nginx目录,打补丁(nginx的版本是1.14补丁就选择1.14的,p1代表在nginx目录,p0是不在nginx目录)

[root@lb02 ~]# cd nginx-1.14.2/
[root@lb02 nginx-1.14.2]# patch -p1 <../nginx_upstream_check_module-master/check_1.14.0+.patch
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/root/nginx_upstream_check_module-master --with-cc-opt=\'-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC\' --with-ld-opt=\'-Wl,-z,relro -Wl,-z,now -pie\'
[root@lb02 nginx-1.14.2]# make && make install

 

5)在已有的负载均衡上增加健康检查的功能

[root@lb01 conf.d]# cat proxy_web.conf
upstream web {
  server 172.16.1.7:80 max_fails=2 fail_timeout=10s;
  server 172.16.1.8:80 max_fails=2 fail_timeout=10s;
  check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
  #interval 检测间隔时间,单位为毫秒
  #rise     表示请求2次正常,标记此后端的状态为up
  #fall     表示请求3次失败,标记此后端的状态为down
  #type     类型为tcp
  #timeout   超时时间,单位为毫秒
}

server {
  listen 80;
  server_name web.drz.com;
  location / {
      proxy_pass http://web;
      include proxy_params;
  }

  location /upstream_check {
      check_status;
  }
}

 

分类:

技术点:

相关文章: