资讯专栏INFORMATION COLUMN

nginx动态负载均衡

izhuhaodev / 790人阅读

摘要:实现无需动态负载均衡实现无需动态负载均衡环境搭建出现下面提示说明成功启动是新浪微博开源的基于实现动态配置的三方模块。配置动态负载均衡创建指定从拉取的上游服务器后持久化到的位置,这样即使服务器出问题了,本地还有一个备份。

方案

传统的负载均衡,如果Upstream参数发生变化,每次都需要重新加载nginx.conf文件,因此扩展性不是很高,所以我们可以采用动态负载均衡,实现Upstream可配置化、动态化,无需人工重新加载nginx.conf,类似分布式的配置中心

Consul+Consul-template 每次发现配置更改需要raload nginx,重启Nginx。

Consul+OpenResty 实现无需raload动态负载均衡

Consul+upsync+Nginx 实现无需raload动态负载均衡

Consul+upsync+Nginx Consul环境搭建

https://releases.hashicorp.com/consul/0.7.5/consul_0.7.5_linux_amd64.zip

yum -y install unzip

unzip consul_0.7.5_linux_amd64.zip

./consul 出现下面提示说明成功

[root@localhost local]# ./consul
usage: consul [--version] [--help]  []

Available commands are:
    agent          Runs a Consul agent
    configtest     Validate config file
    event          Fire a new event
    exec           Executes a command on Consul nodes
    force-leave    Forces a member of the cluster to enter the "left" state
    info           Provides debugging information for operators
    join           Tell Consul agent to join cluster
    keygen         Generates a new encryption key
    keyring        Manages gossip layer encryption keys
    kv             Interact with the key-value store
    leave          Gracefully leaves the Consul cluster and shuts down
    lock           Execute a command holding a lock
    maint          Controls node or service maintenance mode
    members        Lists the members of a Consul cluster
    monitor        Stream logs from a Consul agent
    operator       Provides cluster-level tools for Consul operators
    reload         Triggers the agent to reload configuration files
    rtt            Estimates network round trip time between nodes
    snapshot       Saves, restores and inspects snapshots of Consul server state
    version        Prints the Consul version
    watch          Watch for changes in Consul

5.启动consul
./consul agent -dev -ui -node=consul-dev -client=192.168.102.129

nginx-upsync-module

Upsync是新浪微博开源的基于Nginx实现动态配置的三方模块。Nginx-Upsync-Module的功能是拉取Consul的后端server的列表,并动态更新Nginx的路由信息。此模块不依赖于任何第三方模块。Consul作为Nginx的DB,利用Consul的KV服务,每个Nginx Work进程独立的去拉取各个upstream的配置,并更新各自的路由。
nginx-upsync-module:

cd /usr/local

wget https://github.com/weibocom/nginx-upsync-module/archive/master.zip

unzip master.zip

unzip consul_0.7.1_linux_amd64.zip

配置Nginx

tar -zxvf nginx-1.9.9.tar.gz

groupadd nginx

useradd -g nginx -s /sbin/nologin nginx

mkdir -p /var/tmp/nginx/client/

mkdir -p /usr/local/nginx

cd nginx-1.9.9/

./configure --prefix=/usr/local/nginx --user=nginx --group=root --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=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre --add-module=../nginx-upsync-module-master

make && make install

动态负载均衡

创建upsync_dump_pathmkdir /usr/local/nginx/conf/servers/

upsync_dump_path指定从consul拉取的上游服务器后持久化到的位置,这样即使consul服务器出问题了,本地还有一个备份。

添加nginx Upstream服务

使用linux命令方式发送put请求
curl -X PUT http://192.168.102.129:8500/v1/kv/upstreams/tmp/192.168.102.128:8081
curl -X PUT http://192.168.102.129:8500/v1/kv/upstreams/tmp/192.168.102.128:8081

使用postmen 发送put请求
http://192.168.102.129:8500/v1/kv/upstreams/tmp/192.168.102.128:8081
http://192.168.102.129:8500/v1/kv/upstreams/tmp/192.168.102.128:8081

负载均衡信息参数
{"weight":1, "max_fails":2, "fail_timeout":10, "down":0}

Upstream 动态配置
##动态去consul 获取注册的真实反向代理地址
upstream tmpup{

    server 127.0.0.1:11111;
    upsync 192.168.212.134:8500/v1/kv/upstreams/tmp upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
    upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;
}

server {
    listen       80;
    server_name  localhost;

    location / {
        proxy_pass http://tmpup;
        index  index.html index.htm;
    }
}

upsync指令指定从consul哪个路径拉取上游服务器配置;upsync_timeout配置从consul拉取上游服务器配置的超时时间;upsync_interval配置从consul拉取上游服务器配置的间隔时间;upsync_type指定使用consul配置服务器;strong_dependency配置nginx在启动时是否强制依赖配置服务器,如果配置为on,则拉取配置失败时nginx启动同样失败。upsync_dump_path指定从consul拉取的上游服务器后持久化到的位置,这样即使consul服务器出问题了,本地还有一个备份。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/40482.html

相关文章

  • 微服务的接入层设计与动静资源隔离

    摘要:接入层作用一的聚合。接入层作用二服务发现与动态负载均衡既然统一的入口变为了接入层,则接入层就有责任自动的发现后端拆分,聚合,扩容,缩容的服务集群,当后端服务有所变化的时候,能够实现健康检查和动态的负载均衡。 此文已由作者刘超授权网易云社区发布。 欢迎访问网易云社区,了解更多网易技术产品运营经验。 这个系列是微服务高并发设计,所以我们先从最外层的接入层入手,看都有什么样的策略保证高并发。...

    jindong 评论0 收藏0
  • Nginx 极简入门教程!

    上篇文章和大家聊了 Spring Session 实现 Session 共享的问题,有的小伙伴看了后表示对 Nginx 还是很懵,因此有了这篇文章,算是一个 Nginx 扫盲入门吧! 基本介绍 Nginx 是一个高性能的 HTTP 和反向代理 web 服务器,同时也提供了 IMAP/POP3/SMTP 服务。 Nginx 是由伊戈尔·赛索耶夫为俄罗斯访问量第二的 Rambler.ru 站点开发...

    _DangJin 评论0 收藏0
  • nginx反向代理、动静分离、负载均衡

    摘要:反向代理要说反向代理,我们就先要理解正向代理下面我们就谈谈正向代理和反向代理吧。客户端才能使用正向代理。反向代理总结就一句话代理端代理的是服务端。因此,动态资源转发到服务器我们就使用到了前面讲到的反向代理了。 反向代理 要说反向代理,我们就先要理解正向代理 ,下面我们就谈谈正向代理和反向代理吧。 正向代理 一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始...

    DTeam 评论0 收藏0
  • nginx反向代理、动静分离、负载均衡

    摘要:反向代理要说反向代理,我们就先要理解正向代理下面我们就谈谈正向代理和反向代理吧。客户端才能使用正向代理。反向代理总结就一句话代理端代理的是服务端。因此,动态资源转发到服务器我们就使用到了前面讲到的反向代理了。 反向代理 要说反向代理,我们就先要理解正向代理 ,下面我们就谈谈正向代理和反向代理吧。 正向代理 一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始...

    eechen 评论0 收藏0
  • Rancher通过Aliyun-slb服务对接阿里云SLB教程

    摘要:本文将详尽演示如何通过服务对接阿里云。概要阿里云负载均衡是将访问流量根据转发策略分发到后端多台云服务器,简称的流量分发控制服务。 阿里云负载均衡(Server Load Balancer)是将访问流量根据转发策略分发到后端多台云服务器(ECS)的流量分发控制服务。 本文将详尽演示Rancher如何通过Aliyun-slb服务对接阿里云SLB。 概要 阿里云负载均衡(Server Loa...

    baihe 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<