0
0
Dockerdevops~5 mins

Manager and worker nodes in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
When running multiple Docker hosts together, you need a way to control and organize them. Manager nodes control the cluster and assign tasks, while worker nodes run the tasks. This setup helps run apps reliably across many machines.
When you want to run a web app on several servers to handle more users without downtime
When you need to update your app without stopping all servers at once
When you want to balance work evenly across multiple computers
When you want to add or remove servers easily without breaking your app
When you want automatic recovery if one server fails
Commands
This command starts a new swarm cluster and makes this machine the manager node. The advertise address is the IP other nodes use to connect.
Terminal
docker swarm init --advertise-addr 192.168.1.100
Expected OutputExpected
Swarm initialized: current node (abcdef123456) is now a manager. To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-0abcdef1234567890 192.168.1.100:2377
--advertise-addr - Sets the IP address that other nodes use to connect to this manager
Run this command on a worker node to join the swarm cluster controlled by the manager at 192.168.1.100.
Terminal
docker swarm join --token SWMTKN-1-0abcdef1234567890 192.168.1.100:2377
Expected OutputExpected
This node joined a swarm as a worker.
--token - The secret token that allows this node to join the swarm as a worker
Run this on the manager node to see all nodes in the swarm and their status.
Terminal
docker node ls
Expected OutputExpected
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION abcdef1234567890abcdef1234 manager-node Ready Active Leader 20.10.12 1234567890abcdef1234567890 worker-node1 Ready Active 20.10.12
Key Concept

Manager nodes control the swarm and assign tasks, while worker nodes only run the tasks assigned to them.

Common Mistakes
Trying to join a worker node without using the correct join token
The swarm will reject the node because the token is like a password to join the cluster.
Always use the exact join token provided by the manager node when running 'docker swarm join'.
Running 'docker node ls' on a worker node
Only manager nodes can list all nodes; workers do not have permission to see the cluster state.
Run 'docker node ls' only on a manager node.
Summary
Initialize a swarm on one machine to make it the manager node.
Join other machines as worker nodes using the join token from the manager.
Use 'docker node ls' on the manager to see all nodes and their status.