Docker: Playing with consul

root's picture

Below you have few ways to create small consul clusters that will do nothing, just to show you how they can be made to connect to each other.

Useful things to work with

Here you have just small docker tricks to be able to see what consul is doing and what is exposing.

View container's logs by having them shown as they come, starting with the last 10 lines:

docker logs -f --tail 10 CONTAINER_NAME

root@uranus:~# docker logs -f --tail 10 c4
    2019/04/11 21:12:55 [DEBUG] memberlist: Stream connection from=192.168.11.28:44010
    2019/04/11 21:13:12 [DEBUG] agent: Skipping remote check "serfHealth" since it is managed automatically
    2019/04/11 21:13:12 [DEBUG] agent: Node info in sync
    2019/04/11 21:13:15 [DEBUG] memberlist: Initiating push/pull sync with: 192.168.11.28:8301
    2019/04/11 21:13:25 [DEBUG] memberlist: Stream connection from=192.168.11.28:44011
    2019/04/11 21:13:30 [DEBUG] manager: Rebalanced 2 servers, next active server is uranus.dc1 (Addr: tcp/192.168.11.10:8300) (DC: dc1)
    2019/04/11 21:13:37 [DEBUG] memberlist: Initiating push/pull sync with: 192.168.11.28:8302
    2019/04/11 21:13:39 [DEBUG] memberlist: Stream connection from=192.168.11.28:42384
    2019/04/11 21:13:45 [DEBUG] memberlist: Initiating push/pull sync with: 192.168.11.28:8301
    2019/04/11 21:13:55 [DEBUG] memberlist: Stream connection from=192.168.11.28:44014

View container's open ports:

docker port CONTAINER_NAME

23:06:22 root@service:~# docker port c1
8300/tcp -> 0.0.0.0:8300
8301/tcp -> 0.0.0.0:8301
8301/udp -> 0.0.0.0:8301
8302/tcp -> 0.0.0.0:8302
8302/udp -> 0.0.0.0:8302
8500/tcp -> 0.0.0.0:8500
8600/tcp -> 0.0.0.0:8600
8600/udp -> 0.0.0.0:8600

Consul's web interface:

http://HOSTPORT:8500

Consul's port needs

Server RPC (Default 8300/TCP) - used by servers to handle incoming requests from other agents
Serf LAN (Default 8301/TCP/UDP) - used to handle gossip in the LAN. Required by all agents
Serf WAN (Default 8302/TCP/UDP) - used by servers to gossip over the WAN to other servers
HTTP API (Default 8500/TCP) - used by clients to talk to the HTTP API
DNS Interface (Default 8600/TCP/UDP) - used to resolve DNS queries

Create a small docker consul cluster in one host

In this example, a 3 nodes consul cluster will be created within the same host. As you can see, the communication is done within the docker's private network.

23:54:06 root@service:~# docker run -d --name=c1 -p 8301:8301 -p 8500:8500 consul agent -dev -client=0.0.0.0 -bind=0.0.0.0
49ffdb1c7da1b990f95f31068c1868d80ce2476e150cd3ae0ac4239faad8c64e
23:55:04 root@service:~# IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' c1); echo $IP
172.17.0.6
23:55:09 root@service:~# docker run -d --name c2 consul agent -dev -bind=0.0.0.0 -join=$IP
325d5bb7d7c81dad03e6cb9713454683a1af27d9930e1c1227db5a9c885b38d4
23:55:14 root@service:~# docker run -d --name c3 consul agent -dev -bind=0.0.0.0 -join=$IP
66be7144da0342e9b9316a2d3f35ad267423da6c543b23367a013d6f7a687159
23:55:23 root@service:~# docker exec -t c1 consul members
Node          Address          Status  Type    Build  Protocol  DC   Segment
325d5bb7d7c8  172.17.0.7:8301  alive   server  1.4.4  2         dc1  <all>
49ffdb1c7da1  172.17.0.6:8301  alive   server  1.4.4  2         dc1  <all>
66be7144da03  172.17.0.8:8301  alive   server  1.4.4  2         dc1  <all>

Create a small docker consul cluster between hosts

In this example we attempt to create a two nodes consul cluster between two hosts. As you may notice, this time we will not use docker's private network but the host's one.

On host1:

23:58:45 root@service:~# docker run -d --network host --name=c1 consul agent -dev -client=0.0.0.0 -bind=192.168.11.28
089fc4c0ea905827e29df93d481948552602a840d196db8a7daa4e8676acd58e
23:58:52 root@service:~# docker exec -t c1 consul members
Node     Address             Status  Type    Build  Protocol  DC   Segment
service  192.168.11.28:8301  alive   server  1.4.4  2         dc1  <all>

On host2:

root@uranus:~# docker run -d --network host --name c4 consul agent -dev -bind=192.168.11.10 -join=192.168.11.28
fe2ebf7f573ae462a3835bf5090f02748edd11c96cf046a85d78918ef8a8776d
root@uranus:~# docker exec -t c4 consul members
Node     Address             Status  Type    Build  Protocol  DC   Segment
service  192.168.11.28:8301  alive   server  1.4.4  2         dc1  <all>
uranus   192.168.11.10:8301  alive   server  1.4.4  2         dc1  <all>

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself! Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.

Recent content

root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root