nginx负载均衡

nginx负载均衡

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

四层负载均衡(传输层)

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

在这里插入图片描述
在这里插入图片描述

七层负载均衡

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

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

Nginx upstream虚拟配置语法

Syntax: upstream name { ... }
Default: -
Context: http
#upstream例
upstream backend {
      server backend1.example.com         
      server backend2.example.com:8080;
      server unix:/tmp/backend3;
      server backup1.example.com:8080  backup;
}
server {
    location / {
         proxy_pass http://backend;
    }
}
  • 环境准备
角色地址主机名
LB01ens33:192.168.13610lb01
web01ens33:192.168.136.20web01
web02ens33:192.168.136.30web02

Web01服务器上配置nginx

[root@web01 ~]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# vim node.conf
server {
 listen 80;
 server_name node.test.com;
 location / {
   root /code/node;
   index index.html;
 }
}
[root@web01 conf.d]# mkdir -p /code/node
[root@web01 conf.d]# echo "web01 ..." > /code/node/index.html
[root@web01 conf.d]# systemctl restart ngin

Web02服务器上配置nginx

[root@web02 ~]# cd /etc/nginx/conf.d/
[root@web02 conf.d]# vim node.conf
server {
 listen 80;
 server_name node.test.com;
 location / {
   root /code/node;
   index index.html;
 }
}
[root@web02 conf.d]# mkdir -p /code/node
[root@web02 conf.d]# echo "web02 ..." > /code/node/index.html
[root@web02 conf.d]# systemctl restart nginx

配置Nginx负载均衡

[root@lb01 ~]# cd /etc/nginx/conf.d/
[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;

[root@lb01 conf.d]# vim node_proxy.conf
upstream node {
 server 192.168.136.20:80; 
 server 192.168.136.30:80;
}
server {
 listen 80;
 server_name node.test.com;
 location / {
   proxy_pass http://node;
   include proxy_params; 
   proxy_next_upstream error timeout http_403;
 }
}
[root@lb01 conf.d]# nginx -t
[root@lb01 conf.d]# systemctl restart nginx

Nginx负载均衡调度算法

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

Nginx负载均衡ip_hash

  • 具体配置不能和weight一起使用。
#如果客户端都走相同代理, 会导致某一台服务器连接过多
upstream load_pass {
 ip_hash;
 server 192.168.175.10:80;
 server 192.168.175.20:80;
}

Nginx负载均衡后端状态

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

  • 测试backup以及down状态

upstream load_pass {
 server 192.168.136.10:80 backup;
 server 192.168.136.20: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 192.168.136.10:80;
     server 192.168.136.20:80 max_fails=2 fail_timeout=10s;
    }
    
  • 测试max_conns最大TCP连接数

upstream load_pass {
 server 192.168.136.10:80;
 server 192.168.136.20:80 max_conns=1;
}

Nginx负载均衡健康检查

  • 第三方模块项目地址:https://github.com/yaoweibin/nginx_upstream_check_module
[root@lb01 ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch 
  • 下载nginx源码包以及nginx_upstream_check模块第三方模块
[root@lb01 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@lb01 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
  • 解压nginx源码包以及第三方模块
[root@lb01 ~]# tar xf nginx-1.14.2.tar.gz
[root@lb01 ~]# unzip master.zip
  • 进入nginx目录,打补丁(nginx的版本是1.14补丁就选择1.14的,p1代表在nginx目录,p0是不在nginx目录
[root@lb01 ~]# cd nginx-1.14.2
[root@lb01 nginx-1.14.2]# patch -p1 <../nginx_upstream_check_module-master/check_1.14.0+.patch
[root@lb01 nginx-1.14.2]#./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@lb01 conf.d]# vim proxy_web.conf
upstream node {
 server 192.168.175.10:80 max_fails=2 fail_timeout=10s;
 server 192.168.175.20: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 node.test.com;
    
    location / {
         proxy_pass http://node;
         include proxy_params;
 }
 
 location /upstream_check {
      check_status;
 }
}

会话保持配置

  • 环境安装
yum install -y epel-release
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
cat <<EOF >/etc/yum.repos.d/php.repo
[php]
name = php Repository
baseurl = http://repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0
EOF
yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb 
sed -i '/^user/c user = nginx' /etc/php-fpm.d/www.conf
sed -i '/^group/c user =nginx' /etc/php-fpm.d/www.conf
systemctl start php-fpm
systemctl enable php-fpm
  • 配置Nginx
[root@web01 ~]# vim /etc/nginx/conf.d/php.conf
server{
    listen 80;
    server_name php.test.com;
    root /code/phpMyAdmin-4.8.4-all-languages;

    location / {
        index index.php index.html;
}

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
              }
}

[root@web01 ~]# nginx -t
[root@web01 ~]# systemctl restart nginx
  • 安装phpmyadmin (web01和web02上都装)
[root@web01 ~]# cd /code
[root@web01 code]# wget https://files.phpmyadmin.net/phpMyAdmin/4.8.4/phpMyAdmin-4.8.4-all-languages.zip
[root@web01 code]# unzip phpMyAdmin-4.8.4-all-languages.zip
  • 配置phpmyadmin连接远程的数据库,这个数据库可以用代理服务器安装后做测试
[root@web01 code]# cd phpMyAdmin-4.8.4-all-languages/
[root@web01 phpMyAdmin-4.8.4-all-languages]# cp config.sample.inc.php config.inc.php
[root@web01 phpMyAdmin-4.8.4-all-languages]# vim config.inc.php
$cfg['Servers'][$i]['host'] = '192.168.136.10'
  • 配置权限
[root@web01 ~]# chown -R nginx.nginx /var/lib/php/ 
  • 查看服务器上的session
[root@web01 ~]# ll /var/lib/php/session/
总用量 4
-rw------- 1 www www 2625 711 20:08 sess_aa3bdfab93197f49bbfddb15cb41a595
  • 将web01上配置好的phpmyadmin以及nginx的配置文件推送到web02主机上
[root@web01 code]# scp -rp phpMyAdmin-4.8.4-all-languages  root@192.168.36.30:/code/
[root@web01 code]# scp /etc/nginx/conf.d/php.conf root@192.168.136.30:/etc/nginx/conf.d/
  • 配置权限
[root@web01 ~]# chown -R nginx.nginx /var/lib/php/ 

在这里插入图片描述

  • 接入负载均衡
[root@lb01 ~]# vim /etc/nginx/conf.d/proxy_php.com.conf
upstream php {
   server 192.168.136.20:80;
   server 192.168.136.30:80;
}
server {
   listen 80;
   server_name php.test.com;
   location / {
       proxy_pass http://php;
       include proxy_params;
   }
}
[root@lb01 ~]# nginx -t
[root@lb01 ~]# systemctl restart nginx
  • 使用负载均衡的轮询功能之后,会发现,如果将session保存在本地文件的话,永远都登录不上去。(session不一致导致的)

使用redis解决会话登录问题

  • 安装redis缓存数据库
[root@lb01 ~]# yum -y install redis 
  • 配置redis监听在0.0.0.0网段上
[root@lb01 ~]# sed -i '/^bind/c bind 127.0.0.1 0.0.0.0' /etc/redis.conf 
[root@lb01 ~]# systemctl start redis
[root@lb01 ~]# systemctl enable redis
  • php配置session连接redis
[root@web01 ~]# vim /etc/php.ini
session.save_handler = redis
session.save_path = "tcp://192.168.136.10:6379"
;session.save_path = "tcp://192.168.136.10:6379?auth=123" #如果redis存在密码,则使
用该方式
session.auto_start = 1
[root@web01 ~]# vim /etc/php-fpm.d/www.conf
#注释php-fpm.d/www.conf里面的两条内容,否则session内容会一直写入/var/lib/php/session目
录中
;php_value[session.save_handler] = files
;php_value[session.save_path]   = /var/lib/php/session
  • 重启php-fpm
[root@web01 ~]# systemctl restart php-fpm
  • 将web01上配置好的文件推送到web02
[root@web01 ~]# scp /etc/php.ini root@192.168.175.30:/etc/php.ini
[root@web01 ~]# scp /etc/php-fpm.d/www.conf root@192.168.175.30:/etc/php-fpm.d/www.conf
  • 在web02上重启php-fpm
[root@web02 ~]# systemctl restart php-fpm 
  • redis查看数据
[root@lb01 ~]# redis-cli
127.0.0.1:6379> keys *
1) "PHPREDIS_SESSION:7e772fe37b1488069e3c54e419df7eda"

Nginx四层负载均衡概述

四层负载均衡是基于传输层协议包来封装的(如:TCP/IP),那我们前面使用到的七层是指的应用层,他的组装在四层的基础之上,无论四层还是七层都是指的OSI网络模型。
四层+七层来做负载均衡,四层可以保证七层的负载均衡的高可用性;如:nginx就无法保证自己的服务高可用,需要依赖LVS或者keepalive。
如:tcp协议的负载均衡,有些请求是TCP协议的(mysql、ssh),或者说这些请求只需要使用四层进行端口的转发就可以了,所以使用四层负载均衡。

四层+七层构建大规模集群架构使用场

在这里插入图片描述

四层负载均衡总结

  • 四层负载均衡仅能转发TCP/IP协议、UDP协议、通常用来转发端口,如:tcp/22、udp/53;
  • 四层负载均衡可以用来解决七层负载均衡端口限制问题;(七层负载均衡最大使用65535个端口号)
  • 四层负载均衡可以解决七层负载均衡高可用问题;(多台后端七层负载均衡能同事的使用)
  • 四层的转发效率比七层的高得多,但仅支持tcp/ip协议,不支持http和https协议;
  • 通常大并发场景通常会选择使用在七层负载前面增加四层负载均衡。

Nginx四层负载均衡场景实践

Nginx如何配置四层负载均衡
1、通过访问负载均衡的5555端口,实际是后端的web01的22端口在提供服务;
2、通过访问负载均衡的6666端口,实际是后端的mysql的3306端口在提供服务。

  • 创建存放四层负载均衡配置文件的目录
[root@lb01 ~]# vim /etc/nginx/nginx.conf
include /etc/nginx/conf.c/*.conf;
#include /etc/nginx/conf.d/*.conf;
[root@lb01 ~]# mkdir /etc/nginx/conf.c
  • 配置四层负载均衡
[root@lb01 ~]# vim /etc/nginx/conf.c/lb_domain.conf
stream {
 upstream lb {
     server 192.168.136.20:80 weight=5 max_fails=3 fail_timeout=30s;
     server 192.168.136.30:80 weight=5 max_fails=3 fail_timeout=30s;
 }
 server {
     listen 80;
     proxy_connect_timeout 3s;
     proxy_timeout 3s;
     proxy_pass lb;
 }
}
  • 四层负载均衡开启日志
    • 四层负载均衡是没有access的日志的,因为在nginx.conf的配置中,access的日志格式是配置在http下的,而四层负载均衡配置实在http以外的
    • 如果需要日志则需要配置在stream下面
[root@lb01 ~]# cd /etc/nginx/conf.c/
[root@lb01 conf.c]# vim lb_domain.conf
stream {
 log_format proxy '$remote_addr $remote_port - [$time_local] $status
$protocol '
         '"$upstream_addr" "$upstream_bytes_sent"
"$upstream_connect_time"' ;
 access_log /var/log/nginx/proxy.log proxy;
 upstream lb {
     server 192.168.175.20:80 weight=5 max_fails=3 fail_timeout=30s;
     server 192.168.175.30:80 weight=5 max_fails=3 fail_timeout=30s;
 }
 server {
     listen 80;
     proxy_connect_timeout 3s;
     proxy_timeout 3s;
     proxy_pass lb;
 }
}
[root@lb01 conf.c]# nginx -t
[root@lb01 conf.c]# systemctl restart nginx

#浏览器访问域名或者IP,查看日志
[root@lb01 conf.c]# tail -f /var/log/nginx/proxy.log
192.168.175.1 55295 - [11/Jul/2021:23:27:09 +0800] 200 TCP "192.168.175.20:80"
"0" "0.001"
192.168.175.1 64066 - [11/Jul/2021:23:27:13 +0800] 200 TCP "192.168.175.30:80"
"0" "0.000"
192.168.175.1 58528 - [11/Jul/2021:23:27:16 +0800] 200 TCP "192.168.175.20:80"
"0" "0.001"
192.168.175.1 65099 - [11/Jul/2021:23:27:20 +0800] 200 TCP "192.168.175.30:80"
"0" "0.000"
192.168.175.1 55919 - [11/Jul/2021:23:27:20 +0800] 200 TCP "192.168.175.20:80"
"6460" "0.001"

Nginx四层负载均衡端口转发

  • 配置nginx四层负载均衡实现tcp的转发
[root@lb01 ~]# vim /etc/nginx/conf.c/lb_domain.conf
stream {
 log_format proxy '$remote_addr $remote_port - [$time_local] $status
$protocol '
           '"$upstream_addr" "$upstream_bytes_sent"
"$upstream_connect_time"' ;
 access_log /var/log/nginx/proxy.log proxy;
#定义转发ssh的22端口
 upstream ssh {
     server 192.168.136.20:22;
 }
#定义转发mysql的3306端口
 upstream mysql {
     server 192.168.136.30:3306;
 }
 server {
     listen 5555;
     proxy_connect_timeout 3s;
     proxy_timeout 300s;
     proxy_pass ssh;
 }
 server {
     listen 6666;
     proxy_connect_timeout 3s;
     proxy_timeout 3s;
     proxy_pass mysql;
 }
}
[root@lb01 ~]# nginx -t
[root@lb01 ~]# systemctl restart nginx


‘“ u p s t r e a m a d d r " " upstream_addr" " upstreamaddr""upstream_bytes_sent”
“$upstream_connect_time”’ ;
access_log /var/log/nginx/proxy.log proxy;
#定义转发ssh的22端口
upstream ssh {
server 192.168.136.20:22;
}
#定义转发mysql的3306端口
upstream mysql {
server 192.168.136.30:3306;
}
server {
listen 5555;
proxy_connect_timeout 3s;
proxy_timeout 300s;
proxy_pass ssh;
}
server {
listen 6666;
proxy_connect_timeout 3s;
proxy_timeout 3s;
proxy_pass mysql;
}
}
[root@lb01 ~]# nginx -t
[root@lb01 ~]# systemctl restart nginx

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值