Docker by doing 4 - Docker swarm

Setting up dockerswarm (1 manager)

Initialize docker swarm

  1. Initialize the Docker swarm on manager node (SwarmServer1)
    1
    docker swarm init
  2. 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

  1. 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

  1. From the manager node (SwarmServer1), create a service to test your swarm configuration
    1
    2
    3
    docker service create --name weather-app --publish published=80,target=3000 --replicas=3 weather-app
    # verify it
    docker service ls
  2. 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

  1. Follow the previous section “initilize and add”, so you get the manager and 2 workers up and running
  2. View the running services on swarm manager node
    1
    2
    3
    4
    5
    docker 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

  1. 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

  1. 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/
  2. Extract the backup file on backup server (4th server)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    cd /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

  1. The 2 workers need to leave the first swarm and join the second one
    1
    2
    3
    docker 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

  1. Check backup server is up and running
    1
    2
    3
    4
    docker service ls
    # check the service list
    docker service ps backup
    # you should see 3 replicas on backup manager
  2. 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

  1. Create a swarm manager (SwarmManager1)
    1
    2
    docker swarm init
    # copy the join command

Add worker nodes

  1. To each worker (e.g. 3 workers)
    1
    docker swarm join --token <TOKEN> <IP_ADDRESS:2377>

The Manager Token

  1. Generate the manager token on SwamManager1 node
    1
    2
    docker swarm join-token manager
    # This command is for when we want a manager node to join
  2. Add a second swam manager server SwamManager2
    1
    docker swarm join --token <TOKEN> <IP_ADDRESS:2377>
  3. Check our work (from SwarmManager1)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    docker 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

  1. Create a service, and replicate it three times (image httpd from DockerHub)
    1
    docker service create --name httpd -p 80:80 --replicas 3 httpd
  2. Check what is running (from SwarmManager1)
    1
    2
    docker service ps httpd
    # shows 3 replicas, 1 on each worker

Scale a service up and down

  1. Scale the httpd service up to 5
    1
    2
    3
    docker service scale httpd=5
    docker service ps httpd
    # check there are 5 now, managers are only managers
  2. Scale the httpd Service back down to 2 Nodes
    1
    2
    3
    docker service scale httpd=2
    docker service ps httpd
    # check there are 2 now, managers are only managers