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
    评论
Docker+Consul+Nginx+keepalived是一种常用的云原生架构方案,它结合了容器化、服务发现、负载均衡和高可用等多种技术手段,可用于构建高可用、弹性、可扩展的应用系统。 该方案的主要组件包括: 1. Docker:用于容器化应用程序和服务,提供了高效、轻量、可移植的应用打包和部署方式。 2. Consul:用于服务发现和配置管理,支持多数据中心、跨平台、高度可扩展的分布式系统。 3. Nginx:用于负载均衡和反向代理,支持高并发、高可用的流量分发。 4. keepalived:用于实现高可用的服务和节点,提供了基于 VRRP 协议的故障转移和自动切换功能。 在该方案中,Docker 容器作为应用程序和服务的运行环境,使用 Consul 进行服务注册和发现,并通过 Nginx 进行流量分发和负载均衡。同时,使用 keepalived 实现高可用的服务和节点,确保系统的稳定性和可用性。 项目描述可以按照以下步骤进行撰写: 1. 项目背景和目的:简要介绍本项目的背景和目的,说明为什么选择 Docker+Consul+Nginx+keepalived 方案。 2. 技术架构:详细介绍该方案的技术架构和组件,包括 DockerConsulNginx 和 keepalived 的作用和使用方式。 3. 系统功能:描述系统的主要功能和特点,包括服务发现、负载均衡、高可用等方面。 4. 实现方式:介绍系统的具体实现方式和实现步骤,包括 Docker 镜像的构建、应用程序的容器化、Consul 的配置和使用、Nginx 的配置和使用、keepalived 的配置和使用等。 5. 测试和验证:对系统进行测试和验证,验证系统的功能和性能是否符合预期,是否满足高可用和弹性的要求。 6. 总结和展望:对本项目进行总结和展望,分析该方案的优缺点和适用范围,展望未来的发展方向和趋势。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值