负载均衡和动态负载均衡

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配置文件:

 
  1. upstream test {

  2. server 127.0.0.1:11111;

  3. upsync 127.0.0.1:8500/v1/kv/upstreams/test/ upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;

  4. upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;

  5. include /usr/local/nginx/conf/servers/servers_test.conf;

  6. }

  7.  
  8. server {

  9. listen 80;

  10. server_name localhost;

  11.  
  12. #charset koi8-r;

  13.  
  14. #access_log logs/host.access.log main;

  15. location = / {

  16. proxy_pass http://test;

  17. }

  18.  
  19. error_page 500 502 503 504 /50x.html;

  20. location = /50x.html {

  21. root html;

  22. }

  23. }

启动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;
 
  1. 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

  2. or

  3. 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

调整服务器的权重:

 
  1. 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

  2. or

  3. 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

标记服务器停止服务:

 
  1. 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

  2. or

  3. 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

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值