栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Docker consul 部署

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Docker consul 部署

目录

-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]# ls
consul_0.9.2_linux_amd64.zip
[root@consul ~/consul]# unzip consul_0.9.2_linux_amd64.zip
Archive:  consul_0.9.2_linux_amd64.zip
  inflating: consul
[root@consul ~/consul]# ls
consul  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 members
Node             Address              Status  Type    Build  Protocol  DC
consul-server01  192.168.37.100:8301  alive   server  0.9.2  2         dc1

此时我们看群集信息

[root@consul ~/consul]# consul members
Node             Address              Status  Type    Build  Protocol  DC
consul-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' locally
latest: Pulling from gliderlabs/registrator
Image 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 complete
a0559c0b3676: Pull complete
a28552c49839: Pull complete
Digest: sha256:6e708681dd52e28f4f39d048ac75376c9a762c44b3d75b2824173f8364e52c10
Status: Downloaded newer image for gliderlabs/registrator:latest
2536df1b1fbb77dcbf5d2e09295f222496ecc63f00a49020ce6976decd52a439

查看容器

[root@server ~]# docker ps -a
ConTAINER ID   IMAGE                           COMMAND                  CREATED          STATUS          PORTS     NAMES
2536df1b1fbb   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 nginx
docker run -itd -p:84:80 --name test-02 -h testo2 nginx
docker run -itd -p:88:80 --name test-03 -h test03 httpd
docker 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.ctmp1


upstream 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 nginx
tcp        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.zip
Archive:  consul-template_0.19.3_linux_amd64.zip
  inflating: consul-template
[root@consul ~/consul]# ls
consul_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

查看生成配置文件 

是都可以访问。 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/729723.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号