Consul Template的使用


1安装

地址 https://github.com/hashicorp/consul-template/releases

wget https://releases.hashicorp.com/consul-template/0.14.0/consul-template_0.14.0_linux_amd64.zip

unzip consul-template_0.14.0_linux_amd64.zip cp consul-template /usr/local/bin

2 执行命令前 请确定已经安装consul 并且创建好了集群

3 创建模板

tmpltest.ctmpl

内容

{{range services}}
{{.Name}}
{{range .Tags}}
{{.}}{{end}}
{{end}}

4 执行

E:\consul321>consul-template.exe -consul 192.168.5.156:8500 -template "./tmpl/tmpltest.ctmpl:./tmpl/result"

命令说明:
-consul后是consul的webui接口 ,用web管理consul就用的8500端口。

-template 后面是模板参数 第一个是模板地址 。冒号后的第二个参数是输出位置。

结果:

consul

sonarqube

dev

说明:consul 是系统自带的服务 sonarqube 是我创建的服务

该服务的配置文件sonarqube.json 内容如下

{
"service": {
"name": "sonarqube",
"tags": ["dev"],
"address":"www.163.com",
"port": 80,
"checks":[
{
"http":"http://www.163.com",
"interval":"5s"
}
]
}
}

其他

命令的其他参数和说明

-template 的参数 除了输入输出参数 还可以添加其他命令 如

E:\consul321>consul-template.exe -consul 192.168.5.156:8500 -template "./tmpl/tmpltest.ctmpl:./tmpl/result:service nginx restart"

表示输出后 重启nginx服务

-config 模板配置文件的路径

-dry 模板内容不写入磁盘,写到控制台

-log-level 日志级别 通常是info warn之类

-max-stale 默认1秒,设置后,consul会把任务分发给各个server,而不是有leader独自完成。

-once 运行一次后退出

-pid-file 写模板文件的pid的信息保存的路径

-ssl 和consul使用ssl通信 相关的有ssl-ca-cert ssl-cert ssl-verify

-token consul的api token。没有默认值

-version 版本

除了consul和template 其他参数都是可选的

参看https://github.com/hashicorp/consul-template#examples

再来个例子
$ consul-template \
-consul 127.0.0.1:8500 \
-template "/tmp/template.ctmpl:/var/www/nginx.conf:service nginx restart" \
-retry 30s \
-once
表示如果consul有问题的话,每30秒轮询一次。

来个证书的命令的例子

$ consul-template \
-consul 127.0.0.1:8543 \
-ssl \
-ssl-cert /path/to/client/cert.pem \
-ssl-ca-cert /path/to/ca/cert.pem \
-template "/tmp/template.ctmpl:/tmp/result" \
-dry \
-once

模板的配置文件

例如 创建一个tmpl.json文件
内容 如下

consul = "127.0.0.1:8500"

template {

source = "/etc/haproxy/haproxy.ctmpl"
destination = "/etc/haproxy/haproxy.cfg"
command = "service haproxy restart"

}
详细的参数可以看这里 https://github.com/hashicorp/consul-template#examples

接下来 我们就可以这样执行了
consul-template -config /data/cfg/consul/tmpl.json

如果有多个模板要执行的话,可以这样,配多个template参数就行了

consul-template \
-consul my.consul.internal:6124 \
-template "/tmp/nginx.ctmpl:/var/nginx/nginx.conf:service nginx restart" \
-template "/tmp/redis.ctmpl:/var/redis/redis.conf:service redis restart" \
-template "/tmp/haproxy.ctmpl:/var/haproxy/haproxy.conf"

Consul Template的简单使用的更多相关文章

  1. 在Windows环境中使用Nginx, Consul, Consul Template搭建负载均衡和服务发现服务

    搭建负载均衡和服务发现服务的目的 随着网站业务的不断提升,单个服务器的性能越来越难满足客户的业务需求,所以很多情况下,需要使用多服务器实例和负载均衡器来满足业务需要. Nginx 什么是Nginx N ...

  2. 初识服务发现及Consul框架的简单使用

    初识服务发现及Consul框架的简单使用   1.什么是服务发现? 服务发现组件记录了(大规模)分布式系统中所有服务的信息,人们或者其它服务可以据此找到这些服务. DNS 就是一个简单的例子. 当然, ...

  3. 庐山真面目之四微服务架构Consul和Ocelot简单版本实现

    庐山真面目之四微服务架构Consul和Ocelot简单版本实现 一.简介      在上一篇文章<庐山真面目之三微服务架构Consul简单版本实现>中,我们已经探讨了如何搭建基于Consu ...

  4. jQuery.template.js 简单使用

    之前看了一篇文章<我们为什么要尝试前后端分离>,深有同感,并有了下面的评论: 我最近也和前端同事在讨论这个问题,比如有时候前端写好页面给后端了,然后后端把这些页面拆分成很多的 views, ...

  5. C++中template的简单用法

    模板(Template)指C++程序设计设计语言中采用类型作为参数的程序设计,支持通用程序设计.C++ 的标准库提供许多有用的函数大多结合了模板的观念,如STL以及IO Stream.使用模板可以使用 ...

  6. template.js简单入门

    template.js是一款开源的JavaScript模板引擎,用来渲染页面的. github地址 https://github.com/yanhaijing/template.js 下载templa ...

  7. 关于string.Template的简单介绍

    一.简介 string模块定义了一种新字符串类型Template,它简化了特定的字符串置换操作. 何谓“简化”?我们可以先想一下我们之前比较常用的有关字符串的“置换”操作有哪些:一种是利用%操作符实现 ...

  8. 使用nomad && consul && fabio 创建简单的微服务系统

    具体每个组件的功能就不详细说明了 nomad 一个调度工具,consul 一个服务发现,健康检查多数据中心支持的工具 fabio 一个基于consul的负载均衡&&动态路由工具,对于集 ...

  9. Centos 7 Docker、docker-compose、Registrator、Consul、Consul Template和Nginx实现高可扩展的Web框架

    安装所需软件 Docker Docker-compose 配置docker-compose.yml文件内容如下: #load balancer will automatically update th ...

随机推荐

  1. 一封给JVM懵懂者的情书【不看错过一生幸福】

    别说你懂我 你只是在意Java你把我留在家里身和心却始终在她那里难道我只是她的附属品?错,我是我,我是JVM,没有我就没有他! 如果你想懂我或者不管你是否懂我我都在这里等你---[深入JVM内核—原理 ...

  2. jsp\struts1.2\struts2 中文件上传(转)

    jsp\struts1.2\struts2 中文件上传 a.在jsp中简单利用Commons-fileupload组件实现 b.在struts1.2中实现c.在sturts2中实现现在把Code与大家 ...

  3. scp使用加密算法报错unknown cipher type

    为了提高scp的传输速度指定了scp的加密算法为arcfour $ scp -c arcfour localFile userName@remoteIP:remoteFile 得到报错unknown ...

  4. 移动端性能优化动态加载JS、CSS

    JS CODE (function() { /** * update: * 1.0 */ var version = "insure 1.1.0"; var Zepto = Zep ...

  5. laravel redis

    安装配置redis服务器 $ wget http://download.redis.io/releases/redis-3.0.5.tar.gz $ tar xzf redis-.tar.gz $ c ...

  6. 3D touch的 使用心得

    一.设置图标touch 快捷进入 1.静态标签 静态标签是我们在项目的配置plist文件中配置的标签,在用户安装程序后就可以使用,并且排序会在动态标签的前面. 我们先来看静态标签的配置: 首先,在in ...

  7. jQuery extend 实现代码封装

    jQuery 有两种方式封装代码 $.extend 和 $.fn.extend,我们也称为封装插件 $.extend DEMO // 封装 $.extend({ say:function(option ...

  8. 【转】OpenStack奥斯汀峰会Keynotes国内抢先看

    http://www.openstack.cn/?p=5341 OpenStack奥斯汀峰会Keynotes国内抢先看入口:http://www.tudou.com/home/_903780397/i ...

  9. LeetCode House Robber

    原题链接在这里:https://leetcode.com/problems/house-robber/ 题目: You are a professional robber planning to ro ...

  10. javascript中的真假值、数据类型判断以及+的特殊用法

    一.javascript中的假值 jQuery中拥有一组数量奇大的假值,包括 0,NaN(非数),''(空字符串),false,null,undefined 这些值在if判断中全部等于假,但这些值彼此 ...