Docker Swarm

Now that we have discussed how to launch individual Docker instances using Docker Machine, let's get a little more adventurous and create a cluster of instances. To do this, Docker ships a tool called Swarm. When deployed, it acts as a scheduler between your Docker client and host Docker instances, deciding where to launch containers based on scheduling rules.

Creating a local cluster

To start off, we are going to be using Docker Machine to create a cluster locally using VirtualBox (https://www.virtualbox.org), which is bundled with Docker Toolbox. To start, we are going to launch a VM to generate a discovery token. To do this, run the following commands:

docker-machine create -d virtualbox discover

Then configure your Docker client to use the newly created local instance:

eval "$(docker-machine env discover)"

You can check that your Docker client is configured to use the discover instance by running docker-machine ls and making sure that discover has a star in the active column.

Finally, you can install the discovery service by running the following command:

docker run swarm create

This will download and run the discovery service and generate the token. At the end of the process, you will be given a token; it is important that you keep a note of this for the next steps. If everything went as planned, you should see something similar to the following output:

Creating a local cluster

In the preceding example, the token is 40c3bf4866eed5ad14ade6633fc4cefc. Now that we have our token, we need to launch an instance that will act as the scheduler, this is know as a Swarm manager.

To do this, enter the following command, making sure that you replace the token with the one you generated:

docker-machine create 
    -d virtualbox 
    --swarm 
    --swarm-master 
    --swarm-discovery token://40c3bf4866eed5ad14ade6633fc4cefc 
    swarm-master

Now that we have the Swarm manager VM up and running, we can start launching VMs that act as nodes within the cluster. Again, using the discovery token, run the following commands to launch two nodes:

docker-machine create 
    -d virtualbox 
    --swarm 
    --swarm-discovery token://40c3bf4866eed5ad14ade6633fc4cefc 
    swarm-node-01

Then launch the second node using the following command:

docker-machine create 
    -d virtualbox 
    --swarm 
    --swarm-discovery token://40c3bf4866eed5ad14ade6633fc4cefc 
    swarm-node-02

We can check our VMs by running the docker-machine ls command and then switch our Docker client to use the cluster by running the following command:

eval $(docker-machine env --swarm swarm-master)

Now that your Docker client is communicating with the cluster, you can run docker info to find information about all the nodes and the cluster itself, you will see something similar to the following screenshot:

Creating a local cluster

So, now we have a three CPU, 3-GB cluster running over three nodes. To test it, let's run the Hello World container and then run docker ps -a so that we can see which node the container launched on:

Creating a local cluster

As you can see from the terminal output, the container was launched on swarm-node-01, running the container again should launch it on our second node:

Creating a local cluster

So there you have it, a really basic Docker Swarm cluster that you can launch your containers into using your local Docker client, all launched a managed using Docker Machine.

Before we move onto the next section, we should remove the local cluster. To do this, just run the following command:

docker-machine rm discover swarm-master swarm-node-01 swarm-node-02

Click on yes when prompted. You can then check whether the VMs have been terminated by running the docker-machine ls command.

Creating a Remote Cluster

Before we move onto looking at the next tool, let's launch a cluster in the cloud. I am going to be using DigitalOcean for this.

First of all, let's create a new discovery token. As all we need to do is generate a discovery token, there is no need to launch an instance in DigitalOcean just for this task, so we will simply bring up a machine locally, make a note of the discovery token and then remove it:

docker-machine create -d virtualbox token
eval "$(docker-machine env token)"
docker run swarm create
docker-machine rm token

Now that we have our discovery token, let's launch our Swarm cluster in DigitalOcean, first of all we will look into Swarm manager:

docker-machine create 
    --driver digitalocean 
    --digitalocean-access-token sdnjkjdfgkjb345kjdgljknqwetkjwhgoih314rjkwergoiyu34rjkherglkhrg0 
    --digitalocean-region lon1 
    --swarm 
    --swarm-master 
    --swarm-discovery token://453sdfjbnfvlknmn3435mwedvmndvnwe 
    swarm-master

Then the we will use the two nodes:

docker-machine create 
    --driver digitalocean 
    --digitalocean-access-token sdnjkjdfgkjb345kjdgljknqwetkjwhgoih314rjkwergoiyu34rjkherglkhrg0 
    --digitalocean-region lon1 
    --digitalocean-size 1gb 
    --swarm 
    --swarm-discovery token://453sdfjbnfvlknmn3435mwedvmndvnwe 
    swarm-node-01

docker-machine create 
    --driver digitalocean 
    --digitalocean-access-token sdnjkjdfgkjb345kjdgljknqwetkjwhgoih314rjkwergoiyu34rjkherglkhrg0 
    --digitalocean-region lon1 
    --digitalocean-size 1gb 
    --swarm 
    --swarm-discovery token://453sdfjbnfvlknmn3435mwedvmndvnwe 
    swarm-node-02

As you can see in the following screenshot, I launched the cluster in DigitalOcean's London datacenter and gave the two nodes additional resources:

Creating a Remote Cluster

We will configure our local Docker client to use the remote cluster using the following command:

eval $(docker-machine env --swarm swarm-master)

This will give us the following information:

Creating a Remote Cluster

We are going to be using this cluster for the next part of this chapter, so try to keep it running for now. If you can't, then you can remove the cluster by running the following command:

docker-machine rm swarm-master swarm-node-01 swarm-node-02

You should also double the DigitalOcean control panel to ensure that your instances have terminated correctly.

Note

Remember that with public cloud services, you are paying for that you use, so if you have an instance sat powered on, even if it is an errored state, with Docker Machine, the meter is running and you will be incurring cost.

Discovery backends

At this point, it is worth pointing out that Docker allows you to swap out the Discovery backends, at the moment we are using the default one which the Hosted Discovery with Docker Hub, which isn't recommend for production.

Swarm supports the following discovery services:

For the time being, we are just going to be looking at the tools Docker provides rather than any third-party options, so we are going to stick to the default Discovery backend.

Unfortunately, the one thing that the default Discovery backend doesn't give you is high availability, this means that our Swarm manager is a single point of failure. For our needs, this isn't a problem; however, I would not recommend running this configuration in production.

For more information on the different discovery backends and high availability with Swarm, refer to the following URLs:

We are going to be looking a lot more at schedulers in later chapters, so for now, let's move onto the final service installed by Docker Toolbox.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset