Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save markwylde/0b48a20cc683f54eb8b7aad769d33fac to your computer and use it in GitHub Desktop.
Save markwylde/0b48a20cc683f54eb8b7aad769d33fac to your computer and use it in GitHub Desktop.

Example

1. Docker swarm

Make docker a one node swarm cluster.

docker swarm init

2. Create network

Create an overlay network so multiple stacks can communicate with each other.

docker network create --scope swarm --driver overlay --attachable nats-net

3. Create stacks

Deploy a new scalable nats stack.

docker stack deploy --compose-file docker-compose-with-bootstrap.yml one
docker stack deploy --compose-file docker-compose-node-only.yml two
version: "3.7"
services:
nats-node:
image: nats
command: -m 8222 -D -cluster nats://0.0.0.0:6222 -routes nats://one_nats-bootstrap:6222
networks:
default:
external: true
name: nats-net
version: "3.7"
services:
nats-bootstrap:
image: nats
command: -m 8222 -D -cluster nats://0.0.0.0:6222
deploy:
endpoint_mode: dnsrr
nats-node:
image: nats
command: -m 8222 -D -cluster nats://0.0.0.0:6222 -routes nats://nats-bootstrap:6222
networks:
default:
external: true
name: nats-net
@markwylde
Copy link
Author

Just to note, if you don't set the endpoint_mode: dnsrr then the nodes will hit the load balancer when trying to contact the nats-bootstrap service. The problem with this is that the bootstrap instance of nats will see the ip address of nodes as coming form the vip (virtual ip in swarm) rather than it's own ip address.

So just set the endpoint_mode to dnsrr which will prevent the load balancer from working.

Also, never scale the bootstrap node. If your first stack needs to be scaled, then scale the nats-node. The bootstrap node is just a central point for nodes when they start up. The load should be shared among the whole cluster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment