docker-consul容器服务与发现
一、consul部署
1.基础环境
服务器:192.168.226.130 Docker-ce、 Compose 3、Consul、 Consul-template
服务器:192.168.226.131 Docker-ce、registrator
template 模板(更新)
registrator(自动发现)
后端每构建出一个容器,会向registrator进行注册,控制consul 完成更新操作,consul会触发consul template模板进行个更新
2.核心机制
consul :自动发现、自动更新,为容器提供服务(添加、删除、生命周期)
二、部署过程
1.consul服务器
mkdir /root/consul
cp consul_0.9.2_linux_amd64.zip /root/consul
cd /root/consul
unzip consul_0.9.2_linux_amd64.zip
mv consul /usr/bin
consul agent \
-server \ 服务端
-bootstrap \ 前端框架
-ui \ 可被访问的web界面
-data-dir=/var/lib/consul-data \
-bind=192.168.226.130 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &
consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.226.130 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &
查看群集状态信息
[root@consul consul]# consul members
Node Address Status Type Build Protocol DC
consul-server01 192.168.226.130:8301 alive server 0.9.2 2 dc1
[root@consul consul]# consul info | grep leader
leader = true
leader_addr = 192.168.226.130:8300
通过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.195.129","wan":"192.168.195.129"},"Meta":{},"CreateIndex":5,"ModifyIndex":6}]
2.容器服务自动加入consoul集群
- 安装 Gliderlabs/Registrator Gliderlabs/Registrator
可检查容器运行状态自动注册,还可注销 docker 容器的服务 到服务配置中心。 - 目前支持 Consul、Etcd 和 SkyDNS2。
在 192.168.226.131 节点,执行以下操作:
docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.226.131 \
consul://192.168.226.130:8500
3.测试服务发现功能是否正常
docker run -itd -p:83:80 --name test-01 -h test01 nginx
docker run -itd -p:84:80 --name test-02 -h test02 nginx
docker run -itd -p:88:80 --name test-03 -h test03 httpd
docker run -itd -p:89:80 --name test-04 -h test04 httpd
4.验证 http 和 nginx 服务是否注册到 consul
浏览器输入 http://192.168.226.130:8500,“单击 NODES”,然后单击 “consurl-server01”,会出现 5 个服务.
//在consul服务器上查看服务
[root@consul consul]# curl 127.0.0.1:8500/v1/catalog/services
{"consul":[],"httpd":[],"nginx":[]}
5.安装consoul服务器查看服务
- Consul-Template 是一个守护进程,用于实时查询 Consul 集群信息,并更新文件系统 上任意数量的指定模板,生成配置文件。更新完成以后,可以选择运行 shell 命令执行更新 操作,重新加Nginx。
- Consul-Template 可以查询 Consul 中的服务目录、Key、Key-values 等。
- 这种强大的抽象功能和查询语言模板可以使 Consul-Template 特别适合动态的创建配置文件。
- 例如:创建 Apache/Nginx Proxy Balancers、Haproxy Backends
6.准备template nginx模板
//在consul上操作
vim /root/consul/nginx.ctmpl
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}}; #??此处引用的变量会指向后端的地址和端口(动态变化)
{{end}}
}
server {
listen 83;
server_name localhost 192.168.226.130; #反向代理的IP地址
access_log /var/log/nginx/kgc.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;
}
}
7.编译安装nginx
yum install gcc pcre-devel zlib-devel -y
tar zxvf nginx-1.12.0.tar.gz -C /opt
./configure --prefix=/usr/local/nginx
make && make install
8.配置nginx
vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
include vhost/*.conf; //添加虚拟主机目录(consul动态生成的配置文件就会放在这里)
default_type application/octet-stream;
//创建虚拟主机目录
mkdir /usr/local/nginx/conf/vhost
//创建日志文件目录
mkdir /var/log/nginx
//启动nginx
/usr/local/nginx/sbin/nginx
9.配置并启动template
上传 consul-template_0.19.3_linux_amd64.zip 包到/root 目录下
cp consul-template_0.19.3_linux_amd64.zip /root/
unzip consul-template_0.19.3_linux_amd64.zip
mv consul-template /usr/bin/
//
consul-template -consul-addr 192.168.226.130:8500 \
-template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info
//另外打开一个终端查看生成配置文件
[root@consul ~]# cat /usr/local/nginx/conf/vhost/kgc.conf
upstream http_backend {
server 192.168.195.130:83;
server 192.168.195.130:84;
}
server {
listen 83;
server_name localhost 192.168.195.129;
access_log /var/log/nginx/kgc.cn-access.log;
index index.html index.php;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://http_backend;
}
}
10.增加一个nginx容器节点
//在registrator服务端注册
docker run -itd -p:85:80 --name test-05 -h test05 nginx
//在consul服务器监控装填会有提示自动更新
2019/12/30 14:59:21.750556 [INFO] (runner) initiating run
2019/12/30 14:59:21.751767 [INFO] (runner) rendered "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/kgc.conf"
2019/12/30 14:59:21.751787 [INFO] (runner) executing command "/usr/local/nginx/sbin/nginx -s reload" from "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/kgc.conf"
2019/12/30 14:59:21.751823 [INFO] (child) spawning: /usr/local/nginx/sbin/nginx -s reload
//查看三台nginx容器日志,请求正常轮询到各个容器节点上
docker logs -f test-01
docker logs -f test-02
docker logs -f test-05