Nginx动态负载均衡

2023-10-31

Nginx一般作为反向代理服务器来实现反向代理来转发处理请求,同时也可以作为静态资源服务器来加快静态资源的获取和处理。

1.正向代理与反向代理

正向代理:

       正向代理 是一个位于客户端和原始服务器之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。客户端必须要进行一些特别的设置才能使用正向代理。

作用:访问原来无法访问的资源;可以做缓存,加速访问资源;对客户端访问授权,上网进行认证;代理可以记录用户访问记录,对外隐藏用户信息

反向代理:

反向代理是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。客户端是无感知代理的存在的,反向代理对外都是透明的,访问者者并不知道自己访问的是一个代理。因为客户端不需要任何配置就可以访问。

作用:保证内网的安全,可以使用反向代理提供WAF功能,阻止web攻击;负载均衡,通过反向代理服务器来优化网站的负载

2.Nginx架构

如图所示:Nginx采用多进程方式,一个Master与多个Worker进程。客户端访问请求通过负载均衡配置来转发到不同的后端服务器依次来实现负载均衡。

3.Nginx负载均衡策略

Nginx负载均衡策略主要有 轮询,加权轮询,最少连接数以及IP Hash。

负载均衡配置文件如下:

轮询策略: 实现请求的按顺序转发,即从服务srv1--srv2--srv3依次来处理请求

When the load balancing method is not specifically configured, it defaults to round-robin. All requests are proxied to the server group myapp1, and nginx applies HTTP load balancing to distribute the requests.

加权轮询策略: 请求将按照服务器的设置权重来实现请求转发和处理,如下所示,最终请求处理数将为3:1:1

With the round-robin in particular it also means a more or less equal distribution of requests across the servers — provided there are enough requests, and when the requests are processed in a uniform manner and completed fast enough.

最少连接数策略:  请求将转发到连接数较少的服务器上

With the least-connected load balancing, nginx will try not to overload a busy application server with excessive requests, distributing the new requests to a less busy server instead.

Ip Hash策略:  web服务需要共享session,使用该策略可以实现某一客户端的请求固定转发至某一服务器

If there is the need to tie a client to a particular application server — in other words, make the client’s session “sticky” or “persistent” in terms of always trying to select a particular server — the ip-hash load balancing mechanism can be used.

With ip-hash, the client’s IP address is used as a hashing key to determine what server in a server group should be selected for the client’s requests. This method ensures that the requests from the same client will always be directed to the same server except when this server is unavailable.

由以上可知,轮询只是简单实现请求的顺序转发,并没有考虑不同服务器的性能差异;加权轮询设置了初始时服务器的权重,但是没有考虑运行过程中的服务器状态;IP Hash保证同一个客户端请求转发到同一个后台服务器实现了session保存,然而当某一后台服务器发生故障时,某些客户端将访问失败;最少连接数只是考虑了后端服务器的连接数情况,并没有完全考虑服务器的整体性能。

4.动态负载均衡

动态负载均衡策略类似于加权轮询策略,可以通过对于后端服务器集群的状态监测,量化不同服务器的性能差异,来周期性调整服务器的比重来实现权重的动态调整。

主要待解决的问题:

1.后端服务器集群的性能量化计算来分配不同权重。这方面有很多研究,以后慢慢学习,主要是从后端服务器的cpu,内存,io,网络以及连接数等方面来量化不同的服务器性能。

2.如何实现权重的动态调整,之前一直想写一个nginx第三方模块来实现,但是难度太大。

这里搜集了一些资料,可以通过consul+nginx-upsync-module模块来实现权重的动态调整

https://github.com/weibocom/nginx-upsync-module

利用consul模块来实现权重服务的配置信息

nginx周期性从consul中读取配置信息来动态改变权重

5.consul+nginx-upsync-module实现动态负载均衡

consul安装

下载consul:https://releases.hashicorp.com/consul

关闭防火墙:

启动consul:    ./consul agent -advertise=10.112.99.152 -client=0.0.0.0 -dev 

访问图形化界面:  http://10.112.99.152:8500

nginx安装

http://nginx.org/download/

解压:tar -zxvf nginx-1.9.10.tar.gz

配置环境;

nginx-upsync-module安装  

https://github.com/weibocom/nginx-upsync-module

解压该模块:

unzip nginx-upsync-module-master.zip

将nginx-upsync-module编译到nginx中

cd nginx-1.9.10

安装第三方库

yum -y install openssl openssl-devel

注意下面安装路径应与上面路径对应

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --http-client-body-temp-path=/data/dynamic/nginx/client/ --http-proxy-temp-path=/data/dynamic/nginx/proxy/ --http-fastcgi-temp-path=/data/dynamic/nginx/fcgi/ --http-uwsgi-temp-path=/data/dynamic/nginx/uwsgi --http-scgi-temp-path=/data/dynamic/nginx/scgi --with-pcre --add-module=../nginx-upsync-module-master

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --http-client-body-temp-path=/data/dynamic/nginx/client/ --http-proxy-temp-path=/data/dynamic/nginx/proxy/ --http-fastcgi-temp-path=/data/dynamic/nginx/fcgi/ --http-uwsgi-temp-path=/data/dynamic/nginx/uwsgi --http-scgi-temp-path=/data/dynamic/nginx/scgi --with-pcre --add-module=../nginx-upsync-module-master

make && make install  编译成功

动态负载均衡配置

nginx配置文件:

upstream test {
        server 127.0.0.1:11111;
        upsync 127.0.0.1:8500/v1/kv/upstreams/test/ upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
        upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;
        include /usr/local/nginx/conf/servers/servers_test.conf;
    }
  
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        location = / {
            proxy_pass http://test;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

启动nginx服务,如果报错

则添加nginx用户

useradd nginx -s /sbin/nologin -M

再次启动

/usr/local/nginx/sbin/nginx

在服务器的8088,8089端口启动两个tomcat

向consul中写入后端服务器信息:

curl -X PUT http://10.112.99.152:8500/v1/kv/upstreams/test/10.112.99.152:8088

curl -X PUT http://10.112.99.152:8500/v1/kv/upstreams/test/10.112.99.152:8089

观察配置文件:

cat /usr/local/nginx/conf/servers/servers_test.conf

此时访问ip实习负载均衡,可以通过改变consul中数据来实现服务器数量以及权重的改变

其余consul中参数配置命令:

upsync参数:  配置consul   key  value  后端服务器ip  port 权重

syntax: upsync $consul/etcd.api.com:$port/v1/kv/upstreams/$upstream_name/ [upsync_type=consul/etcd] [upsync_interval=second/minutes] [upsync_timeout=second/minutes] [strong_dependency=off/on]

The parameters' meanings are:

  • upsync_interval

    pulling servers from consul/etcd interval time.

  • upsync_timeout

    pulling servers from consul/etcd request timeout.

  • upsync_type

    pulling servers from conf server type.

  • strong_dependency

    when strong_dependency is on, nginx will pull servers from consul/etcd every time when nginx start up or reload.

upsync_dump_path参数: 保存consul配置信息

syntax: upsync_dump_path $path

default: /tmp/servers_$host.conf

context: upstream

description: dump the upstream backends to the $path.

利用http命令向consul中写入后端服务器以及权重信息:

增加服务器指令:  默认权重  以及添加权重

curl -X PUT http://$consul_ip:$port/v1/kv/upstreams/$upstream_name/$backend_ip:$backend_port
default: weight=1 max_fails=2 fail_timeout=10 down=0 backup=0;
    curl -X PUT -d "{\"weight\":1, \"max_fails\":2, \"fail_timeout\":10}" http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend_ip:$backend_port
or
    curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10}' http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend

删除服务器:

curl -X DELETE http://$consul_ip:$port/v1/kv/upstreams/$upstream_name/$backend_ip:$backend_port

调整服务器的权重:

curl -X PUT -d "{\"weight\":2, \"max_fails\":2, \"fail_timeout\":10}" http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend_ip:$backend_port
or
    curl -X PUT -d '{"weight":2, "max_fails":2, "fail_timeout":10}' http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend_ip:$backend_port

标记服务器停止服务:

  curl -X PUT -d "{\"weight\":2, \"max_fails\":2, \"fail_timeout\":10, \"down\":1}" http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend_ip:$backend_port
or
    curl -X PUT -d '{"weight":2, "max_fails":2, "fail_timeout":10, "down":1}' http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend_ip:$backend_port

检查服务器状态:

curl http://$consul_ip:$port/v1/kv/upstreams/$upstream_name?recurse

nginx管理命令如下:

nginx文件目录: /usr/local/nginx/

配置文件: /usr/local/nginx/conf/nginx.conf

服务器配置信息:/usr/local/nginx/conf/servers/servers_test.conf

nginx操作指令:

参考地址:

https://www.cnblogs.com/Anker/p/6056540.html

https://www.cnblogs.com/dormant/p/5218266.html

http://nginx.org

https://github.com/weibocom/nginx-upsync-module

https://www.aliyun.com/jiaocheng/145775.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Nginx动态负载均衡 的相关文章

随机推荐

  • 周鸿祎:什么是好的用户体验?

    说今天是一个体验为王的时代 一点也不过分 做大众消费品的人可能已经感觉到 今天消费者的话语权越来越强 如果你的产品做得好 不久就会口口相传 如果你的产品做得烂 不久就会骂声一片 所有这一切在过去是不可想象的 但今天 每个人都可以发布信息 每
  • hadoop任务执行时,报错

    2020 11 06 03 42 43 205 ERROR org apache hadoop yarn server resourcemanager scheduler SchedulerApplicationAttempt Error
  • Collectors.summing唯独没有BigDecimal的求和方法

    最近在做订单相关的模块 有个订单列表接口 需要对订单金额进行求和 每次都得遍历list 然后用BigDecimal add 方法取求和 感觉很麻烦 想到之前有用到java8的stream collect的Collectors summing
  • 一个例子搞懂 tabelu的上下文筛选器

    示例 1 将维度筛选器转换为上下文筛选器 本示例以及以下示例使用 Tableau Desktop 附带的 Sample Superstore 数据源 在此示例中 视图解决以下问题 按总销售额计 纽约市位居前 10 名的客户有哪些 视图包含两
  • Mono和MonoDevelop源码编译安装

    Mono和MonoDevelop源码编译安装 之所以用源码编译的方式安装mono和monodevelop 是因为通过yum安装的mono不是最新版本 而且monodevelop不能建 asp net MVC3的工程 而且通过源码安装 可以进
  • 世界坐标系、相机坐标系、图像坐标系、像素坐标系之间的转换及三维空间的刚体运动

    基本概念 https blog csdn net sunshine zoe article details 73457686 世界坐标系到相机坐标系下的变换 https www jianshu com p 64b4c887c439 通过两个
  • jquery中的伪数组和each和map静态方法区别,以及其他的一些静态方法

    伪数组 1 必须要有length属性 2 如果这个length的属性值是0 那么这个对象有没有元素无所谓 3 如果这个length的属性值不为0 那么这个对象一定头下标为 length 1 的属性值 列如 伪装组 var obj lengt
  • 公有链、联盟链、私有链区别

    1 公有链 公有链是世界上任何人都可以访问读取的 任何人都可以发送交易并且如果交易有效的话可以将之包括到区块中的 以及任何人都能够参与与其共识过程的区块 优点 所有交易数据公开 透明 无法篡改 缺点 低吞吐量 TPS 交易速度慢 2 联盟链
  • java橙色风格小说精品屋小说网站源码

    没有搭建教程 懂的自行下载研究 文件 590m com f 25127180 486121419 fbf84f 访问密码 551685 安装环境 宝塔面板 tomcat8 nginx1 17 mysql5 6 不知道最高支持到多少 打开服务
  • linux netLink检测usb插拔事件

    include
  • 从一到无穷大

    这本书一开始讲数学 后来讲到4围空间 空间压缩 相对论就头疼了 本不想读下去了 后来硬着头皮往下读 发现后面章节讲解生物 化学 天文等等 哈哈 原来是本科普的读物 大概都知道一些 不错 推荐给初高中生来看一看 2013 12 07
  • Linux内核之softirq机制

    中断的上半部执行紧要的任务 下半部则可以处理数据等次要的任务 tasklet也是基于softirq实现的 不同于tasklet的是 softirq是kernel编译时静态分配的 所以如果动态创建softirq或者kill softirqs
  • java 加密知识整理(未完)

    以下为网络转载 如有侵权请告知以便改正 JKS JCEKS PKCS12 BKS UBER 转一篇文章 http hi baidu com beyond456 blog item a3a009d7479b44d9a044df32 html
  • 计算机网络自顶向下WireShark实验:IP

    前言 计算机网络自顶向下WireShark实验记录 可供参考 题目 1 Select the first ICMP Echo Request message sent by your computer and expandthe Inter
  • 第二十六篇 DenseNet实战

    文章目录 摘要 1 项目结构 2 划分训练集和测试集 3 计算mean和Standard 3 1 标准化的作用 3 2 归一化的作用 4 Mixup CutMix CutOut数据集增强 5 训练
  • Windows应用-C#使用命令行执行PowerShell脚本

    前言 类似于bat脚本 能够自动执行一些任务 但是对bat不熟悉 因此选择使用C 来实现 具体是能够通过执行特定的语句实现对文件的读写与执行 代码 using System using System Collections Generic
  • 排序算法-堆排序

    思路 堆排序 Heapsort 是指利用堆积树 堆 这种数据结构所设计的一种排序算法 它是选择排序的一种 它是通过堆 来进行选择数据 需要注意的是排升序要建大堆 排降序建小堆 我们先将要排序的数据建成堆 然后通过下图所示的步骤进行排序 特性
  • 调试经验 - Fat文件打开失败的问题

    问题描述 在M7上调试打开SD卡中的某个文件 并读取内容的过程中 发现在SD卡中存在的一个文件firmware bin 通过open接口访问时 出现访问失败的问题 排查过程 发现f open 中传入的open flag为FA READ FA
  • 算法之拓扑关系

    目录 前言 算法解析 Kahn算法 DFS算法 总结 参考资料 前言 如何确定代码源文件的编译依赖关系 我们知道 一个完整的项目往往会包含很多代码源文件 编译器在编译整个项目的时候 需要按照依赖关系 依次编译每个源文件 比如 A cpp 依
  • Nginx动态负载均衡

    Nginx一般作为反向代理服务器来实现反向代理来转发处理请求 同时也可以作为静态资源服务器来加快静态资源的获取和处理 1 正向代理与反向代理 正向代理 正向代理 是一个位于客户端和原始服务器之间的服务器 为了从原始服务器取得内容 客户端向代