使用Docker-Compose搭建consul集群环境!!!
Posted 龙少。
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Docker-Compose搭建consul集群环境!!!相关的知识,希望对你有一定的参考价值。
使用Docker-Compose搭建consul集群环境
- 一.Compose consul的概述
- 二.Consul的概述
- 三.Compose的部署
- 四.Consul的部署
一.Compose consul的概述
1.什么是Compose
Docker Compose的前身是fig,他是一个定义及运行多个Docker容器的工具,使用Docker Compose不需要使用shell脚本来启动容器;Docker Compose非常适合组合多个容器进行开发的场景
2.Docker Compose容器编排
YAML是一种标记语言很直观的数据序列化格式
文件格式及编写注意事项:
不支持制表符tab键缩进,需要使用空格缩进
通常开头缩进2个空格
字符后缩进1个空格,如冒号、逗号、横杆
用#号注释
如果包含特殊字符用单引号弓|起来
布尔值必须用引号括起来
3.Docker Compose配置常用字段字段 描述
4.Docker Compose常用命令
二.Consul的概述
1.什么是consul
Consul是HashiCorp公司推出的开源工具, 用于实现分布式系统的服务发现与配置
2.Consul的特性
支持健康检查,允许存储键值对
基于Golong语言,可移植性强
支持ACL访问控制
同时可与Docker等轻量级容器可无缝配合
3.Consul容器服务更新与发现
consul服务端:
(1)consul template:通过变量定义模板;定义的内容就是upstream地址池,通过变量的形式,并在接收到指令的时候动态更新web_01中ng的配置文件
(2)consul server:管理consul template
(3)consul agent:获取服务发现的机制;UI界面也可以看到新更新的服务
compose:
consul registrator:监听;部署在server端上
过程:
设置registrator监控,监听服务端口,有新的端口监控会注册到agent,交给server端,把后端真实的容器和ip写入template模板内,模板更新到nginx.conf内,再重载配置文件,从而对外识别新加的服务后端的节点:docker服务器
三.Compose的部署
1.服务器准备
192.168.206.177
安装服务docker concul agent/server/template nginx手工编译安装
192.168.206.188
安装服务docker nginx httpd
2.两台服务器安装docker,并设置镜像加速
3.上传Compose
[root@docker1 ~]# ls
1.sh anaconda-ks.cfg initial-setup-ks.cfg test.sh 视频 下载
2.sh docker-compose mm.sh 公共 图片 音乐
3.sh first.sh qiuhe.sh 模板 文档 桌面
[root@docker1 ~]# cp docker-compose /usr/local/bin
[root@docker1 ~]# chmod +x /usr/local/bin/docker-compose
4.创建目录,编写nginx的dockerfile
[root@docker1 ~]# mkdir /root/compose_nginx
[root@docker1 ~]# cd compose_nginx/
[root@docker1 compose_nginx]# mkdir nginx wwwroot
[root@docker1 compose_nginx]# cd nginx/
[root@docker1 nginx]# touch Dockerfile
[root@docker1 nginx]# ls #上传软件包
Dockerfile nginx-1.12.2.tar.gz
[root@docker1 nginx]# vim Dockerfile #编写dockerfile
FROM centos:7
ADD nginx-1.12.2.tar.gz /mnt
WORKDIR /mnt/nginx-1.12.2
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && \\
yum clean all && \\
sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && \\
./configure --prefix=/usr/local/nginx &> /dev/null && \\
make &> /dev/null && make install &> /dev/null &&\\
rm -rf /mnt/nginx-1.12.2
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
5.编写站点网页
[root@docker1 nginx]# cd ..
[root@docker1 compose_nginx]# ls
nginx wwwroot
[root@docker1 compose_nginx]# cd wwwroot/
[root@docker1 wwwroot]# vim index.html
this is NG web!!!
6.编写docker-compose.yml文件
[root@docker1 wwwroot]# vim /root/compose_nginx/docker-compose.yml
version: '3'
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 1216:80
- 1217:443
networks:
- cluster
volumes:
- ./wwwroot:/usr/local/nginx/html
networks:
cluster:
[root@docker1 wwwroot]# cd ..
[root@docker1 compose_nginx]# docker-compose -f docker-compose.yml up -d #基于yml文件运行启动
[root@docker1 compose_nginx]# docker-compose ps #使用Compose容器编排
Name Command State Ports
-----------------------------------------------------------------------------------------
compose_nginx_nginx_1 /usr/local/nginx/sbin/ngin Up 0.0.0.0:1217->443/tcp,:::12
... 17->443/tcp, 0.0.0.0:1216->
80/tcp,:::1216->80/tcp
[root@docker1 compose_nginx]#
7.查看目录结构
[root@docker1 compose_nginx]# yum install -y tree
[root@docker1 compose_nginx]# tree .
.
├── docker-compose.yml
├── nginx
│ ├── Dockerfile
│ └── nginx-1.12.2.tar.gz
└── wwwroot
└── index.html
2 directories, 4 files
[root@docker1 compose_nginx]#
8.查看网页
四.Consul的部署
1.服务器准备
192.168.206.177
Docker-ce、 Compose 3、Consul、 Consul-template
192.168.206.188
Docker-ce、registrator
template 模更新
registrator 自动发现
registrator 控制consul 完成更新操作
consul template模板 进行热更新
consul 自动发现、自动更新,为容器提供服务(添加、删除、生命周期)
2.consul服务器部署
[root@docker1 ~]# mkdir consul
[root@docker1 ~]# cd consul
[root@docker1 consul]# ls #上传软件包
consul_0.9.2_linux_amd64.zip
[root@docker1 consul]# unzip consul_0.9.2_linux_amd64.zip #解压
Archive: consul_0.9.2_linux_amd64.zip
inflating: consul
[root@docker1 consul]# ls
consul consul_0.9.2_linux_amd64.zip
[root@docker1 consul]# consul agent \\
> -server \\
> -bootstrap \\
> -ui \\
> -data-dir=/var/lib/consul-data \\
> -bind=192.168.206.177 \\
> -client=0.0.0.0 \\
> -node=consul-server01 &> /var/log/consul.log &
[1] 68301
[root@docker1 consul]# jobs
[1]+ 运行中 consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=192.168.206.177 -client=0.0.0.0 -node=consul-server01 &>/var/log/consul.log &
[root@docker1 consul]# consul members #查看集群信息
Node Address Status Type Build Protocol DC
consul-server01 192.168.206.177:8301 alive server 0.9.2 2 dc1
[root@docker1 consul]# consul info | grep leader
leader = true
leader_addr = 192.168.206.177:8300
[root@docker1 consul]#
3.通过httpd api 获取集群信息
curl 127.0.0.1:8500/v1/status/peers //查看集群server成员
curl 127.0.0.1:8500/v1/status/leader //集群 Raf leader
curl 127.0.0.1:8500/v1/catalog/services //注册的所有服务
curl 127.0.0.1:8500/v1/catalog/nginx //查看 nginx 服务信息
[root@consul consul]# curl 127.0.0.1:8500/v1/catalog/nodes //集群节点详细信息
["ID":"ac1937e8-8f21-bbc0-61d6-0a24ea946cee","Node":"consul-server01","Address":"192.168.226.130","Datacenter":"dc1","TaggedAddresses":"lan":"192.168.226.129","wan":"192.168.226.129","Meta":,"CreateIndex":5,"ModifyIndex":6]
4.容器服务自动加入consul集群
(1)安装 Gliderlabs/Registrator
可检查容器运行状态自动注册,还可注销 docker 容器的服务到服务配置中心。
目前支持 Consul、Etcd 和 SkyDNS2。
在 192.168.206.188节点,执行以下操作:
[root@docker2 ~]# docker run -d \\
> --name=registrator \\
> --net=host \\
> -v /var/run/docker.sock:/tmp/docker.sock \\
> --restart=always \\
> gliderlabs/registrator:latest \\
> -ip=192.168.206.188 \\
> consul://192.168.206.177:8500
Unable to find image 'gliderlabs/registrator:latest' locally
latest: Pulling from gliderlabs/registrator
Image docker.io/gliderlabs/registrator:latest uses outdated schema1 manifest format. Please upgrade to a schema2 image for better future compatibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/
c87f684ee1c2: Pull complete
a0559c0b3676: Pull complete
a28552c49839: Pull complete
Digest: sha256:6e708681dd52e28f4f39d048ac75376c9a762c44b3d75b2824173f8364e52c10
Status: Downloaded newer image for gliderlabs/registrator:latest
5b0d113a2507502f45cd662b004cd2e3079c62cc9c0775e3c049a2b07177c0f3
您在 /var/spool/mail/root 中有邮件
[root@docker2 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5b0d113a2507 gliderlabs/registrator:latest "/bin/registrator -i…" 36 seconds ago Up 35 seconds registrator
[root@docker2 ~]#
(2) 测试服务发现功能是否正常
[root@docker2 ~]# docker run -itd -p:83:80 --name test-01 -h test01 nginx
54efb755d03fcd890691368bc951cbd31b2194ddbfa05d95f6a921778070bf8a
您在 /var/spool/mail/root 中有邮件
[root@docker2 ~]# docker run -itd -p:84:80 --name test-02 -h test02 nginx
07ddc115ddc845a19c5c086c5feabf437ced6228f14fe148c655a02d5c397c26
[root@docker2 ~]# docker run -itd -p:88:80 --name test-03 -h test03 httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
a330b6cecb98: Already exists
14e3dd65f04d: Pull complete
fe59ad2e7efe: Pull complete
2cb26220caa8: Pull complete
3138742bd847: Pull complete
Digest: sha256:af1199cd77b018781e2610923f15e8a58ce22941b42ce63a6ae8b6e282af79f5
Status: Downloaded newer image for httpd:latest
d23bea581643f82438e9ac6183734ffad3a95be6a43be57cdfd10e8f5bc3817b
[root@docker2 ~]# docker run -itd -p:89:80 --name test-04 -h test04 httpd
505a91e3609a2de9e1768dd0e2fdae440f3b6b10a178c4a3bab0c242e1fda201
[root@docker2 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
505a91e3609a httpd "httpd-foreground" 22 seconds ago Up 21 seconds 0.0.0.0:89->80/tcp, :::89->80/tcp test-04
d23bea581643 httpd "httpd-foreground" 35 seconds ago Up 33 seconds 0.0.0.0:88->80/tcp, :::88->80/tcp test-03
07ddc115ddc8 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:84->80/tcp, :::84->80/tcp test-02
54efb755d03f nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:83->80/tcp, :::83->80/tcp test-01
5b0d113a2507 gliderlabs/registrator:latest "/bin/registrator -i…" 4 minutes ago Up 4 minutes registrator
(3)验证 http 和 nginx 服务是否注册到 consul
浏览器输入 http://192.168.206.177:8500,“单击 NODES”,然后单击 “consurl-server01”,会出现 5 个服务
(4)在consul服务器上查看
[root@docker1 ~]# curl 127.0.0.1:8500/v1/catalog/services
"consul":[],"httpd":[],"nginx":[][root@docker1 ~]#
5.安装 consul-template
Consul-Template 是一个守护进程,用于实时查询 Consul 集群信息,并更新文件系统 上任意数量的指定模板,生成配置文件。更新完成以后,可以选择运行 shell 命令执行更新 操作,重新加载 Nginx。Consul-Template 可以查询 Consul 中的服务目录、Key、Key-values 等。
这种强大的抽象功能和查询语言模板可以使 Consul-Template 特别适合动态的创建配置文件。
例如:创建 Apache/Nginx Proxy Balancers、Haproxy Backends
(1)准备 template nginx 模板文件
在consul上操作
[root@docker1 ~]# vim /root/consul/nginx.ctmpl
upstream http_backend
range service "nginx"
server .Address:.Port; #此处引用的变量会指向后端的地址和端口(动态变化)
end
server
listen 83; #ngixn的监听端口
server_name localhost 192.168.35.40; #反向代理的IP地址(前端展示的NG服务的IP)
access_log /var/log/nginx/klj.cn-access.log;
index index.html index.php;
location /
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr; #后端真实IP
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #转发地址
proxy_pass http://http_backend;
(2)编译安装nginx
[root@docker1 ~]# yum install gcc pcre-devel zlib-devel -y
[root@docker1 ~]# cd /opt
[root@docker1 opt]# ls
containerd nginx-1.12.2.tar.gz rh
[root@docker1 ~]# tar zxvf nginx-1.12.2.tar.gz -C /opt
[root@docker1 ~]# cd nginx-1.12.2
[root@docker1 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx
[root@docker1 nginx-1.12.2]# make && make install
(3)配置ngiinx
[root@docker1 nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
http
include mime.types; #默认存在的
include vhost/*.conf; #添加虚拟主机目录为了让那个识别vhost/*.conf的文件(consul动态生成的配置文件就会放在这里)
default_type application/octet-stream;
[root@docker1 nginx-1.12.2]# mkdir /usr/local/nginx/conf/vhost
[root@docker1 nginx-1.12.2]# mkdir /var/log/nginx
[root@docker1 nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin
[root@docker1 nginx-1.12.2]# nginx #启动nginx
[root@docker1 nginx-1.12.2]# netstat -antp |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 75178/nginx: master
6.配置并启动 template
(1)上传压缩包解压
[root@docker1 ~]# ls
1.sh consul mm.sh 视频 桌面
2.sh consul-template_0.19.3_linux_amd64.zip qiuhe.sh 图片
3.sh docker-compose test.sh 文档
anaconda-ks.cfg first.sh 公共 下载
compose_nginx initial-setup-ks.cfg 模板 音乐
[root@docker1 ~]# unzip consul-template_0.19.3_linux_amd64.zip
Archive: consul-template_0.19.3_linux_amd64.zip
inflating: consul-template
[root@docker1 ~]# mv consul-template /usr/bin/
(2)关联nginx 虚拟目录中的子配置文件操作
[root@docker1 ~]# consul-template -consul-addr 192.168.206.177:8500 \\
> -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/klj.conf:/usr/local/nginx/sbin/nginx -s reload" \\
> --log-level=info
2021/09/14 19:44:29.448911 [INFO] consul-template v0.19.3 (ebf2d3d)
2021/09/14 19:44:29.448941 [INFO] (runner) creating new runner (dry: false, once: false)
2021/09/14 19:44:29.449719 [INFO] (runner) creating watcher
2021/09/14 19:44:29.450803 [INFO] (runner) starting
2021/09/14 19:44:29.450826 [INFO] (runner) initiating run
2021/09/14 19:44:29.455808 [INFO] (runner) initiating run
2021/09/14 19:44:29.464021 [INFO] (runner以上是关于使用Docker-Compose搭建consul集群环境!!!的主要内容,如果未能解决你的问题,请参考以下文章