阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

nginx+consul做动态负载均衡(docker)

34次阅读
没有评论

共计 3534 个字符,预计需要花费 9 分钟才能阅读完成。

导读 负载均衡建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。
1 consul
1.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 信息,并且每隔一段时间更新。

1.2 consul 安装

拉取 consul 镜像

docker pull consul
2. 构建 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'

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

2 nginx 安装 nginx-upsync-module 模块

1. 将准备的素材 copy 到 nginx 容器

docker cp /home/nginx-upsync-module-master.zip nginx:/home/ 
docker cp /home/nginx-1.21.0.tar.gz nginx:/home/

2. 进入容器解压安装包

unzip nginx-upsync-module-master.zip 
tar -zxvf nginx-1.21.0.tar.gz

3. 安装相关扩展

apt-get install -y gcc autoconf automake make libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev

4. 进入解压的 nginx 安装包,进行编译安装

cd /home/nginx-1.21.0


#通过命令查看 nginx 版本与编译信息
nginx -V

#复制相关编译信息,添加 nginx- upsync-module-master 模块
./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-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.21.0/debian/debuild-base/nginx-1.21.0=. -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' --add-module=/home/nginx-upsync-module-master

5. 编译安装

make && make install
3 nginx 动态负载均衡配置
upstream edu{
    server 192.168.35.138;
    upsync 172.17.0.3:8500/v1/kv/upstreams/nginx_test upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
    upsync_dump_path /etc/nginx/conf.d/server_test.conf;
    include /etc/nginx/conf.d/server_test.conf;
}
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    root /docker/www/;
    index index.php index.html;

    location / {proxy_pass http://edu;}
}

往 consul 加入 nginx 服务

curl -X PUT -d '{"weight":1,"max_fails":2,"fail_timeout":10}' http://192.168.35.139:8500/v1/kv/upstreams/nginx_test/192.168.35.131:81

curl -X PUT -d '{"weight":1,"max_fails":2,"fail_timeout":10}' http://192.168.35.139:8500/v1/kv/upstreams/nginx_test/192.168.35.131:80

接着查看 server_test.conf 文件, 看看 consul 是否有将我们新增的 nginx 服务加入到负载均衡中。

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-07-25发表,共计3534字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中