Consul

(updated: )
  1. 1. Installation
  2. 2. Run
    1. 2.1. Dev Mode
    2. 2.2. With UI
    3. 2.3. More Options
    4. 2.4. Docker
    5. 2.5. hashi-ui
  3. 3. Used Ports
  4. 4. Cluster
    1. 4.1. Server
    2. 4.2. Client
  5. 5. Health Checks
    1. 5.1. Script + Interval
    2. 5.2. HTTP + Interval
    3. 5.3. TCP + Interval
    4. 5.4. TTL
    5. 5.5. Docker + Interval
  6. 6. Watches
    1. 6.1. key
    2. 6.2. keyprefix
    3. 6.3. services
    4. 6.4. nodes
    5. 6.5. service
    6. 6.6. checks
    7. 6.7. event
  7. 7. HTTP API
    1. 7.1. ACLs
      1. 7.1.1. Create ACL Token
      2. 7.1.2. Update ACL Token
      3. 7.1.3. Delete ACL Token
      4. 7.1.4. Read ACL Token
      5. 7.1.5. Clone ACL Token
      6. 7.1.6. List ACLs
    2. 7.2. Agent
      1. 7.2.1. List Members
      2. 7.2.2. Read Conf
      3. 7.2.3. Reload
      4. 7.2.4. Enable Maintenance Mode
      5. 7.2.5. Stream Logs
      6. 7.2.6. Join Agent
      7. 7.2.7. Graceful Leave
      8. 7.2.8. Force Leave
    3. 7.3. Check
      1. 7.3.1. List Checks
      2. 7.3.2. Register Check
      3. 7.3.3. Deregister Check
      4. 7.3.4. TTL Check Pass
      5. 7.3.5. TTL Check Warn
      6. 7.3.6. TTL Check Fail
    4. 7.4. Service
      1. 7.4.1. List Services
    5. 7.5. Register Service
      1. 7.5.1. Deregister Service
      2. 7.5.2. Enable Maintenance Mode
    6. 7.6. Catelog
      1. 7.6.1. List DataCenters
      2. 7.6.2. List Services
      3. 7.6.3. List Nodes for Service
      4. 7.6.4. List Services for Node
    7. 7.7. Health
      1. 7.7.1. Critical Service
      2. 7.7.2. Passing Service
      3. 7.7.3. Warning Service
    8. 7.8. KV Store
      1. 7.8.1. Get
      2. 7.8.2. Create/Update Key
      3. 7.8.3. Delete Key

Installation

Download package from https://www.consul.io/downloads.html, and unzip to ~/bin or /usr/local/bin.

1
2
curl https://releases.hashicorp.com/consul/0.8.3/consul_0.8.3_linux_amd64.zip -o /tmp/consul.zip;
unzip /tmp/consul.zip -d /usr/local/bin

Run

Options for consul agent

Dev Mode

1
consul agent -dev

With UI

1
consul agent -dev -ui

More Options

1
2
3
4
5
6
7
8
9
10
11
consul agent \
-bootstrap-expect=3 \ ## Consul waits until the specified number of servers are available and then bootstraps the cluster
-bind=10.0.20.1 \ ## If you have multi local IPs, choose one manually. Otherwize it will fail
-client=10.0.20.1 \ ## Don't set it, unless you expects the HTTP, DNS and RPC servers be exposed to non-127.0.0.1
-config-dir=/etc/consul.d/ \ ## A directory of configuration files (.json) to load
-data-dir=~/usr/local/consul/ \ ## Persistent state, the directory must be durable across reboots
-datacenter=uat \ ## Default: `dc1`. Nodes in the same datacenter should be on a single LAN
-encrypt=<`consul keygen`> \ ## All nodes in the cluster share the same key, used for messages between agents
-node=node_name \ ## Default: `$hostname`, must be unique within the cluster
-server \ ## Server mode, otherwise client mode
-ui ## Enable the default dashboard on http://localhost:8500/ui

Docker

Dev Mode:

1
2
3
4
5
6
7
docker run -d \
--name consul \
-p 8300:8300 \
-p 8400:8400 \
-p 8500:8500 \
-p 8600:8600 \
consul

hashi-ui

1
2
3
4
5
6
docker run -d \
--name=hashi-ui \
-e CONSUL_ENABLE=1 \
-e CONSUL_ADDR=<ip>:<port> \
-p 3000:3000 \
jippi/hashi-ui

Used Ports

  • 8300: server RPC, TCP only, handle RPC from other agents
  • 8301: gossip in LAN, TCP and UDP, required
  • 8302: gossip over WAN, TCP and UDP
  • 8400: cli RPC, TCP only, handle RPC from cli
  • 8500: HTTP API, UI, TCP only
  • 8600: DNS Interface, TCP and UDP, handle DNS queries

Cluster

Server

3 or 5 server recommended. Start the Consul server with the following command on 3 or 5 servers.

1
2
3
4
5
6
consul agent \
-server \
-bootstrap-expect=3 \
-data-dir=/tmp/consul \
-bind=<ip> \
-start_join=<server1>

Client

Start the Consul client on every server that is part of the cluster with the following command.

1
2
3
4
consul agent \
-data-dir=/tmp/consul \
-bind=<ip> \
-start_join=<server1,server2,server3>

Health Checks

Script + Interval

Exit code of 0 will be treated as passing.
Example:

1
2
3
4
5
6
{
"check": {
"script": "curl localhost >/dev/null 2>&1",
"interval": "10s"
}
}

1
2
3
4
5
6
7
8
9
{
"check": {
"id": "mem-util",
"name": "Memory utilization",
"script": "/usr/local/bin/check_mem.py",
"interval": "10s",
"timeout": "1s"
}
}

HTTP + Interval

Based on the http response code:

  • 2xx: passing
  • 429 (Too Many Requests): warning
  • others: failure

Example:

1
2
3
4
5
6
7
8
9
{
"check": {
"id": "api",
"name": "HTTP API on port 5000",
"http": "http://localhost:5000/health",
"interval": "10s",
"timeout": "1s"
}
}

TCP + Interval

Checks if the tcp connection attemp is successful.

Example:

1
2
3
4
5
6
7
8
9
{
"check": {
"id": "ssh",
"name": "SSH TCP on port 22",
"tcp": "localhost:22",
"interval": "10s",
"timeout": "1s"
}
}

TTL

Services report its status to Consul periodically over the HTTP, if no updates for the given TTL, the service will be marked as cretical.
Endpoint: /v1/agent/check/pass/<checkId>

Example:

1
2
3
4
5
6
7
8
{
"check": {
"id": "web-app",
"name": "Web App Status",
"notes": "Web app does a curl internally every 10 seconds",
"ttl": "30s"
}
}

Docker + Interval

Docker ported [Script + Interval]

Example:

1
2
3
4
5
6
7
8
9
10
{
"check": {
"id": "mem-util",
"name": "Memory utilization",
"docker_container_id": "f972c95ebf0e",
"shell": "/bin/bash",
"script": "/usr/local/bin/check_mem.py",
"interval": "10s"
}
}

Watches

key

Watch a specific KV pair.

Example:

1
2
3
4
5
{
"type": "key",
"key": "foo/bar/baz",
"handler": "/usr/bin/my-key-handler.sh"
}

1
consul watch -type=key -key=foo/bar/baz /usr/bin/my-key-handler.sh

keyprefix

Watch a prefix in the KV store.

Example:

1
2
3
4
5
{
"type": "keyprefix",
"prefix": "foo/",
"handler": "/usr/bin/my-prefix-handler.sh"
}

1
consul watch -type=keyprefix -prefix=foo/ /usr/bin/my-prefix-handler.sh

services

Watch the list of available services.

nodes

Watch the list of nodes.

service

Watch the instances of a service.

Example:

1
2
3
4
5
{
"type": "service",
"service": "redis",
"handler": "/usr/bin/my-service-handler.sh"
}

1
consul watch -type=service -service=redis /usr/bin/my-service-handler.sh

checks

Watch the value of health checks.

event

Watch for custom user events.

Example:

1
2
3
4
5
{
"type": "event",
"name": "web-deploy",
"handler": "/usr/bin/my-deploy-handler.sh"
}

1
consul watch -type=event -name=web-deploy /usr/bin/my-deploy-handler.sh

To fire a new web-deploy event:

1
consul event -name=web-deploy 1609030

HTTP API

ACLs

Create ACL Token

payload.json

1
2
3
4
5
{
"Name": "my-app-token",
"Type": "client",
"Rules": ""
}

Param Type Description
ID string If not provided, a UUID is generated
Name string a human-friendly name for the ACL token
Type string client (default) or management
Rules string rules for this ACL token
1
curl -X PUT -d @payload.json http://localhost:8500/v1/acl/create

Update ACL Token

payload.json:

1
2
3
4
5
6
{
"ID": "adf4238a-882b-9ddc-4a9d-5b6758e4159e",
"Name": "my-app-token-updated",
"Type": "client",
"Rules": "# New Rules",
}

1
curl -X PUT -d @payload.json http://localhost:8500/v1/acl/update

Delete ACL Token

1
curl -X PUT http://localhost:8500/v1/acl/destroy/8f246b77-f3e1-ff88-5b48-8ec93abf3e05

Read ACL Token

1
curl http://localhost:8500/v1/acl/destroy/8f246b77-f3e1-ff88-5b48-8ec93abf3e05

Sample Response:

1
2
3
4
5
6
7
8
9
10
[
{
"CreateIndex": 3,
"ModifyIndex": 3,
"ID": "8f246b77-f3e1-ff88-5b48-8ec93abf3e05",
"Name": "Client Token",
"Type": "client",
"Rules": "..."
}
]

Clone ACL Token

1
curl -X PUT http://localhost:8500/v1/acl/clone/8f246b77-f3e1-ff88-5b48-8ec93abf3e05

Sample Response:

1
2
3
{
"ID": "adf4238a-882b-9ddc-4a9d-5b6758e4159e"
}

Sample Response:

1
2
3
4
5
6
7
8
9
10
11
12
{
"service:redis": {
"Node": "foobar",
"CheckID": "service:redis",
"Name": "Service 'redis' check",
"Status": "passing",
"Notes": "",
"Output": "",
"ServiceID": "redis",
"ServiceName": "redis"
}
}

List ACLs

1
curl -X PUT http://localhost:8500/v1/acl/list

Agent

From the local Consul agent’s point of view, might be different from other agents.

List Members

1
curl http://localhost:8500/v1/agent/members

Read Conf

1
curl http://localhost:8500/v1/agent/self

Reload

1
curl -X PUT http://localhost:8500/v1/agent/reload

Enable Maintenance Mode

1
curl -X PUT http://localhost:8500/v1/agent/maintenance?enable=true&reason=Upgration

Stream Logs

1
curl http://localhost:8500/v1/agent/monitor

Join Agent

1
curl -X PUT http://localhost:8500/v1/agent/join/:address

Graceful Leave

1
curl -X PUT http://localhost:8500/v1/agent/leave

Force Leave

1
curl -X PUT http://localhost:8500/v1/agent/force-leave

Check

Operating on Check by agent API.

List Checks

1
curl http://localhost:8500/v1/agent/checks

Register Check

Sample payload.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"ID": "mem",
"Name": "Memory utilization",
"Notes": "Ensure we don't oversubscribe memory",
"DeregisterCriticalServiceAfter": "90m",
"Script": "/usr/local/bin/check_mem.py",
"DockerContainerID": "f972c95ebf0e",
"Shell": "/bin/bash",
"HTTP": "http://example.com",
"TCP": "example.com:22",
"Interval": "10s",
"TTL": "15s",
"TLSSkipVerify": true
}

1
curl -X PUT -d @payload.json http://localhost:8500/v1/agent/check/register

Deregister Check

1
curl -X PUT http://localhost:8500/v1/agent/check/deregister/:check_id

TTL Check Pass

1
curl http://localhost:8500/v1/agent/check/pass/:check_id

TTL Check Warn

1
curl http://localhost:8500/v1/agent/check/warn/:check_id

TTL Check Fail

1
curl http://localhost:8500/v1/agent/check/fail/:check_id

Service

Operating on Check by agent API.

List Services

1
curl http://localhost:8500/v1/agent/services

Register Service

Sample payload.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"ID": "redis1",
"Name": "redis",
"Tags": [
"primary",
"v1"
],
"Address": "127.0.0.1",
"Port": 8000,
"EnableTagOverride": false,
"Check": {
"DeregisterCriticalServiceAfter": "90m",
"Script": "/usr/local/bin/check_redis.py",
"HTTP": "http://localhost:5000/health",
"Interval": "10s",
"TTL": "15s"
}
}

1
curl -X PUT -d @payload.json http://localhost:8500/v1/agent/service/register

Deregister Service

1
curl -X PUT http://localhost:8500/v1/agent/service/deregister/:service_id

Enable Maintenance Mode

1
curl -X PUT http://localhost:8500/v1/agent/service/maintenance/:service_id?enable=true&reason=Upgration

Catelog

https://www.consul.io/api/catalog.html

List DataCenters

1
curl -X PUT http://localhost:8500/v1/catalog/datacenters

List Services

1
curl -X PUT http://localhost:8500/v1/catalog/services

List Nodes for Service

1
curl http://localhost:8500/v1/catalog/service/:service

List Services for Node

1
curl http://localhost:8500/v1/catalog/node/:node

Health

Critical Service

1
curl http://localhost:8500/v1/health/state/critical

Passing Service

1
curl http://localhost:8500/v1/health/state/passing

Warning Service

1
curl http://localhost:8500/v1/health/state/warning

KV Store

Get

1
2
curl http://localhost:8500/v1/kv/:key
curl http://localhost:8500/v1/kv/:key?recurse=true

Create/Update Key

1
curl -X PUT -d "value" http://localhost:8500/v1/kv/:key

Delete Key

1
2
curl -X DELETE http://localhost:8500/v1/kv/:key
curl -X DELETE http://localhost:8500/v1/kv/:key?recurse=true