Docker Swarm

    First create an overlay network:

    Next instantiate an initial “seed” server for a NATS cluster listening for other servers to join route to it on port 6222:

    1. docker service create --network nats-cluster-example --name nats-cluster-node-1 nats:1.0.0 -cluster nats://0.0.0.0:6222 -DV

    The 2nd step is to create another service which connects to the NATS server within the overlay network. Note that we connect to to the server at nats-cluster-node-1:

    1. docker service create --name ruby-nats --network nats-cluster-example wallyqs/ruby-nats:ruby-2.3.1-nats-v0.8.0 -e '
    2. NATS.on_error do |e|
    3. puts "ERROR: #{e}"
    4. end
    5. NATS.start(:servers => ["nats://nats-cluster-node-1:4222"]) do |nc|
    6. inbox = NATS.create_inbox
    7. puts "[#{Time.now}] Connected to NATS at #{nc.connected_server}, inbox: #{inbox}"
    8. nc.subscribe(inbox) do |msg, reply|
    9. puts "[#{Time.now}] Received reply - #{msg}"
    10. nc.subscribe("hello") do |msg, reply|
    11. next if reply == inbox
    12. puts "[#{Time.now}] Received greeting - #{msg} - #{reply}"
    13. nc.publish(reply, "world")
    14. end
    15. EM.add_periodic_timer(1) do
    16. puts "[#{Time.now}] Saying hi (servers in pool: #{nc.server_pool}"
    17. end
    18. end'

    In this case, nats-cluster-node-1 is seeding the rest of the cluster through the autodiscovery feature. Now NATS servers nats-cluster-node-1 and nats-cluster-node-2 are clustered together.

    Add in more replicas of the subscriber:

    1. docker service scale ruby-nats=3

    Then confirm the distribution on the Docker Swarm cluster:

    1. docker service ps ruby-nats

    The sample output after adding more NATS server nodes to the cluster, is below - and notice that the client is dynamically aware of more nodes being part of the cluster via auto discovery!

    1. [2016-08-15 12:51:52 +0000] Saying hi (servers in pool: [{:uri=>#<URI::Generic nats://10.0.1.3:4222>, :was_connected=>true, :reconnect_attempts=>0}]
    2. [2016-08-15 12:51:53 +0000] Saying hi (servers in pool: [{:uri=>#<URI::Generic nats://10.0.1.3:4222>, :was_connected=>true, :reconnect_attempts=>0}]
    3. [2016-08-15 12:51:54 +0000] Saying hi (servers in pool: [{:uri=>#<URI::Generic nats://10.0.1.3:4222>, :was_connected=>true, :reconnect_attempts=>0}]
    4. [2016-08-15 12:51:55 +0000] Saying hi (servers in pool: [{:uri=>#<URI::Generic nats://10.0.1.3:4222>, :was_connected=>true, :reconnect_attempts=>0}, {:uri=>#<URI::Generic nats://10.0.1.7:4222>, :reconnect_attempts=>0}, {:uri=>#<URI::Generic nats://10.0.1.6:4222>, :reconnect_attempts=>0}]

    Sample output after adding more workers which can reply back (since ignoring own responses):

    1. [2016-08-15 16:06:26 +0000] Received reply - world
    2. [2016-08-15 16:06:26 +0000] Received reply - world
    3. [2016-08-15 16:06:27 +0000] Received greeting - hi - _INBOX.b8d8c01753d78e562e4dc561f1

    From here you can experiment adding to the NATS cluster by simply adding servers with new service names, that route to the seed server nats-cluster-node-1. As you’ve seen above, clients will automatically be updated to know that new servers are available in the cluster.