Actions

HashiCorp Consul

From Ever changing code

Revision as of 08:36, 12 March 2019 by Pio2pio (talk | contribs) (Define a service)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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