一、 Consul
(一)、Consul简介
1、Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置
2、Consul的特性
- 支持健康检查,允许存储键值对
- 基于Golong语言,可移植性强
- 支持ACL访问控制
3、与Docker等轻量级容器可无缝配合
4、做服务发现的框架常用的有:
- zookeeper
- eureka
- etcd
- consul
(二)、Docker Consul容器服务更新与发现
容器服务更新与发现拓扑图
二、构建自动发现的Docker 服务架构部署步骤
项目需求:
公司提出一个新需求,使用Docker将Consul、Consul Template、Registrator 和Nginx组装成一个值得信任且可扩展的服务框架,可在这个框架中添加和移除服务,不需要重写任何配置,也不需要重启任何服务,一切都能正常运行
项目步骤:
服务器IP | 安装软件与服务 |
---|---|
服务器: 192.168.200.40 | Docker-ce、Compose 3、Consul、Consul-template |
服务器: 192.168.200.60 | Docker-ce、registrator |
(一)部署consul步骤(192.168.200.40)
关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
mkdir /root/consul
cd /root/consul
#将consul.0.9.2_linux_amd64.zip上传到/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.200.40 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &
//查看集群信息
[root@localhost consul]#consul members
Node Address Status Type Build Protocol DC
consul-server01 192.168.200.40:8301 alive server 0.9.2 2 dc1
[root@localhost consul]#consul info | grep leader
leader = true
leader_addr = 192.168.200.40: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服务信息
curl 127.0.0.1:8500/v1/catalog/nodes //集群节点详细信息
(二)、容器服务自动加入nginx集群
1.安装Gliderlabs/Registrator Gliderlabs/Registrator
可检查容器运行状态自动注册,还可注销docker容器的服务到服务配置中心。
目前支持Consul、Etcd和SkyDNS2
1)、在192.168.200.60节点,执行以下操作
docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.200.60 \
consul://192.168.200.40:8500
2.测试服务发现功能是否正常
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:99:80 --name test-03 -h test03 httpd
docker run -itd -p:88:80 --name test-04 -h test04 httpd
3.验证http和nginx服务是否注册到consul
浏览器输入http://192.168.200.40:8500,“单击NODES",然后单击"consurl-server01" ,会出现5个服务
2)、在consul服务器.上查看服务
curl 127.0.0.1:8500/v1/catalog/services
{"consul":[],"httpd":[], "nginx":[]}
2、安装consul-template
Consul-Template是一个守护进程,用于实时查询Consul集群信息,并更新文件系统上任意数量的指定模板,生成配置文件。更新完成以后,可以选择运行shell命令执行更新操作,重新加载Nginx。 Consul-Template可以查询Consul中的服务目录、Key、Key-values等。
这种强大的抽象功能和查询语言模板可以使Consul-Template特别适合动态的创建配置文件。
例如:创建Apache/Nginx Proxy Balancers、Haproxy Backends
3、准备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.200.40;
access_log /var/log/nginx/gcc.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;
}
}
4、编译安装nginx(在consul上操作)
yum install gcc pcre-devel zlib-devel -y
tar zxvf nginx-1.12.0.tar.gz -C /opt
cd //opt/nginx-1.12.0
./configure --prefix=/usr/local/nginx
make && make install
5、配置nginx (在consul上操作)
vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
include vhost/*.conf; #添加虚拟主机目录
default_type application/octet-stream;
//创建虚拟主机目录
mkdir /usr/local/nginx/conf/vhost
//创建日志文件目录
mkdir /var/log/nginx
//启动nginx
/usr/local/nginx/sbin/nginx
6、配置并启动 template(在consul上操作)
cd /root
#上传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.200.40:8500 -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/test.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info
//另外打开-个终端查看生成配置文件
[root@localhost vhost]#cat /usr/local/nginx/conf/vhost/test.conf
upstream http_backend {
server 192.168.200.60:83;
server 192.168.200.60:84;
}
server {
listen 83;
server_name localhost 192.168.200.40;
access_log /var/log/nginx/gcc.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;
}
}
(三)、增加一个nginx容器节点
#增加一个nginx容器节点,测试服务发现及配置更新功能
//在registrator(192.168.200.60)服务端注册
docker run -itd -p:85:80 --name test-05 -h test05 nginx
//在consul服务器监控装填会有提示自动更新
2021/03/26 05:51:59.624677 [INFO] (child) spawning: /usr/local/nginx/sbin/nginx -s reload
2021/03/26 05:53:09.576538 [INFO] (runner) initiating run
2021/03/26 05:53:09.577487 [INFO] (runner) rendered "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/test.conf"
2021/03/26 05:53:09.577505 [INFO] (runner) executing command "/usr/local/nginx/sbin/nginx -s reload" from "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/test.conf"
2021/03/26 05:53:09.577534 [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
(四)、consul多节点配置
//添加一台已有docker环境的服务器192.168.200.30/24加入已有的群集中
consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/ib/consul-data \
-bind=192.168.200.30 \
-client=0.0.0.0 \
-node=consul-server02 \
-enable-script-checks=true \
-datacenter=dc1 \
-join 192.168.200.40 &> /var/log/consul.log &
-enable-script-checks=true: 设置检查服务为可用
-datacenter: 数据中心名称
-join: 加入到已有的集群中