nginx动态负载均衡

栏目: 服务器 · Nginx · 发布时间: 5年前

内容简介:传统的负载均衡,如果Upstream参数发生变化,每次都需要重新加载nginx.conf文件,因此扩展性不是很高,所以我们可以采用动态负载均衡,实现Upstream可配置化、动态化,无需人工重新加载nginx.conf,类似分布式的配置中心5.启动consul./consul agent -dev -ui -node=consul-dev -client=192.168.102.129

方案

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

  1. Consul+Consul-template 每次发现配置更改需要raload nginx,重启Nginx。
  2. Consul+OpenResty 实现无需raload动态负载均衡
  3. 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
[root@localhost local]# ./consul
usage: consul [--version] [--help] <command> [<args>]

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:

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

配置Nginx

  1. tar -zxvf nginx-1.9.9.tar.gz
  2. groupadd nginx
  3. useradd -g nginx -s /sbin/nologin nginx
  4. mkdir -p /var/tmp/nginx/client/
  5. mkdir -p /usr/local/nginx
  6. cd nginx-1.9.9/
  7. ./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
  8. make && make install

动态负载均衡

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

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

  1. 添加nginx Upstream服务

负载均衡信息参数

{"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服务器出问题了,本地还有一个备份。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

这还是马云

这还是马云

陈伟 / 浙江人民出版社 / 2013-5 / 39.80元

“幽默马云”、“开心马云”、“顽皮马云”、“狂妄马云”……《这还是马云(全新升级版)》由陈伟所著,《这还是马云(全新升级版)》从各个角度揭开了实际生活中“千面马云”的真面目,告诉你一个与想象中大不一样的马云。这不只是一本书,更像一部喜剧电影,让你通过声音、色彩、表情等诸多要素走近马云,感受阿里巴巴。没有冗长的说教,只有让人忍俊不禁的细节;没有高深的理论,只有通俗、诚恳的陈述。作者借幽默平常的琐事,......一起来看看 《这还是马云》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具