Consul+upsync+Nginx 动态负载均衡 方案

Consul服务器

下载consul_1.6.2_linux_amd64.zip,解压至/usr/bin,依赖unzip

mkdir -p /data/consul
mkdir /etc/consul.d
consul agent -server -bootstrap-expect=1 -data-dir=/data/consul -node=agent-one -bind=192.168.1.95 -enable-script-checks=true -config-dir=/etc/consul.d -client 0.0.0.0 -ui

nohup启动方式

mkdir /var/opt/consul
nohup consul agent -server -bootstrap-expect=1 -data-dir=/data/consul -node=agent-one -bind=192.168.1.95 -enable-script-checks=true -config-dir=/etc/consul.d -client 0.0.0.0 -ui >> /var/opt/consul/consul.log 2>&1 &

Nginx服务器

前置依赖

apt-get install -y unzip gcc make

nginx 配置用户和组

groupadd nginx
useradd -g nginx -s /sbin/nologin nginx
mkdir -p /var/tmp/nginx/client/
mkdir -p /usr/local/nginx

ubuntu编译安装Nginx

apt-get install -y openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev
./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=/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

mkdir /usr/local/nginx/conf/servers/

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

POSTMAN发包创建upstream

   upstream 98apache2{
        server 127.0.0.1:11111;
        upsync 192.168.1.95:8500/v1/kv/upstreams/98apache2 upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
        upsync_dump_path /usr/local/nginx/conf/servers/98apache2.conf;
    }

    server {
        listen       80;
        server_name  localhost;

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

kv中可配置权重

{"weight":2, "max_fails":2, "fail_timeout":10, "down":0}

发表回复

您的电子邮箱地址不会被公开。