consul的特性:
consul的端口:
consul工具能够对容器服务更新和发现的原理图
主机 | 主机IP | 安装工具 |
---|---|---|
consul服务器 | 192.168.247.140 | Docker、Consul、Consul-template模板、nginx代理服务 |
容器服务器 | 192.168.247.150 | Docker、registrator、nginx容器 |
●每个提供consul服务的节点上都要部署和运行Consul的agent
●Consul agent有两种运行模式:Server 和 Client
●Server和Client只是Consul集群层面的区分,与搭建在Cluster之上的应用服务无关
[root@server2 ~]# mkdir consul
[root@server2 ~]# cd consul/
[root@server2 consul]# mv /root/consul_0.9.2_linux_amd64.zip ./
[root@server2 consul]# unzip consul_0.9.2_linux_amd64.zip
[root@server2 consul]# mv consul /usr/bin #使得全局可使用此命令工具
[root@server2 consul]# consul agent \
> -server \ #定义本机运行模式为server
> -bootstrap \
> -ui \ #可视化界面
> -data-dir=/var/lib/consul-data \
> -bind=192.168.247.140 \ #监听自己
> -client=0.0.0.0 \ #监听所有地址
> -node=consul-server01 &> /var/log/consul.log & #放入后台运行
[1] 58533
[root@server2 consul]# consul members #集群成员
Node Address Status Type Build Protocol DC
consul-server01 192.168.247.140:8301 alive server 0.9.2 2 dc1
[root@server2 consul]# consul info | grep leader
leader = true
leader_addr = 192.168.247.140:8300
[root@server2 consul]# curl 127.0.0.1:8500/v1/status/peers
["192.168.247.140:8300"]
[root@server2 consul]# curl 127.0.0.1:8500/v1/status/leader
"192.168.247.140:8300"
[root@server2 consul]# curl 127.0.0.1:8500/v1/catalog/services #consul集群提供的服务
{"consul":[]}
127.0.0.1:8500/v1/catalog/nodes #节点详细信息
[{"ID":"399b0346-1c39-8e8c-6d15-8512cd95a11f","Node":"consul-server01","Address":"192.168.247.140","Datacenter":"dc1","TaggedAddresses":{"lan":"192.168.247.140","wan":"192.168.247.140"},"Meta":{},"CreateIndex":5,"ModifyIndex":6}]
访问192.168.247.140:8500,可见consul工具的界面
此工具可检查容器运行状态,自动注册和注销docker容器的服务到服务配置中心
[root@client1 ~]# docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \ #镜像名
-ip=192.168.247.150 \ #自己的IP
consul://192.168.247.140:8500 #指向consul服务器
查看registrator运行状态
[root@client1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
54c25096b452 gliderlabs/registrator:latest "/bin/registrator -i…" About a minute ago Up About a minute registrator
创建两个容器服务,测试consul是否可自动发现这两个服务
[root@client1 ~]# docker run -itd -p 83:80 --name test1 -h test01 nginx
[root@client1 ~]# docker run -itd -p 84:80 --name test2 -h test02 nginx
浏览器访问192.168.247.140:8500。选择NODES标签查看,选择consul-server01,显示有三个服务,和健康状态
或者用命令在consul服务器查看
[root@server2 ~]# curl 127.0.0.1:8500/v1/catalog/services #共发现三个服务
{"consul":[],"nginx":[]}
在consul服务器部署
[root@server2 ~]# cd consul/
[root@server2 consul]# vi nginx.ctmpl
upstream httpd_back { #地址池
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
server { #代理服务
listen 88;
server_name localhost 192.168.247.140;
access_log /var/log/nginx/nginx.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://httpd_back;
}
}
yum install -y gcc gcc-c++ pcre-devel zlib-devel
tar zxvf nginx-1.12.2.tar.gz -C /opt/
cd /opt/nginx-1.12.2
./configure --prefix=/usr/local/nginx
make && make install
vi /usr/local/nginx/conf/nginx.conf
[root@server2 ~]# mkdir /usr/local/nginx/conf/vhost
#启动template之后,会自动根据模板在此目录下动态生成配置文件,一旦发现新容器,即开始自动更新配置文件
[root@server2 ~]# mkdir /var/log/nginx
[root@server2 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/bin/
[root@server2 ~]# nginx #启动nginx服务
[root@server2 ~]# cd consul/
[root@server2 consul]# unzip consul-template_0.19.3_linux_amd64.zip
Archive: consul-template_0.19.3_linux_amd64.zip
inflating: consul-template
[root@server2 consul]# mv consul-template /usr/bin
#启动服务并指明nginx配置文件
[root@server2 consul]# consul-template -consul-addr 192.168.247.140:8500 -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/zf.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info
2020/11/30 14:45:16.230017 [INFO] consul-template v0.19.3 (ebf2d3d)
2020/11/30 14:45:16.230068 [INFO] (runner) creating new runner (dry: false, once: false)
2020/11/30 14:45:16.230727 [INFO] (runner) creating watcher
2020/11/30 14:45:16.231198 [INFO] (runner) starting
2020/11/30 14:45:16.231221 [INFO] (runner) initiating run
2020/11/30 14:45:16.233590 [INFO] (runner) initiating run
2020/11/30 14:45:16.235042 [INFO] (runner) rendered "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/zf.conf"
2020/11/30 14:45:16.235073 [INFO] (runner) executing command "/usr/local/nginx/sbin/nginx -s reload" from "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/zf.conf"
2020/11/30 14:45:16.235118 [INFO] (child) spawning: /usr/local/nginx/sbin/nginx -s reload
#不可中断,再打开一个终端窗口
在新终端查看虚拟主机的配置文件
[root@server2 ~]# cd /usr/local/nginx/conf/vhost/
[root@server2 vhost]# cat zf.conf
upstream httpd_back {
server 192.168.247.150:83; #consul自动发现的容器服务,通过template自动更新了配置文件
server 192.168.247.150:84;
}
server {
listen 88;
server_name localhost 192.168.247.140;
access_log /var/log/nginx/nginx.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://httpd_back;
}
}
浏览器访问consul服务器的nginx服务,并刷新,查看nginx容器服务的日志,发现是轮询访问两个test-01,test-02的nginx服务
查看test-01日志,并刷新
再刷新,查看test-02日志
再增加一个容器节点测试consul服务
[root@client1 ~]# docker run -itd -p:86:80 --name test-00 -h test00 nginx
consul服务器的template监控端会自动更新配置文件,并跳出提示
查看虚拟主机配置文件
[root@server2 consul]# cd /usr/local/nginx/conf/vhost/
[root@server2 vhost]# cat zf.conf
upstream httpd_back {
server 192.168.247.150:86; #新增nginx节点容器
server 192.168.247.150:83;
server 192.168.247.150:84;
}
server {
listen 88;
server_name localhost 192.168.247.140;
access_log /var/log/nginx/nginx.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://httpd_back;
}
}
这样就完全实现了consul服务的自动发现和自动配置的两个功能,为nginx的反向代理服务提供了很大的便利,可自动的添加删除节点。
新的主机节点上安装docker和consul工具,进行consul配置和启动:
consul agent\
-server \
-bootstrap \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.247.160 \ #监听自己
-client=0.0.0.0 \
-node=consul-server02 \
-enable-script-checks=true \ #设置检查服务为可用
-datacenter=dc1 \ #数据中心名称
-join 192.168.247.140 &> /var/log/consul.log & #加入server01已有的群集