Docker---consul添加compose-temlate自动发现功能

Docker—consul添加compose-temlate自动发现功能

一.什么是compose-temlate?

1.compose-template是一个守护进程,用于实时查询consul集群信息,并更新文件系统上任意数量的指定模板,生成配置文件。更新完成以后,可以选择送行shell命令执行更新操作,重新加载Nginx.
2.Consul-Template可以查询consul中的服务目录、key、key-values等
3.这种强大的抽象功能和查询语言模板使得consul-template特别适合动态的创建配置文件,也就是自动化增加服务

二.具体安装步骤

1.部署consul群集
在前一篇博客中有具体详细步骤
2.准备template nginx模板文件(在consul上操作)
[root@localhost ~]# cd consul/
[root@localhost consul]# ls
consul_0.9.2_linux_amd64.zip
[root@localhost consul]# vim nginx.ctmpl

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

server {
  listen 1216;
  server_name localhost 192.168.88.137;
  access_log /var/log/nginx/kgc.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;
  }
}

在这里插入图片描述

3.在consul服务器上安装nginx
###### 准备nginx-1.12.2软件包

###### 安装编译:
[root@localhost ~]# yum install -y gcc pcre-devel zlib-devel
[root@localhost ~]# tar xzvf nginx-1.12.2.tar.gz -C /opt
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx
[root@localhost nginx-1.12.2]# make && make install
4.配置nginx:
[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf

http {
    include       mime.types;
    include     vhost/*.conf;              //添加虚拟主机文件
    default_type  application/octet-stream;
5.创建虚拟主机目录和日志文件目录
[root@localhost nginx-1.12.2]# mkdir /usr/local/nginx/conf/vhost
[root@localhost nginx-1.12.2]# mkdir /var/log/nginx
6.启动nginx
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost ~]# /usr/local/nginx/sbin/nginx
7.配置并启用template
###### 准备template软件包,并解压

[root@localhost ~]# unzip consul-template_0.19.3_linux_amd64.zip 
Archive:  consul-template_0.19.3_linux_amd64.zip
  inflating: consul-template         
[root@localhost ~]# mv consul-template /usr/bin/
8.启用模板,进入监控状态
[root@localhost ~]# consul-template -consul-addr 192.168.88.137:8500 -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info
// /root/consul/nginx.ctmpl,模板文件
// /usr/local/nginx/conf/vhost/kgc.conf,生成的配置文件
// /usr/local/nginx/sbin/nginx -s reload,重载服务
//-log-level=info,指定日志级别
9.打开另一个终端查看生成的配置文件
[root@localhost ~]# cd /usr/local/nginx/conf/vhost/
[root@localhost vhost]# ls
kgc.conf
[root@localhost vhost]# vim kgc.conf 

upstream http_backend {

  server 192.168.88.138:83;

  server 192.168.88.138:84;

}

server {
  listen 1216;
  server_name localhost 192.168.88.137;
  access_log /var/log/nginx/kgc.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;
  }
}
10.使用浏览器查看后端服务器

在这里插入图片描述

11.进入docker-nginx的服务器中查看容器的日志
[root@localhost ~]# docker logs -f test-01

[root@localhost ~]# docker logs -f test-02
12.添加一个nginx节点验证自动更新
在registrator服务器注册
[root@localhost ~]# docker run -itd -p:85:80 --name test-05 -h test05 nginx
b29ba3cb2898e6fc61215ecbb0be6b27722ae3c0c78aeef7a7e5411112e21f2a
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS              PORTS                NAMES
b29ba3cb2898        nginx                           "nginx -g 'daemon of…"   36 seconds ago      Up 35 seconds       0.0.0.0:85->80/tcp   test-05
13.在consul服务器上查看虚拟主机文件

在这里插入图片描述

在这里插入图片描述

总结:

通过consul的一个实时学习,compose-template可以实现动态增加服务节点到nginx代理的配置文件,实现自动化功能。

注意:

实验过程中,防火墙可能会影响到nginx网页的访问,需要关闭,但是在创建容器的时候防火墙得打开
原创文章 84 获赞 95 访问量 5886

猜你喜欢

转载自blog.csdn.net/obsessiveY/article/details/105781707