Docker------Consul 环境搭建

目录

前言​

一、Docker consul 概述

二、Consul的特性

三、Consul的使用场景

四、搭建consul 集群

1、项目需求

2、环境准备

3、部署步骤

4、client部署(192.168.58.20)

5、配置template模板自动更新

 4、测试访问代理服务器

5、consul 多节点配置 


前言

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

一、Docker consul 概述

template 模板(更新):先发现再更新,发现的是后端节点上容器的变化(registrator),更新的是nginx配置文件(agent)

registrator(自动发现):是consul安插在docker容器里的眼线,用于监听监控节点上容器的变化(增加或减少,或者宕机),一旦有变化会把这些信息告诉并注册在consul server端(使用回调和协程的方式,所以它的延迟和资源消耗会很少),consul server发生一旦发生注册列表的变化后,会把注册的信息告诉agent。

后端每构建出一个容器,会向registrator进行注册,控制consul 完成更新操作,consul会触发consul template模板进行热更新

agent(代理):用来控制consul template模板,用template组件去和nginx.conf来进行对接,模板里全是变量,用变量的方式去加载后端由注册到consul server端之后,server端会把信息告诉agent,agent和template进行对接,写入template,template就有了镜像,更新完之后会作为nginx.conf子配置文件被前端的nginx识别,consul agent会控制reload之后会识别nginx.conf配置文件中的变化,相当于识别后端的节点,就可以在地址池中动态调整自己后端资源。

核心机制:consul :自动发现、自动更新,为容器提供服务(添加、删除、生命周期)

二、Consul的特性

支持健康检查、允许存储键值对

基于Golong语言,可移植性强

支持ACL访问控制

三、Consul的使用场景

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

服务发现场景中consul作为注册中心,服务地址被注册到consul中以后,可以使用consul提供的dns、http接口查询,consul支持health check。

服务隔离场景中consul支持以服务为单位设置访问策略,能同时支持经典的平台和新兴的平台,支持tls证书分发,service-to-service加密。

服务配置场景中consul提供key-value数据存储功能,并且能将变动迅速地通知出去,借助Consul可以实现配置共享,需要读取配置的服务可以从Consul中读取到准确的配置信息。

Consul可以帮助系统管理者更清晰的了解复杂系统内部的系统架构,运维人员可以将Consul看成一种监控软件,也可以看成一种资产(资源)管理系统。

四、搭建consul 集群

建立Consul服务

每个提供服务的节点上都要部署和运行Consul的agent

Consul agent有两种运行模式
Server
Client

Server和Client只是Consul集群层面的区分,与搭建在Cluster之上的应用服务无关

1、项目需求

使用 Docker 将 Consul、Consul template、Registrator 与 Nginx 组成一个值得新人且可扩展的服务架构

添加或移除服务时,不需要重写任何配置,也不需要重启任何服务,一切都能够正常运行,以实现自动化运维

2、环境准备

主机 IP 地址 需要安装的软件
主节点 192.168.58.19 docker-ce、consul、consul-template、nginx
nginx 192.168.58.20 doker-ce

3、部署步骤

#两台节点上都安装 Docker-ce,记得关闭防火墙
systemctl stop firewalld && systemctl disable firewalld
setenforce 0 && sed -i "s/SELINUX=*/SELINUX=disabled/g" /etc/selinux/config

在主节点上部署consul 
mkdir /root/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 \
-data-dir=/var/lib/consul-data \
-bind=192.168.58.19 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &

consul members
consul info | grep 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

4、client部署(192.168.58.20)

容器服务自动加入nginx集群
1、安装Gliderlabs/Registrator Gliderlabs/Registrator
可检查容器运行状态自动注册,还可注销docker容器的服务 到服务配置中心
目前支持Consul、Etcd和SkyDNS2
 
在192.168.58.20节点上,执行以下操作
 
docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.58.20 \
consul://192.168.58.19:8500


systemctl restart docker
docker run -itd -p:81:80 --name test-01 -h test01 nginx
docker run -itd -p:82:80 --name test-02 -h test02 nginx
docker run -itd -p:83:80 --name test-03 -h test03 httpd
docker run -itd -p:84:80 --name test-04 -h test04 httpd

 真机访问http://192.168.184.11:8500
此时应该可以发现5个服务

5、配置template模板自动更新

 server(192.168.58.19)

Consul-Template是一个守护进程,用于实时查询Consul集群信息,并更新文件系统上任意数量的指定模板,生成配置文件,更新完成以后,可以查询Consul中的服务目录,Key、Key-values等。

cd consul/
vim nginx.ctmpl

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

server {
 listen 100;
 server_name localhost 192.168.58.19;
 access_log /var/log/nginx/lic.com-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;
  }     
}

yum -y install gcc pcre-devel zlib-devel
rz nginx-1.12.0.tar.gz
tar zxvf nginx-1.12.0.tar.gz -C /opt
cd /opt/nginx-1.12.10

./configure --prefix=/usr/local/nginx

make && make install

vim /usr/local/nginx/conf/nginx.conf
//19     include vhost/*.conf;

cd /usr/local/nginx/conf/
mkdir vhost
mkdir /var/log/nginx
 
/usr/local/nginx/sbin/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.58.19:8500 -template "/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/liy.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info

 192.168.58.20

docker run -itd -p:85:80 --name test-05 -h test05 nginx

 4、测试访问代理服务器

 是否可以完成代理访问轮询
192.168.58.19

http://192.168.58.19:100/

docker logs -f test-01
docker logs -f test-02
docker logs -f test-05

5、consul 多节点配置 

#添加一台已有docker环境的服务器加入已有的群集中:
 
consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/ib/consul-data \
-bind=192.168.58.19 \
-client=0.0.0.0 \
-node=consul-server02 \
-enable-script-checks=true \
-datacenter=dc1 \
-join 192.168.58.20 &> /var/log/consul.log &
 
 
--解释--
-enable-script-checks=true: 设置检查服务为可用
-datacenter: 数据中心名称
-join: 加入到已有的集群中
 

猜你喜欢

转载自blog.csdn.net/weixin_56270746/article/details/125915275