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

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安装

  1. 拉取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模块

准备素材
nginx-upsync-module-master.zip
nginx-1.21.0.tar.gz(方便编译安装新模块,根据自己的nginx容器版本下载)
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

可能存在的问题

1.编译nginx是openssl依赖问题
openssl依赖找不到,需要进行安装,而使用nginx容器的apt-get进行安装不起作用那么可以自己去下载一个 openssl安装包,copy进入nginx的容器.

docker cp /home/openssl-1.1.1k.tar.gz nginx:/home/
#解压copy进来的安装包 
tar -zxvf openssl-1.1.1k.tar.gz
#完成后再./configure的编译中加入openssl安装包的路径
./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 --with-openssl=/home/openssl-1.1.1k

2.apt-get无法安装扩展
更换apt-get镜像

deb http://mirrors.163.com/debian/ buster main non-free contrib deb http://mirrors.163.com/debian/ buster-updates main non-free contrib deb http://mirrors.163.com/debian/ buster-backports main non-free contrib deb-src http://mirrors.163.com/debian/ buster main non-free contrib deb-src http://mirrors.163.com/debian/ buster-updates main non-free contrib deb-src http://mirrors.163.com/debian/ buster-backports main non-free contrib deb http://mirrors.163.com/debian-security/ buster/updates main non-free contrib deb-src http://mirrors.163.com/debian-security/ buster/updates main non-free contrib

或者使用其他docker源

"registry-mirrors": ["https://0wrdwnn6.mirror.aliyuncs.com"]

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服务加入到负载均衡中:


非docker环境consul实现动态负载均衡详见:https://www.jianshu.com/p/7e1f50da5d32

©著作权归作者所有,转载或内容合作请联系作者
禁止转载,如需转载请通过简信或评论联系作者。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,122评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,070评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,491评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,636评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,676评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,541评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,292评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,211评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,655评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,846评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,965评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,684评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,295评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,894评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,012评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,126评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,914评论 2 355

推荐阅读更多精彩内容