2. Who am I?
● Software Engineer @ Red Hat; developing developer tools
● Co-organizer of Docker and Ansible meetups
● Work on CentOS, Docker, Ansible, Vagrant, etc.
● Love vim, zsh, i3wm, and all things CLI
● dharmit on GitHub
dharm1t on Twitter
3. What is Swarm Mode?
● A mode built into Docker Engine
● Available with Docker 1.12 or later
● Lets you manage a cluster of Docker Engines
● Cluster of Docker Engines is called a swarm
● Use the familiar Docker CLI to create a swarm, deploy
application services to a swarm, and manage swarm
behavior.
● Swarm Mode != Swarm Kit
4. Features
● Cluster management integrated with Docker Engine
● Can deploy Manager/Worker node using Docker Engine
● Declarative model to describe entire application stack
● Scale up or down based on requirements
● Constantly monitors and reconciles the cluster state
● Built in service discovery
● TLS auth and encryption for secure communication
6. My Setup
● 2 VMs running on DigitalOcean
● 1 will run as manager
● 1 will run as worker
● Add 1 VM to work as manager
7. Initialize a Swarm
$ docker swarm init
● This command will initialize a swarm
● Docker Engine targeted by this command becomes a manager
in the newly created single-node swarm
● Generates two random tokens - manager and worker tokens
● Use proper token to make a node join as worker or manager
8. Add a node to Swarm
$ docker swarm join
--token <random-token-generated-by-previous-command>
<ip-port-of-manager>
This command will add a node to a Swarm as worker node
$ docker swarm join-token manager
This command will add a manager node to Swarm
9. Create services
$ docker service create --name redis redis:3.0.6
Creates a service named redis
$ docker service create --name redis --replicas=5
redis:3.0.6
Creates a service redis with 5 replicas across the swarm
$ docker service ls
Lists the services in the cluster
10. Replicated mode & Global mode
$ docker service create --name redis --replicas=5 redis:3.0.6
Creates a service redis with 5 replicas across the swarm
$ docker service create --name redis --mode global redis:3.0.6
Creates a global service with 1 container on each node
11. Attach service to network
$ docker network create --driver overlay my-network
$ docker service create
--replicas 3 --network my-network --name my-web
nginx
$ docker service ls
Containers on the same network can access each other using
service discovery
12. Publish Service Ports
$ docker service create --name my_web --replicas 3 --publish
8080:80 nginx
Makes service accessible on port 8080 of every node
regardless if there is a container running for that service
on the node!
13. Swarm Inspect
$ docker service inspect nginx
● Provides JSON output
● Same as docker inspect for containers
● Should be run only on manager nodes
14. Swarm Update
● Update an existing service
$ docker service update --publish-add 6379:6379 redis
Publishes redis service to port 6379 on all nodes in Swarm
$ docker service update --image redis:latest redis
Let’s watch the output of “$ docker service ps redis”
Has many, many more options! Refer the docs.
15. Scale a service
$ docker service create --name redis redis:3.0.6
Creates a single container on a random node in the Swarm
$ docker service scale redis=2
Starts another container on a random node
16. ps, ls, rm,
● ps
○ List the tasks of a service
● ls
○ Lists the services in the swarm
● rm
○ Removes a service from the swarm