构建自动发现的Docker架构 Consul

13 篇文章 0 订阅
1 篇文章 0 订阅

一、 Consul

(一)、Consul简介

1、Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置
2、Consul的特性

支持健康检查,允许存储键值对
基于Golong语言,可移植性强
支持ACL访问控制

3、与Docker等轻量级容器可无缝配合
4、做服务发现的框架常用的有:

zookeeper
eureka
etcd
consul

(二)、Docker Consul容器服务更新与发现

容器服务更新与发现拓扑图

在这里插入图片描述

二、构建自动发现的Docker 服务架构部署步骤

项目需求:
使用Docker将Consul、Consul Template、Registrator 和Nginx组装成一个值得信任且可扩展的服务框架,可在这个框架中添加和移除服务,不需要重写任何配置,也不需要重启任何服务,一切都能正常运行

项目步骤:

服务器IP安装软件与服务
服务器: 192.168.118.18Docker-ce、Compose 3、Consul、Consul-template
服务器: 192.168.118.17Docker-ce、registrator

(一)部署consul步骤(192.168.118.18)

关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
setenforce 0


mkdir /root/consul
cd /root/consul

#将consul.0.9.2_linux_amd64.zip上传到/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.118.18 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &

//查看集群信息
[root@localhost consul]#consul members
Node             Address              Status  Type    Build  Protocol  DC
consul-server01  192.168.118.18:8301  alive   server  0.9.2  2         dc1


[root@localhost consul]#consul info | grep leader
	leader = true
	leader_addr = 192.168.118.18:8300

//通过httpd 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     //集群节点详细信息

在这里插入图片描述
在这里插入图片描述

(二)容器服务自动加入nginx集群

1.安装Gliderlabs/Registrator Gliderlabs/Registrator

可检查容器运行状态自动注册,还可注销docker容器的服务到服务配置中心。
目前支持Consul、Etcd和SkyDNS2

1)、在192.168.118.17节点,执行以下操作

docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.118.17 \
consul://192.168.118.18:8500

2.测试服务发现功能是否正常
docker run -itd -p:83:80 --name test-01 -h test01 nginx
docker run -itd -p:84:80 --name test-02 -h test02 nginx
docker run -itd -p:99:80 --name test-03 -h test03 httpd
docker run -itd -p:88:80 --name test-04 -h test04 httpd

3.验证http和nginx服务是否注册到consul
浏览器输入http://192.168.118.18:8500,“单击NODES",然后单击"consurl-server01" ,会出现5个服务

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
2)、在192.168.118.18 consul服务器.上查看服务

curl 127.0.0.1:8500/v1/catalog/services
{"consul":[],"httpd":[], "nginx":[]}

在这里插入图片描述
2、安装consul-template

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

3、准备template nginx模板文件

安装consul-template 软件包
consul(192.168.118.18)

在这里插入图片描述

//在consul服务器上操作




vim /root/consul/nginx.ctmpl

upstream http_backend {
  {{range service "nginx"}}
  server {{.Address}}:{{.Port}};
  {{end}}
}

server {
  listen 83;         指定监听的consul端口
  server_name localhost 192.168.118.18;       监听本地代理服务器地址
  access_log /var/log/nginx/nginx01-access.log;   监听本地代理服务器地址
  index index.html index.php;      首页文件名
  location / {                     配置反向代理
  proxy_set_header HOST $host;     指定客户端请求的host主机ip
  proxy_set_header X-Real-IP $remote_addr;    获取请求端的真实IP
  proxy_set_header Client-IP $remote_addr;    指定客户端的真实IP
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    使后端服务器获取客户机的真实IP
  proxy_pass http://http_backend;    跳转的服务器,就是我们上面配置的池
  }
}
--------------------------------------------------------------------------------------------- 
vim /root/consul/nginx.ctmpl

upstream http_backend {
  {{range service "nginx"}}
   server {{.Address}}:{{.Port}};
  {{end}}
}

server {
  listen 83;
  server_name localhost 192.168.118.18;
  access_log /var/log/nginx/gcc.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;
}
}

在这里插入图片描述
4、编译安装nginx(在consul上操作)

yum install gcc pcre-devel zlib-devel -y

tar zxvf nginx-1.12.0.tar.gz -C /opt

cd //opt/nginx-1.12.0
./configure --prefix=/usr/local/nginx

make && make install

5、配置nginx (在consul上操作)

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

在这里插入图片描述
在这里插入图片描述

6、配置并启动 template(在consul上操作)

cd /root

#上传consul-template_0.19.3_linux_amd64.zip包到/root目录下
unzip consul-template_0.19.3_linux_amd64.zip

mv consul-template /usr/bin/

consul-template -consul-addr 192.168.118.18:8500 -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/test.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info

//另外打开-个终端查看生成配置文件
[root@localhost vhost]#cat /usr/local/nginx/conf/vhost/test.conf
upstream http_backend {
  
   server 192.168.118.17:83;
  
   server 192.168.118.17:84;
  
}

server {
  listen 83;
  server_name localhost 192.168.118.18;
  access_log /var/log/nginx/gcc.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;
}
}

在这里插入图片描述
在这里插入图片描述

(三)增加一个nginx容器节点

#增加一个nginx容器节点,测试服务发现及配置更新功能
//在registrator(192.168.118.17)服务端注册
docker run -itd -p:85:80 --name test-05 -h test05 nginx

//在consul服务器监控装填会有提示自动更新
2021/03/26 05:51:59.624677 [INFO] (child) spawning: /usr/local/nginx/sbin/nginx -s reload
2021/03/26 05:53:09.576538 [INFO] (runner) initiating run
2021/03/26 05:53:09.577487 [INFO] (runner) rendered "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/test.conf"
2021/03/26 05:53:09.577505 [INFO] (runner) executing command "/usr/local/nginx/sbin/nginx -s reload" from "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/test.conf"
2021/03/26 05:53:09.577534 [INFO] (child) spawning: /usr/local/nginx/sbin/nginx -s reload


//registrator(192.168.118.17查看三台nginx容器日志,请求正常轮询到各个容器节点上
docker logs -f test-01
docker logs -f test-02
docker logs -f test-05

consul(192.168.168.17)
在consul服务器监控装填会有提示自动更新 并且查看 已经多了一个端口
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

(四)consul多节点配置

//添加一台已有docker环境的服务器192.168.200.30/24加入已有的群集中

consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/ib/consul-data \
-bind=192.168.118.16 \
-client=0.0.0.0 \
-node=consul-server02 \
-enable-script-checks=true \
-datacenter=dc1 \
-join 192.168.118.18 &> /var/log/consul.log &


-enable-script-checks=true: 设置检查服务为可用
-datacenter: 数据中心名称
-join: 加入到已有的集群中

配置完成之后,可以在Consul服务器上consul members查看一下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值