From Ever changing code
Jump to navigation Jump to search


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  
udp     UNCONN  
udp     UNCONN  
tcp     LISTEN  
tcp     LISTEN  
tcp     LISTEN  
tcp     LISTEN  
tcp     LISTEN

Basic cluster checks and calls

consul members -details
Node      Address         Status  Type    Build  Protocol  DC   Segment
u18gui-1  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 @ -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 @ -p 8600 web.service.consul
dig @ -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.