服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - 服务器知识 - Docker容器Consul部署概述

Docker容器Consul部署概述

2022-11-11 17:07羽翔青空 服务器知识

这篇文章主要介绍了Docker容器Consul部署概述,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下

一、Consul概述

template 模板(更新)
registrator(自动发现)
后端每构建出一个容器,会向registrator进行注册,控制consul 完成更新操作,consul会触发consul template模板进行热更新
核心机制: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集群

Docker容器Consul部署概述

建立Consul服务

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

Consul agent有两种运行模式
Server
Client

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

(1)server部署

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

Docker容器Consul部署概述

consul agent 
-server 
-bootstrap 
-ui 
-data-dir=/var/lib/consul-data 
-bind=192.168.109.11 
-client=0.0.0.0 
-node=consul-server01 &> /var/log/consul.log &

consul members
consul info | grep leader

Docker容器Consul部署概述

 

(2)client部署

容器服务自动加入nginx集群:

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

在192.168.184.12节点上,执行以下操作:

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

Docker容器Consul部署概述

Docker容器Consul部署概述

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

Docker容器Consul部署概述

Docker容器Consul部署概述

Docker容器Consul部署概述

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

Docker容器Consul部署概述

Docker容器Consul部署概述

 

(3)配置template模板自动更新

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.109.11;
 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;
  }
}

Docker容器Consul部署概述

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

Docker容器Consul部署概述

Docker容器Consul部署概述

Docker容器Consul部署概述

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

Docker容器Consul部署概述

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

Docker容器Consul部署概述

Docker容器Consul部署概述

mv consul-template /usr/bin
consul-template -consul-addr 192.168.109.11:8500 -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/lic.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info

Docker容器Consul部署概述

再打开另一个终端

Docker容器Consul部署概述

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

Docker容器Consul部署概述

Docker容器Consul部署概述

 

(4)测试访问代理服务器

http://192.168.184.11:100/

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

Docker容器Consul部署概述

到此这篇关于Docker容器Consul部署概述的文章就介绍到这了,更多相关Docker Consul部署内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文地址:https://blog.csdn.net/qq1356059950/article/details/126145773

延伸 · 阅读

精彩推荐