一、概念
1.consul
1.Consul是HashiCorp公司推出的开源工具,Consul由Go语言开发,部署起来非常容易,只需要极少的可执行程序和配置文件,具有绿色、轻量级的特点。
2.Consul是分布式的、高可用的、可横向扩展的用于实现分布式系统的服务发现与配置。
2.Consul的作用
- 服务注册与发现(主要功能),提供HTTP和DNS两种发现方式
- 健康检查,支持多种协议,HTTP、TCP等
- Key/Value存储
- 支持多数据中心
- 基于Golong语言,可移植性强
- 支持ACL访问控制
- 与Docker等轻量级容器可无缝配合
3.consul-template概述(更新)
定义标准的格式,以及管理相关前端服务的配置文件
1.Consul-Template是一个守护进程,用于实时查询Consul集群信息
2.Consul-Template可以更新文件系统上任意数量的指定模板,生成配置文件
- 更新完成以后,可以选择运行shell命令执行更新操作,重新加载Nginx。
3.Consul-Template可以查询Consul中的服务目录、Key、Key-values等。
4.这种强大的抽象功能和查询语言模板可以使Consul-Template特别适合动态的创建配置文件。
- 例如:创建Apache/Nginx Proxy Balancers、Haproxy Backends
4.registrator的作用(自动发现)
一个由Go语言编写的,针对docker使用的,可以用于检测容器状态,自动注册和注销docker容器的服务到服务配置中心。目前支持Consul、Etcd和SkyDNS2
**总结:**后端每构建出一个容器,会向regitrator进行注册,控制consul完成更新操作,consul会触发consul template模板进行更新,核心机制:consul:自动发现自动更新,为容器提供服务(添加,删除,生命周期)
二、实验部署
实验背景:nginx+tomcat实现对tomcat的反向代理
问题/需求:当后端tomcat节点大批量增加,需要在保证服务不中断的情况下在nginx配置文件增加这些后端节点的地址
案例环境
主机 | 操作系统 | IP地址 | 主要软件及版本 |
---|---|---|---|
consul | Centos 7 | 192.168.190.11 | Docker 、Consul、Consul-template |
registrator | Centos 7 | 192.168.190.12 | Docker、registrator |
(一)部署consul服务器
192.168.190.11
1.拖入安装包并安装
mkdir /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 \ #consul数据目录
-bind=192.168.190.11 \ #本地地址
-client=0.0.0.0 \ #表示所有
-node=consul-server01 &> /var/log/consul.log & # &后台运行
consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/log/consul-data \
-bind=192.168.190.11 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &
2.查看集群信息
consul members
[root@localhost consul]#consul members
Node Address Status Type Build Protocol DC
consul-server01 192.168.190.11:8301 alive server 0.9.2 2 dc1
consul info | grep leader #查看consul集群架构并查询leader
[root@localhost consul]#consul info | grep leader
leader = true
leader_addr = 192.168.190.11:8300
----consul常用端口及功能----
8300:集群内数据的读写和复制
8301:单个数据中心gossip协议通讯
8302:跨数据中心gossip协议通讯
8500:提供获取服务列表、注册列表、注销服务等http接口,提供UI服务
8600:采用DNS协议提供服务发现功能
3.通过http 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 //集群节点详细信息
(二)容器服务自动加入consul集群
192.168.190.12
1.安装Gliderlabs/registrator
可检查容器运行状态自动注册,还可以注销docker容器服务,目前支持consul ,etcd和SkyDNS2
docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.190.12 \
consul://192.168.190.11:8500
2.测试服务
测试发现功能是否正常
docker run -itd -p:81:80 --name test-01 -h test01 nginx
docker run -itd -p:82: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
3.验证http和nginx服务是否注册到consul
浏览器输入http://192.168.190.11:8500,“单击NODES”,然后单击“consurl-server01”,会出现5个服务
192.168.190.11验证
curl 127.0.0.1:8500/v1/catalog/services
[root@localhost consul]#curl 127.0.0.1:8500/v1/catalog/services
{"consul":[],"httpd":[],"nginx":[]}
(三)安装consul-template
== consul:192.168.190.11==
1.拖入安装包解压
unzip consul-template_0.19.3_linux_amd64.zip
mv consul-template /usr/bin/
2.准备template nginx模板文件
consul:192.168.190.11
vim /root/consul/nginx.ctmpl
upstream http_backend { #配置地址池名称
{{range service "nginx"}} #指定服务范围
server {{.Address}}:{{.Port}}; #读取服务节点地址端口 server ip port
{{end}} #以end结尾
}
server {
listen 81; #指定监听的consul地址81或者88
server_name localhost 192.168.190.11; #反向代理服务器的地址
access_log /var/log/nginx/nginx01-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;
}
}
------------------
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
server {
listen 81;
server_name localhost 192.168.190.11;
access_log /var/log/nginx/nginx01-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
192.168.190.11
yum install -y gcc pcre-devel zlib-devel
cd /opt
tar zxvf nginx-1.12.0.tar.gz
cd nginx-1.12.0/
./configure --prefix=/usr/local/nginx
make && make install
3.1配置nginx配置文件
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
4.启动template
consul:192.168.190.11
consul-template -consul-addr 192.168.190.11:8500 \
#指定consul集群的serverip端口
-template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/local/nginx/sbin/nginx -s reload" \
#将反向代理服务器的nginx配置文件关联模板文件并重载
--log-level=info
重开一个终端
192.168.190.11
cat /usr/local/nginx/conf/vhost/kgc.conf
upstream http_backend {
server 192.168.190.12:81;
server 192.168.190.12:82;
}
server {
listen 81;
server_name localhost 192.168.190.11;
access_log /var/log/nginx/nginx01-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;
}
}
5.测试访问
192.168.190.12:81
192.168.190.12:82
192.168.190.11:81
(四)测试是否能自动发现
192.168.190.12增加一台后端节点容器服务
docker run -itd -p:83:80 --name test-05 -h test05 nginx
(五)部署consul多节点
192.168.190.13
添加一台已有docker环境的服务器192.168.190.13/24加入已有的群集中
mkdir /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.190.13 \
-client=0.0.0.0 \
-node=consul-server02 \
-enable-script-checks=true \
-datacenter=dc1 \
-join 192.168.190.11 &> /var/log/consul.log &
#--------参数解释--------------------------------
-enable-script-checks=true:设置检查服务为可用
-datacenter:数据中心名称
-join:加入到已有的集群中