文章目录
一、Consul
1、Consul概述
后端每构建出一个容器就会向registrator注册,控制consul完成更新操作,consul会触发consul template模板进行热更新。
2、Consul特性
①支持健康检查,允许存储键值对
②基于go语言开发,可移植性强
③支持ACL访问控制
3、使用场景
Consul的使用场景包括服务发现、服务隔离、服务配置
①服务发现场景中consul作为注册中心,服务地址被注册到consul益州,可以使用consul提供的dns、http接口查询,consul支持健康检查。
②服务隔离场景中consul支持以服务为单位设置访问策略,能同时支持经典平台和新兴平台。
③服务配置场景中consul提供key-value数据存储功能,并且能够将变动迅速通知下去,借助consul可以实现配置共享,需要读取配置的服务可以从consul中读取到准确的配置信息。
④consul可以帮助系统管理者更清晰了解复杂系统内部系统架构,运维可以将consul看成一种监控软件,也可以堪称一种资源管理系统。
二、搭建consul集群
1、环境
server端
192.168.10.101
docker-ce docker-compose consul nginx consul-template
client端
192.168.10.102
docker-ce registrator nginx
2、service配置
创建目录
上传文件并解压到指定目录
配置
consul agent
-server
-bootstrap
-ui
-data-dir=/var/lib/consul-data
-bind=192.168.10.101
-client=0.0.0.0
-node=consul-server01 &> /var/log/consul.log &
查看
集群server成员
curl 127.0.0.1:8500/v1/status/peers
集群leader
curl 127.0.0.1:8500/v1/status/leader
注册的服务
curl 127.0.0.1:8500/v1/catalog/sercices
查看服务信息
curl 127.0.0.1:8500/v1/catalog/nginx
查看集群节点详细信息
curl 127.0.0.1:8500/v1/catalog/nodes
3、client配置
docker run -d
–name=registrator
–net=host
-v /var/run/docker.sock:/tmp/docker.sock
–restart=always
gliderlabs/registrator:latest
-ip=192.168.10.102
consul://192.168.10.101:8500
docker images
拉取一个registrator镜像
Gliderlabs/Registrator 可检查容器运行状态自动注册,还可注销 docker 容器的服务到服务配置中心。目前支持 Consul、Etcd 和 SkyDNS2。
4、验证
启动3个容器
访问8500端口
三、配置template模板自动更新
1、consul-template概述
consul-template是一个守护进程,用于实时查询consul集群信息并更新文件系统上任意数量的指定模板生成配置文件,更新完成以后,可以查询consul中的服务目录、key、key-values等。
2、template nginx模板文件
upstream http_backend {
{{range service “nginx”}}
server {{.Address}}:{{.Port}};
{{end}}
}
server {
listen 100;
server_name localhost 192.168.10.101;
access_log /var/log/nginx/lic.com-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;
}
}
3、安装nginx并启动
安装
yum -y install pcre-devel zlib-devel gcc gcc-c++ make &> /dev/null
useradd -M -s /sbin/nologin nginx
wget http://101.34.22.188/lnmp_wordpress/nginx-1.15.9.tar.gz -P /opt &> /dev/null
tar xf /opt/nginx-1.15.9.tar.gz -C /opt/
cd /opt/nginx-1.15.9/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make -j 4 && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/.
添加虚拟主机目录
vim /usr/local/nginx/conf/nginx.conf
创建虚拟主机
4、编译安装template
5、前台启动
consul-template -consul-addr 192.168.10.101:8500 -template “/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/lic.conf:/usr/local/nginx/sbin/nginx -s reload” --log-level=info
将监听到的nginx.conf文件同步到vhost目录下的lic.conf文件中并重启服务日志级别info
6、测试
再添加一个nginx容器
自动更新
7、增加docker节点,测试
consul agent
-server
-ui
-data-dir=/var/lib/consul-data
-bind=192.168.10.104
-client=0.0.0.0
-node=consul-server02
-enable-script-checks=true
-datacenter=dc1
-join 192.168.10.101 &> /var/log/consul.log &
-enable-script-checks=true :设置检查服务为可用
-datacenter : 数据中心名称
-join :加入到已有的集群中
主机检测