Docker容器如何部署consul(consul,docker,开发技术)

时间:2024-05-02 18:56:32 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

Consul简介

Consul 是一个支持多数据中心分布式高可用的 服务发现 和 配置共享 的服务软件,由 HashiCorp 公司用 Go 语言开发, 基于 Mozilla Public License 2.0 的协议进行开源。 Consul 支持 健康检查,并允许 HTTP 、GRPC 和 DNS 协议调用 API 存储键值对.
命令行超级好用的虚拟机管理软件 vgrant 也是 HashiCorp 公司开发的产品.
一致性协议采用 Raft 算法,用来保证服务的高可用. 使用 GOSSIP 协议管理成员和广播消息, 并且支持 ACL 访问控制.

Consul 使用场景

  • Docker 实例的注册与配置共享

  • Coreos 实例的注册与配置共享

  • SaaS 应用的配置共享、服务发现和健康检查。

  • vitess 集群

  • 与 confd 服务集成,动态生成 nginx 和 haproxy 配置文件

-consul部署-

Docker容器如何部署consul

服务器:192.168.37.100Docker-ce、Consul、Consul-template
服务器:192.168.37.107Docker-ce、registrator
template模板(更新配置文件)
registrator(自动发现+注册到consul-server端)
后端每更新出一个容器,会向registrator进行注册,控制consul完成更新操作,consul会触发consultemplate模板进行热更新(reload)
核心机制: consul :自动发现、自动更新,为容器提供服务(添加、删除、生命周期)—------consul服务器

创建consul目录

[root@localhost~]#hostnamectlset-hostnameconsul[root@localhost~]#su[root@consul~]#mkdir/root/consul

将consul压缩包压缩并放到/use/bin下

[root@consul~]#cdconsul/[root@consul~/consul]#lsconsul_0.9.2_linux_amd64.zip[root@consul~/consul]#unzipconsul_0.9.2_linux_amd64.zipArchive:consul_0.9.2_linux_amd64.zipinflating:consul[root@consul~/consul]#lsconsulconsul_0.9.2_linux_amd64.zip[root@consul~/consul]#mvconsul/usr/bin/

配置consul的模式

onsul agent \
-server \ server模式
-bootstrap 前端框架
-ui \ 可被访问的web界面
-data-dir=/var/lib/consul-data \
-bind=192.168.37.100 \
-client=0.0.0.0 \ registrator(应用服务器-docker-tomcat/apache)
-node=consul-server01 &> /var/ log / consul.log &

[root@consul~/consul]#consulagent\>-server\>-bootstrap\>-ui\>-data-dir=/var/lib/consul-data\>-bind=192.168.37.100\>-client=0.0.0.0\>-node=consul-server01&>/var/log/consul.log&[1]24144[root@consul~/consul]#consulmembersNodeAddressStatusTypeBuildProtocolDCconsul-server01192.168.37.100:8301aliveserver0.9.22dc1

此时我们看群集信息

[root@consul~/consul]#consulmembersNodeAddressStatusTypeBuildProtocolDCconsul-server01192.168.37.100:8301aliveserver0.9.22dc1

查看详细信息,过滤出leader

[root@consul~/consul]#consulinfo|grepleaderleader=trueleader_addr=192.168.37.100:8300

查看consul界面。使用8500端口,8300是通信端口

Docker容器如何部署consul

容器服务自动加入consul集群

安装Gliderlabs/Registrator Gliderlabs/Registrator

可检查容器运行状态自动注册,还可注册docker容器的服务信息到服务配置中心(consul 8300 -----》8500展示)。目前支持Consul、Etcd和skyDNS2。

在192.168.37.107执行以下操作

[root@server~]#dockerrun-d\>--name=registrator\>--net=host\>-v/var/run/docker.sock:/tmp/docker.sock\>--restart=always\>gliderlabs/registrator:latest\>-ip=192.168.37.107\>consul://192.168.37.100:8500###下面的是加载Unabletofindimage'gliderlabs/registrator:latest'locallylatest:Pullingfromgliderlabs/registratorImagedocker.io/gliderlabs/registrator:latestusesoutdatedschema1manifestformat.Pleaseupgradetoaschema2imageforbetterfuturecompatibility.Moreinformationathttps://docs.docker.com/registry/spec/deprecated-schema-v1/c87f684ee1c2:Pullcompletea0559c0b3676:Pullcompletea28552c49839:PullcompleteDigest:sha256:6e708681dd52e28f4f39d048ac75376c9a762c44b3d75b2824173f8364e52c10Status:Downloadednewerimageforgliderlabs/registrator:latest2536df1b1fbb77dcbf5d2e09295f222496ecc63f00a49020ce6976decd52a439

查看容器

[root@server~]#dockerps-aCONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES2536df1b1fbbgliderlabs/registrator:latest"/bin/registrator-i…"51secondsagoUp50secondsregistrator
测试服务发现功能是否正常

安装两个nginx和2个apache

dockerrun-itd-p:83:80--nametest-01-htest01nginxdockerrun-itd-p:84:80--nametest-02-htesto2nginxdockerrun-itd-p:88:80--nametest-03-htest03httpddockerrun-itd-p:89:80--nametest-04-htest04httpd

Docker容器如何部署consul

此时去网页查看

Docker容器如何部署consul

安装consul-template

Consul-Template 是一个守护进程,用于实时查询consul集群信息,并更新文件系统上任意数量的指定模板,生成配置文件。更新完成以后,可以选择运行shell命令执行更新操作,重新加载Nginx。Consul-Template可以查询consul中的服务目录、Key、 Key-values 等。

这种强大的抽象功能和查询语言模板可以使Consul-Template 特别适合动态的创建配置文件。例如:创建 Apache/Nginx (nginx upstream) Proxy Balancers、Haproxy Backends

准备template nginx模板文件

--------------------------------------------------在consul上操作----------------------------------------------------

[root@consul~/consul]#vimnginx.ctmp1upstreamhttp_backend{{{rangeservice"nginx"}}server{{.Address}}:{{.Port}};{{end}}}server{listen83;server_namelocalhost192.168.37.100;access_log/var/log/nginx/xiao.cn-access.log;indexindex.htmlindex.php;location/{proxy_set_headerHOST$host;proxy_set_headerx-Real-IP$remote_addr;proxy_set_headerclient-IP$remote_addr;proxy_set_headerx-Forwarded-For$proxy_add_x_forwarded_for;proxy_passhttp://http_backend;}
安装nginx
####安装所需软件包[root@server~]#yuminstallgccpcre-develzlib-devel-y###解压nginx软件包[root@server~]#tarzxfnginx-1.12.0.tar.gz###执行[root@server/opt/nginx-1.12.0]#./configure--prefix=/usr/local/nginx##编译[root@server/opt/nginx-1.12.0]#make&&makeinstall

修改配置文件,让其识别.conf文件结尾的文件

Docker容器如何部署consul

创建vhost目录

[root@server/usr/local/nginx/conf]#mkdirvhost

创建nginx日志目录

[root@server/usr/local/nginx/conf/vhost]#mkdir/var/log/nginx.log

启动nginx

[root@server/usr/local/sbin]#ln-s/usr/local/nginx/sbin/nginx/usr/local/sbin/[root@server/usr/local/sbin]#nginx

查看是否启动

[root@server/usr/local/sbin]#netstat-natp|grepnginxtcp000.0.0.0:800.0.0.0:*LISTEN54739/nginx:master
配置并启动template
[root@consul~/consul]#unzipconsul-template_0.19.3_linux_amd64.zipArchive:consul-template_0.19.3_linux_amd64.zipinflating:consul-template[root@consul~/consul]#lsconsul_0.9.2_linux_amd64.zipconsul-templateconsul-template_0.19.3_linux_amd64.zipnginx.ctmp1[root@consul~/consul]#mvconsul-template/usr/bin

##关联nginx虚拟目录中的子配置文件操作

[root@consul~/consul]#consul-template-consul-addr192.168.37.100:8500\>-template"/root/consul/nginx.ctmp1:/usr/local/nginx/conf/vhost/xiao.conf:/usr/local/nginx/sbin/nginx-sreload"\>--log-level=info

查看生成配置文件

Docker容器如何部署consul

是都可以访问。

Docker容器如何部署consul

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Docker容器如何部署consul的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Qt如何实现实时鼠标绘制图形下一篇:

5 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18