美文网首页
nginx结合consul实现动态负载均衡

nginx结合consul实现动态负载均衡

作者: dark68 | 来源:发表于2021-04-28 08:39 被阅读0次

    动态的负载均衡,动态更新上游的服务器不需要 reload nginx ,它的功能是拉取 consul 的后端 server 的列表,并更新 Nginx 的路由信息。此模块不依赖于任何第三方模块。 consul 作为 Nginx 的 db,利用 consul 的 KV 服务,每个 Nginx work 进程独立的去拉取各个 upstream 的配置,并更新各自的路由。

    1 什么是consul

    Consul 是一个支持多数据中心分布式高可用的服务发现和配置共享的服务软件。
    服务发现以及注册:
    当服务Producer 启动时,会将自己的Ip/host等信息通过发送请求告知 Consul,Consul 接收到 Producer 的注册信息后,每隔一段时间会向 Producer 发送一个健康检查的请求,检验Producer是否健康。
    服务调用:
    当 Consumer 请求Product时,会先从 Consul 中拿到存储Product服务的 IP 和 Port 的临时表(temp table),从temp table表中任选一个· Producer 的 IP 和 Port, 然后根据这个IP和Port,发送访问请求;temp table表只包含通过了健康检查的 Producer 信息,并且每隔一段时间更新。

    2 cousul的安装

    consul安装包下载地址:https://www.consul.io/downloads
    1.下载完成后,解压,得到一个可执行文件consul

    unzip consul_1.9.4_linux_amd64.zip
    

    2.将这个文件移动到全局变量环境中

    mv consul /usr/local/bin/
    

    3.验证安装

    consul version
    

    3 consul 的常用命令

    consul命令 描述
    agent 运行一个consul agent
    members 列出consul cluster集群中的members
    join 将agent加入到consul集群
    leave 将节点移除所在的集群

    4 启动consul服务

    启动consul服务端:

    consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -node=ali_1 -bind=192.168.35.130 -ui -client=0.0.0.0
    

    参数含义:

    agent
    -server表示启动的是一个服务
    -bootstrap-expect 1 表示等待多少个节点再启动,这里1个,就是自己一个就启动了
    -node=ali_1 就是给consul服务起个别名为ali
    -bind=192.168.35.130 绑定内网ip
    -data-dir /tmp/consul 数据存储目录为/tmp/consul
    -ui 启动默认ui界面
    -client consul绑定在哪个client地址上,这个地址提供HTTP、DNS、RPC等服务,默认是127.0.0.1,可指定允许客户端使用什么ip去访问
    -enable-script-checks=true:设置检查服务为可用

    启动后访问192.168.35.130:8500可以看到ui界面


    consul ui界面

    注:

    如果nginx重启出现unknow directive "upsync",需要先添加nginx-upsync-module模块
    下载并解压

    wget https://github.com/weibocom/nginx-upsync-module/archive/v2.1.0.tar.gz
    tar -zxvf v2.1.0.tar.gz
    

    查看nginx之前编译的模块,获取编译数据

    /usr/local/nginx/nginx -V
    ···
    configure arguments: --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_gzip_static_module --with-http_stub_status_module --with-file-aio --with-http_realip_module --with-http_ssl_module --with-pcre=/home/pcre-8.44 --with-zlib=/home/zlib-1.2.11 --with-openssl=/home/openssl-1.1.1g --add-module=/home/ngx_cache_purge-2.3
    
    

    对nginx进行新的编译安装,添加nginx-upsync-module-2.1.0

    ./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_gzip_static_module --with-http_stub_status_module --with-file-aio --with-http_realip_module --with-http_ssl_module --with-pcre=/home/pcre-8.44 --with-zlib=/home/zlib-1.2.11 --with-openssl=/home/openssl-1.1.1g --add-module=/home/ngx_cache_purge-2.3 --add-module=/home/nginx-upsync-module-2.1.0
    make -j2
    make install
    

    再次查看可以看到nginx-upsync-module-2.1.0添加成功

    /usr/local/nginx/nginx -V
    ···
    configure arguments: --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_gzip_static_module --with-http_stub_status_module --with-file-aio --with-http_realip_module --with-http_ssl_module --with-pcre=/home/pcre-8.44 --with-zlib=/home/zlib-1.2.11 --with-openssl=/home/openssl-1.1.1g --add-module=/home/nginx-upsync-module-2.1.0/
    

    5 配置nginx负载均衡

    upstream blog {
          server www.blog130-httpd.com:81;
          upsync 192.168.35.130:8500/v1/kv/upstreams/blog upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
          upsync_dump_path /vhost/server_test.conf;
          include /vhost/server_test.conf;
    }
    server
    {
        listen 80;
        server_name www.blog130.com;
        index  index.html index.php;
    
        location ~  / {
            proxy_pass http://blog;
        }
    }
    

    参数含义:

    upsync模块会去consul拉取最新的upstream信息并存到本地的文件中
    upsync_timeout 配置从consul拉取上游服务器的超时时间
    upsync_interval 配置从consul拉取上游服务器的间隔时间
    upsync_type 指定使用配置服务器的类型,当前是consul
    strong_dependency 启动时是否强制依赖配置服务器,如果配置为on,则拉取失败,nginx同样会启用失败
    upsync_dump_path 指定从consul拉取的上游服务器后持久化到的位置,这样即使 Consul服务器出问题了,本地同样会有备份

    6 通过curl动态添加服务器

    curl -X PUT -d '{"weight":1,"max_fails":2,"fail_timeout":10}' 192.168.35.130:8500/v1/kv/upstreams/blog/192.168.35.131:81
    #添加成功
    true
    #在添加一台
    curl -X PUT -d '{"weight":1,"max_fails":2,"fail_timeout":10}' 192.168.35.130:8500/v1/kv/upstreams/blog/192.168.35.130:81
    

    查看配置文件server_test.conf,可以看到相应的服务器


    server_test.conf

    在ui界面可以看到相应的key/value


    key/value
    也可使用curl查看key/value
    curl http://127.0.0.1:8500/v1/kv/?recurse
    

    删除服务

    curl -X DELETE -d '{"weight":1,"max_fails":2,"fail_timeout":10}' 192.168.35.130:8500/v1/kv/upstreams/blog/192.168.35.130:81
    

    访问www.blog130.com可以看到相应的负载均衡

    相关文章

      网友评论

          本文标题:nginx结合consul实现动态负载均衡

          本文链接:https://www.haomeiwen.com/subject/zguurltx.html