Docker容器--Consul部署


引言

Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置
与Docker等轻量级容器可无缝配合

一、Consul概述

template 模板(更新)
registrator(自动发现)
后端每构建出一个容器,会向registrator进行注册,控制consul 完成更新操作,consul会触发consul template模板进行热更新
核心机制:consul :自动发现、自动更新,为容器提供服务(添加、删除、生命周期)

二、Consul的特性

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

三、Consul的使用场景

Consul的应用场景包括服务发现、服务隔离、服务配置:

  • 1 .服务发现场景中consul作为注册中心,服务地址被注册到consul中以后,可以使用consul提供的dns、http接口查询,consul支持health check。
  • 2.服务隔离场景中consul支持以服务为单位设置访问策略,能同时支持经典的平台和新兴的平台,支持tls证书分发,service-to-service加密。
  • 3.服务配置场景中consul提供key-value数据存储功能,并且能将变动迅速地通知出去,借助Consul可以实现配置共享,需要读取配置的服务可以从Consul中读取到准确的配置信息。
  • 4.Consul可以帮助系统管理者更清晰的了解复杂系统内部的系统架构,运维人员可以将Consul看成一种监控软件,也可以看成一种资产(资源)管理系统。

四、搭建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

1、部署consul服务器,server部署(192.168.28.200)

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 &


在这里插入图片描述

2.查看集群信息

consul members #插卡集群成员
consul info | grep leader #查看leader信息

在这里插入图片描述

查看集群server成员curl 127.0.0.1:8500/v1/status/peers
集群Raf leadercurl 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

3.安装Gliderlabs/Registrator

[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

在这里插入图片描述

4.测试服务发现功能是否正常

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


在这里插入图片描述

在这里插入图片描述

5.准备 template 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; #反向代理调用转发池
  }
}


在这里插入图片描述

6.配置nginx

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

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

7.关联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;
  }
}


在这里插入图片描述

在这里插入图片描述

8.测试:删除和增加nginx容器节点

增加

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

   

在这里插入图片描述
减少前
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值