Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置
与Docker等轻量级容器可无缝配合
template 模板(更新)
registrator(自动发现)
后端每构建出一个容器,会向registrator进行注册,控制consul 完成更新操作,consul会触发consul template模板进行热更新
核心机制:consul :自动发现、自动更新,为容器提供服务(添加、删除、生命周期)
Consul的应用场景包括服务发现、服务隔离、服务配置:
每个提供服务的节点上都要部署和运行Consul的agent
Consul agent有两种运行模式
Server
Client
Server和Client只是Consul集群层面的区分,与搭建在Cluster之上的应用服务无关
192.168.28.200 Docker-ce、compose、consul、consul-template、nginx
192.168.28.250 Docker-ce、registrator
mkdir consul #创建consul目录
cd consul
rz consul_0.9.2_linux_amd64.zip
unzip consul_0.9.2_linux_amd64.zip #解压
mv consul /usr/bin #将解压出来的可执行文件移动到路径环境变量下,便于系统识别
consul agent \ #进行初始化
-server \
-bootstrap \
-ui \ #UI界面
-data-dir=/var/lib/consul-data \ #数据储存位置
-bind=192.168.28.200 \ #绑定地址
-client=0.0.0.0 \ #监听地址,所有
-node=consul-server01 &> /var/log/consul.log &
consul members #插卡集群成员
consul info | grep leader #查看leader信息
查看集群server成员 | curl 127.0.0.1:8500/v1/status/peers |
---|---|
集群Raf leader | curl 127.0.0.1:8500/v1/status/leader |
注册的所有服务 | curl 127.0.0.1:8500/v1/catalog/services |
查看nginx服务信息 | curl 127.0.0.1:8500/v1/catalog/nginx |
集群节点详细信息 | curl 127.0.0.1:8500/v1/catalog/nodes |
[root@localhost ~]# docker run -d --name=registrator --net=host -v /var/run/docker.sock:/tmp/docker.sock --restart=always gliderlabs/registrator:latest -ip=192.168.30.9 consul://192.168.28.200:8500 // Gliderlabs/Registrator 可检查容器运行状态自动注册,还可注册docker容器的服务到服务配置中心
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cbca419d2d4b gliderlabs/registrator:latest "/bin/registrator -i…" 8 minutes ago Up 8 minutes registrator
[root@localhost ~]# docker run -itd -p:83:80 --name test-01 -h test01 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
a330b6cecb98: Pull complete
5ef80e6f29b5: Pull complete
f699b0db74e3: Pull complete
0f701a34c55e: Pull complete
3229dce7b89c: Pull complete
ddb78cb2d047: Pull complete
Digest: sha256:a05b0cdd4fc1be3b224ba9662ebdf98fe44c09c0c9215b45f84344c12867002e
Status: Downloaded newer image for nginx:latest
3bc856898e0d14c1ff547bdd34a3d157ac44062935727e2f57d6a4beeaeb21b3
[root@localhost ~]# docker run -itd -p:84:80 --name test-02 -h test02 nginx
1c8e9f620c5be04ec3ddecdd89db222b497f538a0423f18ad2106084ae9bbc44
[root@localhost ~]# docker run -itd -p:88:80 --name test-03 -h test03 httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
a330b6cecb98: Already exists
14e3dd65f04d: Pull complete
fe59ad2e7efe: Pull complete
2cb26220caa8: Pull complete
3138742bd847: Pull complete
Digest: sha256:af1199cd77b018781e2610923f15e8a58ce22941b42ce63a6ae8b6e282af79f5
Status: Downloaded newer image for httpd:latest
be4d97f560813250557a13fa846172fda96fdba0fa5d7f6a0fbd33ebcd96bbf7
[root@localhost ~]# docker run -itd -p:89:80 --name test-04 -h test04 httpd
d6b09bf6bed78c605282bd220ba56b12fae166016c1f32daae6af121e2148f5c
[root@localhost ~]#
[root@localhost consul]# curl 127.0.0.1:8500/v1/catalog/services 在consul服务器上查看服务
{"consul":[],"httpd":[],"nginx":[]}
Consul-Template是一个守护进程,用于实时查询Consul集群信息,并更新文件系统上任意数量的指定模板,生成配置文件,更新完成以后,可以查询Consul中的服务目录,Key、Key-values等。
[root@localhost consul]# cat nginx.ctmpl
upstream http_backend { #四层转发模块
{{range service "nginx"}} //过滤nginx服务器,搜索consul中的nginx服务
server {{.Address}}:{{.Port}}; //此处引用的变量会指向后端的地址和端口(动态变化)
{{end}}
}
server {
listen 83; #监听端口
server_name localhost 192.168.30.8;
access_log /var/log/nginx/test.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; #真实IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://http_backend; #反向代理调用转发池
}
}
yum -y install gcc pcre-devel zlib-devel
ls
tar zxvf nginx-1.15.9.tar.gz -C /opt
cd /opt/nginx-1.15.9
./configure --prefix=/usr/local/nginx && make && make install //手工编译安装nginx,定义安装路径
vim /usr/local/nginx/conf/nginx.conf
//19行添加 include vhost/*.conf;
http {
include mime.types;
19 include vhost/*.conf //添加虚拟主机目录
default_type application/octet-stream;
mkdir /usr/local/nginx/conf/vhost //创建虚拟主机目录
mkdir /var/log/nginx //创建日志文件目录
/usr/local/nginx/sbin/nginx //启动nginx
cd /opt
rz consul-template_0.19.3_linux_amd64.zip
unzip consul-template_0.19.3_linux_amd64.zip
mv consul-template /usr/bin #将可执行文件路径移动到环境变量中,便于系统识别
consul-template -consul-addr 192.168.28.200:8500 \
> -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/test.conf:/usr/local/nginx/sbin/nginx -s reload" \
> --log-level=info
#将监听到的nginx.ctmpl文件同步到vhost目录的文件下,并重启服务日志级别info
[root@yzq200 opt]# cat /usr/local/nginx/conf/vhost/test.conf
upstream http_backend {
server 192.168.30.9:83;
server 192.168.30.9:84;
}
server {
listen 83;
server_name localhost 192.168.28.200;
access_log /var/log/nginx/test.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;
}
}
增加
[root@localhost ~]# docker run -itd -p:85:80 --name test-05 -h test05 nginx //增加节点
e8571c4ed2a1b154892c9b8035d40a45302b9299976c11e5a835c672c721b0f6
[root@localhost ~]# cat /usr/local/nginx/conf/vhost/kgc.conf
upstream http_backend {
server 192.168.30.9:83;
server 192.168.30.9:84;
server 192.168.30.9:85;
[root@localhost ~]# docker rm -f test-01
test-01
[root@yzq200 opt]# cat /usr/local/nginx/conf/vhost/test.conf
upstream http_backend {
server 192.168.30.9:84;
server 192.168.30.9:85;
}
server {
listen 83;
server_name localhost 192.168.28.200;
access_log /var/log/nginx/test.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;
}
}