Difference between revisions of "HashiCorp/Consul"

From Ever changing code
Jump to navigation Jump to search
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Install =
<source lang=bash>
wget https://releases.hashicorp.com/consul/1.4.3/consul_1.4.3_linux_amd64.zip
unzip consul_1.4.3_linux_amd64.zip
sudo mv consul /usr/bin
</source>
= Run consul agent =
You need run at least one <tt>consul server</tt> 3-5 are recommended.
All other agents do run in a <tt>client</tt> mode, it's very light weight process that :
* register services
* check healthchecks
* forward queries to servers
* agent needs to run on every node that is a part of a cluster
Run consul in dev mode where single server is also acting as a client:
<source lang=bash>
consul agent -dev -node <consul_hostname>
# -node is optional as by default hostname is used, but for OSX with period in it will case problems with dns queries
</source>
Consul will start listenig on following ports
<source lang=bash>
Netid  State  Local Address:Port 
udp    UNCONN    127.0.0.1:8301 
udp    UNCONN    127.0.0.1:8302 
udp    UNCONN    127.0.0.1:8600 
tcp    LISTEN    127.0.0.1:8300 
tcp    LISTEN    127.0.0.1:8301 
tcp    LISTEN    127.0.0.1:8302 
tcp    LISTEN    127.0.0.1:8500 
tcp    LISTEN    127.0.0.1:8600 
</source>
Basic cluster checks and calls
<source lang=bash>
consul members -details
Node      Address        Status  Type    Build  Protocol  DC  Segment
u18gui-1  127.0.0.1:8301  alive  server  1.4.3  2        dc1  <all>
# API call
curl localhost:8500/v1/catalog/nodes
# DNS interface, using dns lookups, consuls listenig on udp/tcp:8600 port
dig @127.0.0.1 -p 8600 <consul_hostname>.node.consul
</source>
= Define a service =
Consul loads all configuration files in the configuration directory, so a common convention on Unix systems is to name the directory something like <code>/etc/consul.d</code>
<source lang=bash>
mkdir ./consul.d
echo '{"service": {"name": "web", "tags": ["rails"], "port": 80}}' \
    > ./consul.d/web.json
consul agent -dev -config-dir=./consul.d
</source>
Query service
<source lang=bash>
dig @127.0.0.1 -p 8600 web.service.consul
dig @127.0.0.1 -p 8600 web.service.consul SRV
curl  http://localhost:8500/v1/catalog/service/web
curl 'http://localhost:8500/v1/health/service/web?passing'
</source>
;Updating Services
Service definitions can be updated by changing configuration files and sending a SIGHUP to the agent. This lets you update services without any downtime or unavailability to service queries.
Alternatively, the HTTP API can be used to add, remove, and modify services dynamically.
= References =
= References =
*[https://blog.fraq.io/tech/consul-template/ A practical and simple consul-template example] Tutorial with docker restart based on a template change
*[https://blog.fraq.io/tech/consul-template/ A practical and simple consul-template example] Tutorial with docker restart based on a template change

Revision as of 08:36, 12 March 2019

Install

wget https://releases.hashicorp.com/consul/1.4.3/consul_1.4.3_linux_amd64.zip
unzip consul_1.4.3_linux_amd64.zip
sudo mv consul /usr/bin

Run consul agent

You need run at least one consul server 3-5 are recommended. All other agents do run in a client mode, it's very light weight process that :

  • register services
  • check healthchecks
  • forward queries to servers
  • agent needs to run on every node that is a part of a cluster

Run consul in dev mode where single server is also acting as a client:

consul agent -dev -node <consul_hostname>
# -node is optional as by default hostname is used, but for OSX with period in it will case problems with dns queries


Consul will start listenig on following ports

Netid   State  Local Address:Port  
udp     UNCONN     127.0.0.1:8301  
udp     UNCONN     127.0.0.1:8302  
udp     UNCONN     127.0.0.1:8600  
tcp     LISTEN     127.0.0.1:8300  
tcp     LISTEN     127.0.0.1:8301  
tcp     LISTEN     127.0.0.1:8302  
tcp     LISTEN     127.0.0.1:8500  
tcp     LISTEN     127.0.0.1:8600


Basic cluster checks and calls

consul members -details
Node      Address         Status  Type    Build  Protocol  DC   Segment
u18gui-1  127.0.0.1:8301  alive   server  1.4.3  2         dc1  <all>

# API call
curl localhost:8500/v1/catalog/nodes

# DNS interface, using dns lookups, consuls listenig on udp/tcp:8600 port
dig @127.0.0.1 -p 8600 <consul_hostname>.node.consul

Define a service

Consul loads all configuration files in the configuration directory, so a common convention on Unix systems is to name the directory something like /etc/consul.d

mkdir ./consul.d
echo '{"service": {"name": "web", "tags": ["rails"], "port": 80}}' \
    > ./consul.d/web.json
consul agent -dev -config-dir=./consul.d

Query service

dig @127.0.0.1 -p 8600 web.service.consul
dig @127.0.0.1 -p 8600 web.service.consul SRV
curl  http://localhost:8500/v1/catalog/service/web
curl 'http://localhost:8500/v1/health/service/web?passing'


Updating Services

Service definitions can be updated by changing configuration files and sending a SIGHUP to the agent. This lets you update services without any downtime or unavailability to service queries.

Alternatively, the HTTP API can be used to add, remove, and modify services dynamically.

References