Backing Up and Restoring a Docker Swarm

Hands-On Lab

 

Photo of Travis Thomsen

Travis Thomsen

Course Development Director in Content

Length

01:30:00

Difficulty

Intermediate

For the last six months, the Acme Anvil Corporation has been migrating some of their bare metal infrastructure to Docker containers. A schism has developed between the members of your team on whether to use Docker Swarm or Kubernetes. To settle the dispute, your manager has decided to create a series of challenges. You have been tasked with creating a demo on how to back up and restore a Docker swarm. You are to set up a Docker swarm with 3 nodes, scale the backup service up to 3 nodes, and back up your master node and restore it to a backup instance.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Backing Up and Restoring a Docker Swarm

Introduction

For the last six months, the Acme Anvil Corporation has been migrating some of their bare metal infrastructure to Docker containers. A schism has developed between the members of your team on whether to use Docker Swarm or Kubernetes. To settle the dispute, your manager has decided to create a series of challenges. You have been tasked with creating a demo on how to back up and restore a Docker swarm. You are to set up a Docker swarm with 3 nodes, scale the backup service up to 3 nodes, and back up your master node and restore it to a backup instance.

Instructions

In this lab, you are tasked with backing up and restoring a Docker swarm.

  1. Use cat on the file swarm-token.txt to show the join token for your worker nodes.
  2. Add 2 worker nodes to the swarm.
  3. Scale the backup service up to 3 replicas.
  4. Stop the Docker service on the master node and use tar to compress the swarm directory.
  5. Use scp to securely copy the swarm tarball to /home/cloud_user on the backup master node.
  6. Use tar to extract the tarball.
  7. Stop the Docker service, restore the swarm directory, and start up the Docker service.
  8. Force the swarm to reinitialize a new cluster.
  9. Add the two worker nodes to the swarm.

Solution

Begin by logging in to all lab servers using the credentials provided on the hands-on lab page:

ssh cloud_user@PUBLIC_IP_ADDRESS

Become the root user:

sudo su -

Join the worker nodes to the swarm

On Swarm Server 1

  1. View the docker swarm join command:

    cat swarm-token.txt

    Copy the docker swarm join command provided in this output.

  2. Run the command from the previous step on Swarm Server 2 and Swarm Server 3.

  3. View the running services:

    docker service ls
  4. Scale the backup service up to 3 nodes:

    docker service scale backup=3
  5. Show the service across our nodes:

    docker service ps backup

Back up the swarm master

  1. Stop the docker service on the master node.

    systemctl stop docker
  2. Back up the /var/lib/docker/swarm/ directory.

    tar czvf swarm.tgz /var/lib/docker/swarm/

Restore the swarm on the backup master

  1. Copy the swarm backup from the master node to the backup master:

    > Note: Be sure to replace BACKUP_IP_ADDRESS with the private IP address of the Backup server.

    scp swarm.tar.tgz cloud_user@BACKUP_IP_ADDRESS:/home/cloud_user/
  2. From the Backup Swarm Server, extract the backup file:

    tar xzvf swarm.tar.tgz
  3. Copy the swarm directory to /var/lib/docker/swarm:

    cd /var/lib/docker
    cp -rf swarm/ /var/lib/docker/
  4. Restart the Docker service:

    systemctl restart docker
  5. Reinitialize the swarm:

    docker swarm init --force-new-cluster

    Copy the docker swarm join command from this output.

Add the worker nodes to the restored cluster

On Swarm Server 2 and Swarm Server 3

  1. Remove each node from the old swarm:

    docker swarm leave
  2. Add each node to the backup swarm:

    > Note: This will be the command that was copied from the previous task. You will need to replace the IP address with the private IP address of the Backup Swarm Server.

    docker swarm join --token TOKEN IP_ADDRESS:2377

Distribute the replicas across the swarm

On Backup Swarm Server

  1. Show the current service list:

    docker service ps backup
  2. Scale the replicas down to 1:

    docker service scale backup=1
  3. Next, scale the replicas up to 3 to distribute the replicas across the swarm:

    docker service scale backup=3

Conclusion

Congratulations, you've completed this hands-on lab!