基于Docker的consul群集服务搭建(自动发现和配置服务)

一、consul简介

consul的特性:

  • 服务发现:支持服务发现。你可以通过 DNS 或 HTTP 的方式获取服务信息。
  • 健康检查:支持健康检查。可以提供与给定服务相关联的任何数量的健康检查(如 web 状态码或 cpu 使用率)。
  • K/V 存储:键/值对存储。你可用通过 consul 存储如动态配置之类的相关信息。
  • 多数据中心:支持多数据中心,开箱即用。内外网的服务采用不同的端口进行监听。 多数据中心集群可以避免单数据中心的单点故障,而其部署则需要考虑网络延迟,分片等情况等.
  • WEB-UI:支持 WEB-UI。点点点,你就能够了解你的服务现在的运行情况,一目了然,对开发运维是非常友好的

consul的端口:
在这里插入图片描述
consul工具能够对容器服务更新和发现的原理图
在这里插入图片描述

二、consul集群服务的搭建

2.1 案例环境:

主机主机IP安装工具
consul服务器192.168.247.140Docker、Consul、Consul-template模板、nginx代理服务
容器服务器192.168.247.150Docker、registrator、nginx容器

2.2 首先在两个主机上都安装docker,安装方法见 docker安装

2.3 在consul服务器主机上:构建consul服务

●每个提供consul服务的节点上都要部署和运行Consul的agent
●Consul agent有两种运行模式:Server 和 Client
●Server和Client只是Consul集群层面的区分,与搭建在Cluster之上的应用服务无关

2.3.1 创建consul总目录,上传安装包Consul,并解压

[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  #使得全局可使用此命令工具

2.3.2 创建consul代理agent

[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

2.3.3 查看consul的各个信息

[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}]

2.3.4 验证consul服务

访问192.168.247.140:8500,可见consul工具的界面
在这里插入图片描述

2.4 容器服务器的部署

2.4.1 搭建registrator

此工具可检查容器运行状态,自动注册和注销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":[]}

2.5 consul-template自动配置

  • Consul-Template 是一个守护进程,用于实时查询Consul 集群信息,并更新文件系统上任意数量的指定模板,生成配置文件。 更新完成以后,可以选择运行shell 命令执行更新操作,重新加载Nginx。
  • Consul-Template可以查询 Consul 中的服务目录、Key、Key-values 等。
    这种强大的抽象功能和查询语言模板可以使Consul-Template 特别适合动态的创建配置文件。
    例如:创建Apache/Nginx Proxy Balancers、Haproxy Backends

在consul服务器部署

2.5.1 准备template的nginx模板文件

[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;
  }
}

2.5.2 编译安装nginx

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服务

2.5.3 配置consul-template

[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
#不可中断,再打开一个终端窗口

2.5.4 验证consul-template自动配置

在新终端查看虚拟主机的配置文件

[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的反向代理服务提供了很大的便利,可自动的添加删除节点。

2.6 consul多节点配置

新的主机节点上安装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已有的群集
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值