Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Docker Swarm With Docker Machine and Scripts

DZone's Guide to

Docker Swarm With Docker Machine and Scripts

Here are some handy scripts to keep around when running Docker's Swarm Mode in a local environment, including scripts for swarm creation and cleanup.

· Cloud Zone ·
Free Resource

See why enterprise app developers love Cloud Foundry. Download the 2018 User Survey for a snapshot of Cloud Foundry users’ deployments and productivity.

Docker Swarm and Docker Machine allow us to create a cluster to publish our services. This article will show you some scripts to help to create a cluster in a rapid and easy, way taking into account the concepts associated to Docker Swarm.

Requirements

Have Docker and Docker Machine installed.

Environment Settings

$ docker version
Client:
 Version:      17.09.0-ce
 API version:  1.32
 Go version:   go1.8.3
 Git commit:   afdb6d4
 Built:        Tue Sep 26 22:42:18 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.09.0-ce
 API version:  1.32 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   afdb6d4
 Built:        Tue Sep 26 22:40:56 2017
 OS/Arch:      linux/amd64
 Experimental: false


$ docker-machine version
docker-machine version 0.12.2, build 9371605


Source Code

The source code used in this article is published on GitHub.

Create Swarm

The swarm will be composed by:

  • 3 nodes with the manager role.
  • 3 nodes with the worker role.

The following steps were taken to create the swarm:

  • Create the nodes.
  • Initialize swarm mode in the first node created (node1).
  • Get the manager and worker tokens to include the others nodes.
  • Join manager nodes into the swarm.
  • Join worker nodes into the swarm.

The script used was the following:

#!/bin/bash

# Creating 6 nodes 
echo "### Creating nodes ..."
for c in {1..6} ; do docker-machine create -d virtualbox node$c
done

# Get IP from leader node
leader_ip=$(docker-machine ip node1)

# Init Docker Swarm mode
echo "### Initializing Swarm mode ..."
eval $(docker-machine env node1)
docker swarm init --advertise-addr $leader_ip

# Swarm tokens
manager_token=$(docker swarm join-token manager -q)
worker_token=$(docker swarm join-token worker -q)

# Joinig manager nodes
echo "### Joining manager modes ..."
for c in {2..3} ; do eval $(docker-machine env node$c)
    docker swarm join --token $manager_token $leader_ip:2377
done

# Join worker nodes
echo "### Joining worker modes ..."
for c in {4..6} ; do eval $(docker-machine env node$c)
    docker swarm join --token $worker_token $leader_ip:2377
done

# Clean Docker client environment
echo "### Cleaning Docker client environment ..."
eval $(docker-machine env -u)


If you had cloned the repository, you can use the following command inside the root folder:

$ ./scripts/swarm-create.sh

Start Swarm Nodes

The configuration suggested in this article is used in our local machine, so it is possible that you need to turn off the machine sometimes and continue the tests later. When you turn on your machine to continue, you should start each node, one by one. This routine can be tedious.

To avoid spending time on these activities, I suggest you the use of the following script:

#!/bin/bash

# Start nodes
echo "### Starting nodes ..."
for c in {1..6} ; do docker-machine start node$c
done

# Clean Docker client environment
echo "### Cleaning Docker client environment ..."
eval $(docker-machine env -u)


If you had cloned the repository, you can use the following command inside the root folder:

$ ./scripts/swarm-start.sh

Remove Swarm

During swarm testing, we can get to the point where we made some mistakes because of the lack of knowledge of the commands. This situation won't be a problem because everything is happening in a local environment. You can always remove the swarm completely and start again in a clean configuration.

To remove the swarm, you can use the following script:

#!/bin/bash

# Clean Docker client environment
echo "### Cleaning Docker client environment ..."
eval $(docker-machine env -u)

# Remove nodes
echo "### Removing nodes ..."
for c in {1..6} ; do docker-machine rm node$c --force
done


If you had cloned the repository, you can use the following command inside the root folder:

$ ./scripts/swarm-remove.sh

Naming Nodes Into the Swarm

Docker allows us to modify the node role using the commands promote and demote.

Because we are doing tests locally, you will want to realize role changes many times. With that in mind, I recommend you do not set node names like manager-1 or manager-2 because the role could be changed at any time. Instead of these names, you should use names like node1, node2 …. nodeN to avoid confusion.

If you desire to test this functionality, you should use the following commands:

  • $ docker node promote NODE: Promote a node to manager in the swarm.
  • $ docker node demote NODE: Demote a node to a worker in the swarm.

There are multiple articles that talk about the topic of how to name nodes in your infrastructure. If you want to know about this related topic, read up on the phrase Cattle vs. Pets.

Cloud Foundry saves app developers $100K and 10 weeks on average per development cycle. Download the 2018 User Survey for a snapshot of Cloud Foundry users’ deployments and productivity. Find out what people love about the industry standard cloud application platform.

Topics:
docker machine ,docker swarm ,cloud ,scripts ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}