Examples
HAProxy
HAProxy is a very common load balancer. You can read more about the HAProxy configuration file syntax in the HAProxy documentation, but here is an example template for rendering an HAProxy configuration file with Consul Template:
global
daemon
maxconn {{key "service/haproxy/maxconn"}}
defaults
mode {{key "service/haproxy/mode"}}{{range ls "service/haproxy/timeouts"}}
timeout {{.Key}} {{.Value}}{{end}}
listen http-in
bind *:8000{{range service "release.web"}}
server {{.Node}} {{.Address}}:{{.Port}}{{end}}
Save this file to disk as haproxy.ctmpl and run the consul-template daemon:
$ consul-template \
-consul demo.consul.io \
-template haproxy.ctmpl:/etc/haproxy/haproxy.conf
-dry
Depending on the state of the demo Consul instance, you could see the following output:
global
daemon
maxconn 4
defaults
mode default
timeout 5
listen http-in
bind *:8000
server nyc3-worker-2 104.131.109.224:80
server nyc3-worker-3 104.131.59.59:80
server nyc3-worker-1 104.131.86.92:80
For more information on how to save this result to disk or for the full list of functionality available inside a Consul template file, please consult the API documentation.
Varnish
Varnish is an common caching engine that can also act as a proxy. You can read more about the Varnish configuration file syntax in the Varnish documentation, but here is an example template for rendering a Varnish configuration file with Consul Template:
import directors;
{{range service "consul"}}
backend {{.Name}}_{{.ID}} {
.host = "{{.Address}}";
.port = "{{.Port}}";
}{{end}}
sub vcl_init {
new bar = directors.round_robin();
{{range service "consul"}}
bar.add_backend({{.Name}}_{{.ID}});{{end}}
}
sub vcl_recv {
set req.backend_hint = bar.backend();
}
Save this file to disk as varnish.ctmpl and run the consul-template daemon:
$ consul-template \
-consul demo.consul.io \
-template varnish.ctmpl:/etc/varnish/varnish.conf \
-dry
You should see the following output:
import directors;
backend consul_consul {
.host = "104.131.109.106";
.port = "8300";"
}
sub vcl_init {
new bar = directors.round_robin();
bar.add_backend(consul_consul);
}
sub vcl_recv {
set req.backend_hint = bar.backend();
}
Apache httpd
Apache httpd is a popular web server. You can read more about the Apache httpd configuration file syntax in the Apache httpd documentation, but here is an example template for rendering part of an Apache httpd configuration file that is responsible for configuring a reverse proxy with dynamic end points based on service tags with Consul Template:
{{range $tag, $service := service "web" | byTag}}
# "{{$tag}}" api providers.
<Proxy balancer://{{$tag}}>
{{range $service}} BalancerMember http://{{.Address}}:{{.Port}}
{{end}} ProxySet lbmethod=bybusyness
</Proxy>
Redirect permanent /api/{{$tag}} /api/{{$tag}}/
ProxyPass /api/{{$tag}}/ balancer://{{$tag}}/
ProxyPassReverse /api/{{$tag}}/ balancer://{{$tag}}/
{{end}}
Just like the previous examples, save this file to disk and run the consul-template daemon:
$ consul-template \
-consul demo.consul.io \
-template httpd.ctmpl:/etc/httpd/sites-available/balancer.conf
You should see output similar to the following:
# "frontend" api providers.
<Proxy balancer://frontend>
BalancerMember http://104.131.109.106:8080
BalancerMember http://104.131.109.113:8081
ProxySet lbmethod=bybusyness
</Proxy>
Redirect permanent /api/frontend /api/frontend/
ProxyPass /api/frontend/ balancer://frontend/
ProxyPassReverse /api/frontend/ balancer://frontend/
# "api" api providers.
<Proxy balancer://api>
BalancerMember http://104.131.108.11:8500
ProxySet lbmethod=bybusyness
</Proxy>
Redirect permanent /api/api /api/api/
ProxyPass /api/api/ balancer://api/
ProxyPassReverse /api/api/ balancer://api/
Querying all services
As of Consul Template 0.6.0, it is possible to have a complex dependency graph with dependent services. As such, it is possible to query and watch all services in Consul:
{{range services}}# {{.Name}}{{range service .Name}}
{{.Address}}{{end}}
{{end}}
Just like the previous examples, save this file to disk and run the consul-template daemon:
$ consul-template \
-consul demo.consul.io \
-template everything.ctmpl:/tmp/inventory
You should see output similar to the following:
# consul
104.131.121.232
# redis
104.131.86.92
104.131.109.224
104.131.59.59
# web
104.131.86.92
104.131.109.224
104.131.59.59
Running and Process Lifecycle
While there are multiple ways to run Consul Template, the most common pattern is to run Consul Template as a system service. When Consul Template first starts, it reads any configuration files and templates from disk and loads them into memory. From that point forward, changes to the files on disk do not propagate to running process without a reload.
The reason for this behavior is simple and aligns with other tools like haproxy. A user may want to perform pre-flight validation checks on the configuration or templates before loading them into the process. Additionally, a user may want to update configuration and templates simultaneously. Having Consul Template automatically watch and reload those files on changes is both operationally dangerous and against some of the paradigms of modern infrastructure. Instead, Consul Template listens for the SIGHUP syscall to trigger a configuration reload. If you update configuration or templates, simply send HUP to the running Consul Template process and Consul Template will reload all the configurations and templates from disk.
Debugging
Consul Template can print verbose debugging output. To set the log level for Consul Template, use the -log-level flag:
$ consul-template -log-level info ...
<timestamp> [INFO] (cli) received redis from Watcher
<timestamp> [INFO] (cli) invoking Runner
# ...
You can also specify the level as debug:
$ consul-template -log-level debug ...
<timestamp> [DEBUG] (cli) creating Runner
<timestamp> [DEBUG] (cli) creating Consul API client
<timestamp> [DEBUG] (cli) creating Watcher
<timestamp> [DEBUG] (cli) looping for data
<timestamp> [DEBUG] (watcher) starting watch
<timestamp> [DEBUG] (watcher) all pollers have started, waiting for finish
<timestamp> [DEBUG] (redis) starting poll
<timestamp> [DEBUG] (service redis) querying Consul with &{...}
<timestamp> [DEBUG] (service redis) Consul returned 2 services
<timestamp> [DEBUG] (redis) writing data to channel
<timestamp> [DEBUG] (redis) starting poll
<timestamp> [INFO] (cli) received redis from Watcher
<timestamp> [INFO] (cli) invoking Runner
<timestamp> [DEBUG] (service redis) querying Consul with &{...}
# ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论