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 #查看当前容器环境参数
在这里插入图片描述
#根据上图环境变量重新编译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

在这里插入图片描述

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
在这里插入图片描述
自动写入成功

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值