How do I setup and use Consul?
- atmail CoS
Requirement to use CoS within your on-premises installation.
Clients of Consul can register a service, such as
mysql, and other clients can use Consul to discover providers of a given service. Using either DNS or HTTP, applications can easily find the services they depend upon.
For more infomation, refer to https://www.consul.io/intro/index.html
Server mode and client mode
Consul agent can run in both server or client mode. When working on client mode, it works as a proxy.
Consul is just a simple binary, which can be downloaded from https://www.consul.io/downloads.html
After download, it can be put into /usr/local/bin (or any other places searchable by $PATH ).
Using Consul on Development environment
The following command will start consul in dev mode. The difference between dev mode vs production mode is that in dev mode consul doesn't save data.
consul agent -dev
Below command can be used for running consul in a single node
consul agent -server -bind 127.0.0.1 -client 127.0.0.1 -ui -data-dir=/path/to/data -bootstrap-expect=1
It's recommended to have 3-5 nodes running consul server, and the rest can run consul in client mode.
Configure Consul as DNS server
Consul provides dns service by default. Services registered in consul can be resolved via dns query with built-in load balancing. Since doesn't listen to port 53 by default, the best practice is to setup DNS forwarding to use consul's dns feature.
yum install -y dnsmasq
Create below file in /etc/dnsmasq.d
# Enable forward lookup of the ‘consul’ domain:
Start dnsmasq service
systemctl enable dnsmasq
systemctl restart dnsmasq
Point local DNS resolver to local dnsmasq
Assume cosadm is running, now you can dig like below.
$ dig cosadm.service.consul SRV
; <<>> DiG 9.9.4-RedHat-9.9.4-73.el7_6 <<>> cosadm.service.consul SRV
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 51009
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;cosadm.service.consul. IN SRV
;; ANSWER SECTION:
cosadm.service.consul. 0 IN SRV 1 1 9002 c0a83865.addr.dc1.consul.
;; ADDITIONAL SECTION:
c0a83865.addr.dc1.consul. 0 IN A 192.168.56.101
git.ru.node.dc1.consul. 0 IN TXT “consul-network-segment=”
;; Query time: 5 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Tue May 14 19:20:44 EDT 2019
;; MSG SIZE rcvd: 158