Docker consul容器服务更新和发现
文章目录
- Docker consul容器服务更新和发现
-
- 前言
- 1.consul简介
- 2.建立consul服务
- 3.gliderlabs/registrator
- 4.consul-template
- 5.实验过程
前言
- docker consul的服务器的发现机制主要是依赖于consul服务,consul服务能够自动的发现节点上的registrator,registrator能够自适应的监控容器的状态,然后反馈到consul,接着通过web的8500端口,以一种前端的页面能够直观的看到在线的容器。
1.consul简介
- sonsul是hashicorp公司推出的开源工具,用于实现分布式系统的服务发现与配置
- consul的特性
- consul支持健康检查,允许存储键值对
- 一致性协议采用Raft算法,用来保证服务的高可用
- 成员管理和消息广播采用Gossip协议,支持ACL访问能控制
- 方便部署,与docker轻量级容器无缝配合
2.建立consul服务
- 每个提供服务的节点上都要部署和运行Consul的agent
- consul agent有两种模式
- server和client只是consul集群层面的区分,与搭建在cluster之上的应用服务无关
3.gliderlabs/registrator
- 检查容器运行状态
- 自动注册和注销docker容器的服务到服务配置中心
4.consul-template
- 是基于consul的自动替换配置文件的应用
- 可以查询consul中的服务目录、key、key-values等
- 特别适合动态的创建配置文件
- Consul-Template 是一个守护进程,用于实时查询Consul 集群信息,并更新文件系统上任意数量的指定模板,生成配置文件。更新完成以后,可以选择运行shell 命令执行更新操作,重新加载Nginx.、Consul-Template可以查询Consul 中的服务目录、Key、 Key- values 等。
这种强大的抽象功能和查询语言模板可以使Consul-Template特别适合动态的创建配置文件。
5.实验过程
#docker的部署
yum -y install yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum -y install docker-ce
#启动docker
systemctl restart docker
systemctl enable docker
#配置镜像加速
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors":["https://v8z6yng7.mirror.aliyuncs.com"]
}
EOF
#重启docker
systemctl daemon-reload
systemctl restart docker
#网络优化
vim /etc/sysctl.conf
net.ipv4.ip_forward=1
sysctl -p
service netowrk restart
systemctl restart docker
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 \
-data-dir=/var/lib/consul-data \
-bind=192.168.73.11 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &
consul members
consul info | grep leader
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服务信息
- 安装Gliderlabs/Registrator Gliderlabs/Registrator
可检查容器运行状态自动注册,还可注销docker容器的服务到服务配置中心。
目前支持consul、etcd和skyDNS2.
在192.168.73.12节点,执行以下操作:
docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.73.12 \
consul://192.168.73.11:8500
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:85:80 --name test-05 -h test05 nginx
- 在浏览器中查看,输入192168.73.11:8500
- Consul-Template 是一个守护进程,用于实时查询Consul 集群信息,并更新文件系统上任意数量的指定模板,生成配置文件。更新完成以后,可以选择运行shell 命令执行更新操作,重新加载Nginx. Consul-Template可以查询Consul 中的服务目录、Key、 Key- values 等。这种强大的抽象功能和查询语言模板可以使Consul-Template特别适合动态的创建配置文件。例如:创建Apache/Nginx Proxy Balancers、 Haproxy Backends
- 在consul上准备template nginx模板
vim /root/consul/nginx.ctmpl
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
server {
listen 83;
server_name localhost 192.168.73.11;
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;
}
}
yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel
tar -zxvf nginx-1.12.2.tar.gz -C /opt
./configure
--prefix=/usr/local/nginx
make && make install
vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
include vhost/*.conf;
default_type application/sctet-stream;
mkdir /usr/local/nginx/conf/vhost
mkdir /var/log/nginx
/usr/local/nginx/sbin/nginx
上传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.73.11:8500 \
-template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info
增加一个nginx容器节点,测试服务发现及配置更新功能
//在regi strator服务端注册
docker run -itd -p:86:80 --name test-06 -h test06 nginx
//在consu1服务器监控装填会有提示自动更新
2020/04/23 05:34:00.432917 [INFO] consul-template v0.19.3 (ebf2d3d)
2020/04/23 05:34:00.432945 [INFO] (runner) creating new runner (dry: false, once: false)
2020/04/23 05:34:00.435178 [INFO] (runner) creating watcher
2020/04/23 05:34:00.435470 [INFO] (runner) starting
2020/04/23 05:34:00.435500 [INFO] (runner) initiating run
2020/04/23 05:34:00.441936 [INFO] (runner) initiating run
2020/04/23 05:34:00.562420 [INFO] (runner) rendered "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/kgc.conf"
2020/04/23 05:34:00.562515 [INFO] (runner) executing command "/usr/local/nginx/sbin/nginx -s reload" from "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/kgc.conf"
2020/04/23 05:34:00.562675 [INFO] (child) spawning: /usr/local/nginx/sbin/nginx -s reload
#下面是更新的内容
2020/04/23 05:36:26.146062 [INFO] (runner) initiating run
2020/04/23 05:36:26.150574 [INFO] (runner) rendered "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/kgc.conf"
2020/04/23 05:36:26.150633 [INFO] (runner) executing command "/usr/local/nginx/sbin/nginx -s reload" from "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/kgc.conf"
2020/04/23 05:36:26.150719 [INFO] (child) spawning: /usr/local/nginx/sbin/nginx -s reload
- 在consul服务器上面查看vhost文件夹下面的信息
[root@localhost ~]# cd /usr/local/nginx/conf/vhost
[root@localhost vhost]# ls
kgc.conf
[root@localhost vhost]# vim kgc.conf
upstream http_backend {
server 192.168.73.12:83;
server 192.168.73.12:84;
server 192.168.73.12:85;
server 192.168.73.12:86;
}
server {
listen 83;
server_name localhost 192.168.73.11;
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;
}
}
- 在浏览器中查看,输入192.168.73.11:8500
docker rm 897ad786f003
#consul服务器中
2020/04/23 05:50:16.358956 [INFO] (runner) executing command "/usr/local/nginx/sbin/nginx -s reload" from "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/kgc.conf"
2020/04/23 05:50:16.359250 [INFO] (child) spawning: /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# docker logs -f test-02
192.168.73.11 - - [24/Apr/2020:05:52:13 +0000] "GET / HTTP/1.0" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "192.168.73.1"
192.168.73.11 - - [24/Apr/2020:05:52:16 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "192.168.73.1"
192.168.73.11 - - [24/Apr/2020:05:52:17 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "192.168.73.1"
192.168.73.11 - - [24/Apr/2020:05:52:17 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "192.168.73.1"
192.168.73.11 - - [24/Apr/2020:05:52:18 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "192.168.73.1"
^C
[root@localhost ~]# docker logs -f test-05
2020/04/24 05:52:13 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.73.11, server: localhost, request: "GET /favicon.ico HTTP/1.0", host: "192.168.73.11", referrer: "http://192.168.73.11:83/"
192.168.73.11 - - [24/Apr/2020:05:52:13 +0000] "GET /favicon.ico HTTP/1.0" 404 556 "http://192.168.73.11:83/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "192.168.73.1"
192.168.73.11 - - [24/Apr/2020:05:52:16 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "192.168.73.1"
192.168.73.11 - - [24/Apr/2020:05:52:17 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "192.168.73.1"
192.168.73.11 - - [24/Apr/2020:05:52:17 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "192.168.73.1"
192.168.73.11 - - [24/Apr/2020:05:52:18 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "192.168.73.1"
^C
[root@localhost ~]# docker logs -f test-06
192.168.73.11 - - [24/Apr/2020:05:52:16 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "192.168.73.1"
192.168.73.11 - - [24/Apr/2020:05:52:16 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "192.168.73.1"
192.168.73.11 - - [24/Apr/2020:05:52:17 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "192.168.73.1"
192.168.73.11 - - [24/Apr/2020:05:52:17 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "192.168.73.1"
192.168.73.11 - - [24/Apr/2020:05:52:18 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "192.168.73.1"
^C