https://hub.docker.com/r/hashicorp/consul-template

https://github.com/hashicorp/consul-template

consultemplate

hashicorp/consul-template:latest

卷:

/etc/localtime:/etc/localtime:ro

/data/file:/data/file


Consul-Template简介

Consul-Template是基于Consul的自动替换配置文件的应用。在Consul-Template没出现之前,大家构建服务发现系统大多采用的是Zookeeper、Etcd+Confd这样类似的系统。

Consul官方推出了自己的模板系统Consul-Template后,动态的配置系统可以分化为Etcd+Confd和Consul+Consul-Template两大阵营。Consul-Template的定位和Confd差不多,Confd的后端可以是Etcd或者Consul。

Consul-Template提供了一个便捷的方式从Consul中获取存储的值,Consul-Template守护进程会查询Consul实例来更新系统上指定的任何模板。当更新完成后,模板还可以选择运行一些任意的命令。

Consul-Template的使用场景

Consul-Template可以查询Consul中的服务目录、Key、Key-values等。这种强大的抽象功能和查询语言模板可以使Consul-Template特别适合动态的创建配置文件。例如:创建Apache/Nginx Proxy Balancers、Haproxy Backends、Varnish Servers、Application Configurations等。

Consul-Template特性

  • Quiescence:Consul-Template内置静止平衡功能,可以智能的发现Consul实例中的更改信息。这个功能可以防止频繁的更新模板而引起系统的波动。
  • Dry Mode:不确定当前架构的状态,担心模板的变化会破坏子系统?无须担心。因为Consul-Template还有Dry模式。在Dry模式,Consul-Template会将结果呈现在STDOUT,所以操作员可以检查输出是否正常,以决定更换模板是否安全。
  • CLI and Config:Consul-Template同时支持命令行和配置文件。
  • Verbose Debugging:即使每件事你都做的近乎完美,但是有时候还是会有失败发生。Consul-Template可以提供更详细的Debug日志信息。

Consul-Template使用实例

在进行以下测试前,你首先得有一个Consul集群。如果你还没有,可参考「Consul集群部署」 一文搭建一个。当然单节点Consul环境也是可以的,如果你只想做一下简单的测试也可以参考「Consul入门」一文先快速搭建一个单节点环境。

命令行方式

  • 输出已注册服务的名称和Tags。

创建一个服务文件


$ vim  /opt/consul/conf/hi-linux.json
{"service": {"name": "hi-linux", "tags": ["web"], "port": 8080,
"check": {"http": "http://192.168.2.210:8080", "interval": "10s"}}}

通过consul reload进行热注册


$ consul reload --http-addr=192.168.2.210:8500
Configuration reload triggered

验证服务是否注册成功


$ curl http://192.168.2.210:8500/v1/catalog/service/hi-linux

[{"ID":"7d3b4fa3-b284-b761-f973-d856cd41ab7a","Node":"consul-server01","Address":"192.168.2.210","TaggedAddresses":{"lan":"192.168.2.210","wan":"192.168.2.210"},"NodeMeta":{},"ServiceID":"hi-linux","ServiceName":"hi-linux","ServiceTags":["web"],"ServiceAddress":"","ServicePort":8080,"ServiceEnableTagOverride":false,"CreateIndex":4985,"ModifyIndex":4985}]

创建模板文件


$ vim tmpltest.ctmpl

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

调用模板文件生成查询结果


$ consul-template -consul-addr 192.168.2.210:8500 -template "tmpltest.ctmpl:result" -once

命令说明:
-consul-addr:指定Consul的API接口 ,默认是8500端口。
-template:模板参数,第一个参数是模板文件位置,第二个参数是结果输出位置。
-once:只运行一次就退出。

查看模板渲染的结果


$ cat result

consul

hi-linux
web

输出结果说明:consul是系统自带的服务,hi-linux是刚才注册的服务,其Tags为web。

  • 根据已注册的服务动态生成Nginx配置文件

新建Nginx配置模板文件


$ vim nginx.conf.ctmpl

{{range services}} {{$name := .Name}} {{$service := service .Name}}
upstream {{$name}} {
 zone upstream-{{$name}} 64k;
 {{range $service}}server {{.Address}}:{{.Port}} max_fails=3 fail_timeout=60 weight=1;
 {{else}}server 127.0.0.1:65535; # force a 502{{end}}
} {{end}}

server {
 listen 80 default_server;

 location / {
   root /usr/share/nginx/html/;
   index index.html;
 }

 location /stub_status {
   stub_status;
 }

{{range services}} {{$name := .Name}}
 location /{{$name}} {
   proxy_pass http://{{$name}};
 }
{{end}}
}

调用模板文件生成Nginx配置文件


$ consul-template  -consul-addr 192.168.2.210:8500 -template="nginx.conf.ctmpl:default.conf" -once

# 渲染后的Nginx配置文件
$ cat default.conf

upstream consul {
 zone upstream-consul 64k;
 server 192.168.2.210:8300 max_fails=3 fail_timeout=60 weight=1;
 server 192.168.2.211:8300 max_fails=3 fail_timeout=60 weight=1;
 server 192.168.2.212:8300 max_fails=3 fail_timeout=60 weight=1;

}
upstream hi-linux {
 zone upstream-hi-linux 64k;
 server 192.168.2.210:8080 max_fails=3 fail_timeout=60 weight=1;

}
upstream web {
 zone upstream-web 64k;
 server 192.168.2.210:8080 max_fails=3 fail_timeout=60 weight=1;

}

server {
 listen 80 default_server;

 location / {
   root /usr/share/nginx/html/;
   index index.html;
 }

 location /stub_status {
   stub_status;
 }

 location /consul {
   proxy_pass http://consul;
 }

 location /hi-linux {
   proxy_pass http://hi-linux;
 }

}

如果想生成Nginx配置文件后自动加载配置,可以这样:


$ consul-template  -consul-addr 192.168.2.210:8500 -template="nginx.conf.ctmpl:/usr/local/nginx/conf/conf.d/default.conf:service nginx reload" -once

  • 运行consul-temple作为一个服务

上面两个例子都是consul-template运行一次后就退出了,如果想运行为一个服务可以这样:


$ consul-template \
 -consul-addr=192.168.2.210:8500 \
 -template "tmpltest.ctmpl:test.out"

  • 查询Consul实例同时渲染多个模板,然后重启相关服务。如果API故障则每30s尝试检测一次值,consul-template运行一次后退出。


$ consul-template \
 -consul-addr=192.168.2.210:8500 \
 -retry 30s \
 -once \
 -template "nginx.ctmpl:/etc/nginx/nginx.conf:service nginx restart" \
 -template "redis.ctmpl:/etc/redis/redis.conf:service redis restart" \
 -template "haproxy.ctmpl:/etc/haproxy/haproxy.conf:service haproxy restart"

  • 查询一个实例,Dump模板到标准输出。主要用作测试模板输出。


$ consul-template \
 -consul-addr=192.168.2.210:8500 \
 -template "tmpltest.ctmpl:result" \
 -once \
 -dry

配置文件方式

以上参数除了在命令行使用也可以直接配置在文件中,下面看看Consul-Template的配置文件,简称HCL(HashiCorp Configuration Language),它是和JSON兼容的。

下面先看看官方给的配置文件格式:


consul {

 auth {
   enabled  = true
   username = "test"
   password = "test"
 }

 address = "192.168.2.210:8500"
 token = "abcd1234"

 retry {
   enabled = true
   attempts = 5
   backoff = "250ms"
 }

 ssl {

   enabled = true
   verify = false
   cert = "/path/to/client/cert"
   key = "/path/to/client/key"
   ca_cert = "/path/to/ca"
   ca_path = "path/to/certs/"
   server_name = "my-server.com"
 }
}

reload_signal = "SIGHUP"
dump_signal = "SIGQUIT"
kill_signal = "SIGINT"
max_stale = "10m"
log_level = "warn"
pid_file = "/path/to/pid"


wait {
 min = "5s"
 max = "10s"
}

vault {
 address = "https://vault.service.consul:8200"
 token = "abcd1234"
 unwrap_token = true
 renew_token = true
 retry {
   # ...
 }

 ssl {
   # ...
 }
}


syslog {
 enabled = true
 facility = "LOCAL5"
}


deduplicate {
 enabled = true
 prefix = "consul-template/dedup/"
}


exec {
 command = "/usr/bin/app"
 splay = "5s"
 env {

   pristine = false
   custom = ["PATH=$PATH:/etc/myapp/bin"]
   whitelist = ["CONSUL_*"]
   blacklist = ["VAULT_*"]
 }

 reload_signal = ""
 kill_signal = "SIGINT"
 kill_timeout = "2s"
}

template {

 source = "/path/on/disk/to/template.ctmpl"
 destination = "/path/on/disk/where/template/will/render.txt"
 contents = "{{ keyOrDefault \"service/redis/maxconns@east-aws\" \"5\" }}"
 command = "restart service foo"
 command_timeout = "60s"
 perms = 0600
 backup = true
 left_delimiter  = "{{"
 right_delimiter = "}}"

 wait {
   min = "2s"
   max = "10s"
 }
}

更多详细的参数可以参考这里: https://github.com/hashicorp/consul-template#configuration-file-format

注意: 上面的选项不都是必选的,可根据实际情况调整。为了更加安全,token也可以从环境变量里读取,使用CONSUL_TOKENVAULT_TOKEN。强烈建议你不要把token放到未加密的文本配置文件中。

接下来我们看一个简单的实例,生成Nginx配置文件并重新加载:


$ vim nginx.hcl

consul {
address = "192.168.2.210:8500"
}

template {
source = "nginx.conf.ctmpl"
destination = "/usr/local/nginx/conf/conf.d/default.conf"
command = "service nginx reload"
}

注:nginx.conf.ctmpl模板文件内容还是和前面的一样。

执行以下命令就可渲染文件了:


$ consul-template -config "nginx.hcl"

更多官方例子可参考:https://github.com/hashicorp/consul-template/tree/master/examples

参考文档

http://www.google.com
https://my.oschina.net/guol/blog/675281
http://liangxiansen.cn/2017/04/06/consul/
https://github.com/hashicorp/consul-template


Consul Template

GitHub(包涵启动及配置):https://github.com/hashicorp/consul-template
配置参考:https://blog.csdn.net/lizhenhe/article/details/80030051
博客参考:https://www.hi-linux.com/posts/36431.html


签名:这个人很懒,什么也没有留下!
最新回复 (0)
返回