docker下搭建Nginx+Consul+UpSync动态负载均衡

nginx容器

docker run -p 80:80 -d --name nginx -v /docker/nginx/default.conf:/etc/nginx/conf.d/default.conf -v /docker/nginx/upsync.conf:/etc/nginx/conf.d/upsync.conf -v /docker/www:/docker/www --privileged=true nginx

为nginx容器安装依赖环境
apt update
apt -y install wget zip unzip
apt -y install gcc autoconf automake make libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev
下载UpSync包以及nginx包用于重新编译
cd /home
wget https://github.com/weibocom/nginx-upsync-module/archive/refs/tags/v2.1.1.zip
unzip v2.1.1.zip
wget https://github.com/nginx/nginx/archive/refs/tags/release-1.21.1.zip
unzip release-1.21.1.zip
cd nginx-release-1.21.1

nginx -V #查看当前容器环境参数
docker下搭建Nginx+Consul+UpSync动态负载均衡_第1张图片
#根据上图环境变量重新编译nginx
在with部分加入–with-pcre --add-module=/home/nginx-upsync-module-2.1.1
执行下面命令重新编译:

./auto/configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-pcre --add-module=/home/nginx-upsync-module-2.1.1 --with-cc-opt=’-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.21.1/debian/debuild-base/nginx-1.21.1=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC’ --with-ld-opt=’-Wl,-z,relro -Wl,-z,now -Wl,–as-needed -pie’

make && make install

docker下搭建Nginx+Consul+UpSync动态负载均衡_第2张图片

touch /etc/nginx/conf.d/upsync.conf
cat /etc/nginx/conf.d/upsync.conf #这里文件为空,得带consul写入

consul容器

docker pull consul
docker run -p 8500:8500 -d --name consul -v /docker/consul/data:/consul/data --privileged=true -e CONSUL_BIND_INTERFACE=‘eth0’ consul agent -server -bootstrap-expect 1 -data-dir /consul/data -node=ali -ui -client=‘0.0.0.0’
#为consul添加动态规则
curl -X PUT -d ‘{“weight”:1,“max_fails”:2,“fail_timeout”:10}’ http://192.168.31.198:8500/v1/kv/upstreams/upsync/192.168.31.198:8080

修改nginx配置

#/docker/nginx/default.conf
upstream conusl {
   server 92.168.31.198:80;
   upsync 172.17.0.3:8500/v1/kv/upstreams/upsync upsync_timeout=6m upsync_interval=500m upsync_type=consul strong_dependency=off;
   upsync_dump_path /etc/nginx/conf.d/upsync.conf;
   include /etc/nginx/conf.d/upsync.conf;
}

server {
    listen       80;
    listen  [::]:80;
    server_name localhost;
    root /docker/www/consul;
    
    location /{
        proxy_pass http://conusl;
        index index.php index.html;
    }
}

docker restart nginx
docker exec -it nginx bash
cat /etc/nginx/conf.d/upsync.conf
在这里插入图片描述
自动写入成功

你可能感兴趣的:(docker,nginx,docker,consul)