Confd
CONFD generates the final configuration file by reading the configuration (support Etcd,consul, environment variables), and through the go template.
Installation
Installation and ETCD, very convenient, has provided a 64-bit executable program, downloaded directly into the path (/usr/local/bin) can (don't forget +x).
Haproxy Configuration Generation
The CONFD configuration file is/etc/confd by default and can be specified by parameter-confdir. The directory contains two subdirectories, respectively: CONF.D templates.
CONFD will first read the configuration file (toml format) in the CONF.D directory, and then render the template based on the template path specified by the file.
Read the directory and key in/services/web based on the ETCD cluster that was previously configured. Structure is:
/services/web/$DOMAIN/$HOST/IP
|-port
Where domain represents the domain name where the application is registered, and host represents the hostname of the registered machine.
First create the CONFD configuration file:
| The code is as follows |
Copy Code |
[Template] src = "Haproxy.cfg.tmpl" Dest = "/home/babydragon/haproxy/haproxy.cfg" Keys = [ "/services/web", ] #reload_cmd = "/etc/init.d/haproxy Reload" |
Now only test template generation, so do not pay attention to check, restart and other commands, configuration template name, target path and the key can be obtained.
Focus on the change section of the template (the front is the haproxy of some of the standard configuration, such as logs, etc.)
| The code is as follows |
Copy Code |
frontend webin Bind:80 {{{$domains: = Lsdir "/services/web"}} {range $domain: = $domains}} ACL is_{{$domain}} Host)-I {{$domain}}} {{end}} {{range $domain: = $domains}} Use_backend {{$domain}}_cluster if is_{{$domai n} {{end}} {range $domain: = $domains}} Backend {{$domain}}_cluster cookie ServerID Insert Indirect NoCache {{{$domain _dir: = printf "/services/web/%s" $domain}}{{range $host: = Lsdir $domain _dir}} Server {base $h OST} {{$ip _key: = printf "/services/web/%s/%s/ip" $domain $host}}{{getv $ip _key}}:{{$port _key: = printf/services/web/ %s/%s/port ' $domain $host}}{{getv $port _key} ' cookie {base $host}} Check {end}}} |
Here there are two loops, the first loops all the domain names, the first loops all the hosts under each domain name. The
Haproxy needs to do load balancing by domain name by setting ACLs. So first cycle the domain name, create an ACL rule for each domain name and use a rule.
The following loop is used to create a later segment that does not have a domain name corresponding to it. The
confd template Detail document can refer to documents on GitHub. The
roughly means to get all subdirectories under the current directory by Lsdir. The first level subdirectory is the domain name, which can generate ACL rules, rule usage, backend names, and so on.
and then re-pass the bottled domain directory, performs lsdir on the domain name directory, reads each host name under the directory, creates a backend server entry (a load-balanced back-end servers under a domain name), and
gets the property-key-value pairs that are hanging in this directory (only IP and port). After the
finishes creating templates and configurations, construct some test data:
| code is as follows |
copy code |
etcdctl mkdir/services/web Etcdctl mkdir/services/web/a.abc.com Etcdctl mkdir/services/web/b.abc.com Etcdctl mkdir/services/web/a.abc.com/server1 Etcdctl mkdir/services/web/ A.abc.com/server2 Etcdctl mkdir/services/web/b.abc.com/server1 Etcdctl set/services/web/a.abc.com/server1/ IP 10.0.0.1 Etcdctl set/services/web/a.abc.com/server1/port 10000 Etcdctl set/services/web/a.abc.com/server2 /port 10001 Etcdctl set/services/web/a.abc.com/server2/ip 10.0.0.1 Etcdctl set/services/web/b.abc.com/ SERVER1/IP 10.0.0.2 Etcdctl set/services/web/b.abc.com/server1/port 12345 |
This simulates three containers, two of which are running containers as domain a.abc.com, and one as a b.abc.com container.
Then execute CONFD to check the generated configuration file:
| The code is as follows |
Copy Code |
| Confd-confdir./confd-onetime-backend Etcd-node 127.0.0.1:4001 |
After the template was rendered:
| The code is as follows |
Copy Code |
Frontend webin Bind:80
ACL is_a.abc.com HDR (host)-I a.abc.com
ACL is_b.abc.com HDR (host)-I b.abc.com
Use_backend A.abc.com_cluster If is_a.abc.com
Use_backend B.abc.com_cluster If is_b.abc.com
Backend A.abc.com_cluster Cookie ServerID Insert Indirect nocache
Server Server1 10.0.0.1:10000 cookie Server1 Check
Server Server2 10.0.0.1:10001 cookie server2 Check
Backend B.abc.com_cluster Cookie ServerID Insert Indirect nocache
Server Server1 10.0.0.2:12345 cookie Server1 Check
No Comments |