Docker by doing 4 - Docker swarm
Setting up dockerswarm (1 manager)
Initialize docker swarm
- Initialize the Docker swarm on manager node (SwarmServer1)
1
docker swarm init
- Copy the docker swarm join command that is displayed for the next step (e.g.
docker swarm join --token TOKEN IP_ADDRESS:2377
)
Add additional nodes to the swarm
- Add worker nodes to the swarm (SwarmServer2 and SwarmServer3) using the command retrieved from the init
1
docker swarm join --token TOKEN IP_ADDRESS:2377
Create a swarm service
- From the manager node (SwarmServer1), create a service to test your swarm configuration
1
2
3docker service create --name weather-app --publish published=80,target=3000 --replicas=3 weather-app
# verify it
docker service ls - Paste one of the public IP addresses of our worker nodes in a browser to view the weather app is also showing on the ‘worker nodes’
Backing up and restoring dockerswarm
Join the worker nodes to the swarm
- Follow the previous section “initilize and add”, so you get the manager and 2 workers up and running
- View the running services on swarm manager node
1
2
3
4
5docker service ls
# check backup service is there, we will scale it
docker service scale backup=3
# check by showing the service across our nodes
docker service ps backup
Back up the swarm manager
- On the manager node:
1
2
3
4# stop service before
systemctl stop docker
# back up
tar czvf swarm.tgz /var/lib/docker/swarm/
Restore the swarm on the backup manager
- Copy the swarm backup from the manager node to the backup manager
1
2# from manager node terminal
scp swarm.tgz cloud_user@BACKUP_IP_ADDRESS:/home/cloud_user/ - Extract the backup file on backup server (4th server)
1
2
3
4
5
6
7
8
9
10cd /home/cloud_user
tar xzvf swarm.tgz
# copy the swarm directory
/var/lib/docker/swarm
cp -rf swarm/ /var/lib/docker/
# restart the service
systemctl restart docker
# reinitialize the swarm
docker swarm init --force-new-cluster
# copy the swarm join command
Add the worker nodes to the restored cluster
- The 2 workers need to leave the first swarm and join the second one
1
2
3docker swarm leave
# do not forget it should have the backup IP address
docker swarm join --token $NEW_JOIN_TOKEN_HERE
Distribute the replicas across the swarm
- Check backup server is up and running
1
2
3
4docker service ls
# check the service list
docker service ps backup
# you should see 3 replicas on backup manager - Make sure our service is distributed across our swarm
1
2
3
4
5
6
7# scale things down to 1
docker service scale backup=1
# back up to 3
docker service scale backup=3
#check service again
docker service ps backup
# service should be up and running on all 3 nodes
Scaling a docker swarm service (2 managers)
Create a swarm
- Create a swarm manager (SwarmManager1)
1
2docker swarm init
# copy the join command
Add worker nodes
- To each worker (e.g. 3 workers)
1
docker swarm join --token <TOKEN> <IP_ADDRESS:2377>
The Manager Token
- Generate the manager token on SwamManager1 node
1
2docker swarm join-token manager
# This command is for when we want a manager node to join - Add a second swam manager server SwamManager2
1
docker swarm join --token <TOKEN> <IP_ADDRESS:2377>
- Check our work (from SwarmManager1)
1
2
3
4
5
6
7
8
9docker node ls
# 5 nodes running
# 3 workers
# 1 MANAGER STATUS: leader (SwarmManager1)
# 1 MANAGER STATUS: Reachable (SwarmManager2)
## ensure the managers are ONLY managers
docker node update --availability drain <MANAGER1 ID>
docker node update --availability drain <MANAGER2 ID>
Create a service
- Create a service, and replicate it three times (image httpd from DockerHub)
1
docker service create --name httpd -p 80:80 --replicas 3 httpd
- Check what is running (from SwarmManager1)
1
2docker service ps httpd
# shows 3 replicas, 1 on each worker
Scale a service up and down
- Scale the httpd service up to 5
1
2
3docker service scale httpd=5
docker service ps httpd
# check there are 5 now, managers are only managers - Scale the httpd Service back down to 2 Nodes
1
2
3docker service scale httpd=2
docker service ps httpd
# check there are 2 now, managers are only managers