欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Dockerconsul部署

时间:2023-07-20

目录

-consul部署- 

​         容器服务自动加入consul集群

                安装Gliderlabs/Registrator Gliderlabs/Registrator

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

                 安装consul-template

                安装nginx

                  配置并启动template


-consul部署- 

 

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

创建consul目录

[root@localhost ~]# hostnamectl set-hostname consul[root@localhost ~]# su[root@consul ~]# mkdir /root/consul

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

[root@consul ~]# cd consul/[root@consul ~/consul]# lsconsul_0.9.2_linux_amd64.zip[root@consul ~/consul]# unzip consul_0.9.2_linux_amd64.zipArchive: consul_0.9.2_linux_amd64.zip inflating: consul[root@consul ~/consul]# lsconsul consul_0.9.2_linux_amd64.zip[root@consul ~/consul]# mv consul /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]# consul agent > -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]# consul membersNode Address Status Type Build Protocol DCconsul-server01 192.168.37.100:8301 alive server 0.9.2 2 dc1

此时我们看群集信息

[root@consul ~/consul]# consul membersNode Address Status Type Build Protocol DCconsul-server01 192.168.37.100:8301 alive server 0.9.2 2 dc1

查看详细信息,过滤出leader

[root@consul ~/consul]# consul info | grep leader leader = true leader_addr = 192.168.37.100:8300

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

         容器服务自动加入consul集群

                安装Gliderlabs/Registrator Gliderlabs/Registrator

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

在192.168.37.107执行以下操作

[root@server ~]# docker run -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###下面的是加载Unable to find image 'gliderlabs/registrator:latest' locallylatest: Pulling from gliderlabs/registratorImage docker.io/gliderlabs/registrator:latest uses outdated schema1 manifest format、Please upgrade to a sc hema2 image for better future compatibility、More information at https://docs.docker.com/registry/spec/depr ecated-schema-v1/c87f684ee1c2: Pull completea0559c0b3676: Pull completea28552c49839: Pull completeDigest: sha256:6e708681dd52e28f4f39d048ac75376c9a762c44b3d75b2824173f8364e52c10Status: Downloaded newer image for gliderlabs/registrator:latest2536df1b1fbb77dcbf5d2e09295f222496ecc63f00a49020ce6976decd52a439

查看容器

[root@server ~]# docker ps -aConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES2536df1b1fbb gliderlabs/registrator:latest "/bin/registrator -i…" 51 seconds ago Up 50 seconds registrator

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

安装两个nginx和2个apache

docker run -itd -p:83:80 --name test-01 -h test01 nginxdocker run -itd -p:84:80 --name test-02 -h testo2 nginxdocker run -itd -p:88:80 --name test-03 -h test03 httpddocker run -itd -p:89:80 --name test-04 -h test04 httpd

此时去网页查看

 

                 安装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]# vim nginx.ctmp1upstream http_backend { {{range service "nginx"}} server {{.Address}}:{{.Port}}; {{end}}}server { listen 83; server_name localhost 192.168.37.100; access_log /var/log/nginx/xiao.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

####安装所需软件包[root@server ~]# yum install gcc pcre-devel zlib-devel -y###解压nginx软件包[root@server ~]# tar zxf nginx-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 && make install

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

 创建vhost目录

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

创建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 |grep nginxtcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 54739/nginx: master

                  配置并启动template

[root@consul ~/consul]# unzip consul-template_0.19.3_linux_amd64.zipArchive: consul-template_0.19.3_linux_amd64.zip inflating: consul-template[root@consul ~/consul]# lsconsul_0.9.2_linux_amd64.zip consul-template consul-template_0.19.3_linux_amd64.zip nginx.ctmp1[root@consul ~/consul]# mv consul-template /usr/bin

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

[root@consul ~/consul]# consul-template -consul-addr 192.168.37.100:8500 > -template "/root/consul/nginx.ctmp1:/usr/local/nginx/conf/vhost/xiao.conf:/usr/local/nginx/sbin/nginx -s reload" > --log-level=info

查看生成配置文件 

是都可以访问。 

 

 

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。