Replicating our microservice

Picking up from the previous chapter, we ended with a Docker Compose configuration that starts our two containers, one with our microservice and the other with the database server that stores our data:

You may have noticed when deploying with Docker Compose that our two containers have a _1 suffix on the name. This is because Docker Compose supports replicating our instance using Docker swarm.

Docker swarm is another component of the Docker engine that enables you to create a cluster of hosts to run your microservice instances while orchestrating all of this using a simple tool from any of the hosts in your cluster.

Swarm is the name Docker uses to reference a cluster of nodes. Let's create a new Swarm using our host as the manager of the swarm. Since I'm using Docker for Mac, I need to create another Docker virtual machine to be able to simulate a couple of hosts. If you're using a Linux machine, you can just test this with two different hosts.

If you're using macOS like me, follow these steps. We'll use Docker machine to create two virtual machines to run as Docker hosts. First, let's see what machines we have created by typing:

docker-machine ls

You should see an output as follows:

No machines. Let's create a manager node. To do that, run:

docker-machine create manager

Docker will create the new machine, assign a new IP address, and prepare the Docker engine inside the machine:

Now, run the following command:

docker-machine ls

You'll see that our new machine is ready to be used. You can see the IP address of the machine in the URL column:

The Docker engine has an exposed API that enables you to manage a Docker host using the Docker commands from another host. The URL column indicates the API address to manage that specific host.

We can use a little helper command to configure our Docker command to manage this new host. Just type the following command:

 docker-machine env manager 

And follow the instructions:

Now, let's keeps this Terminal untouched and open a new tab so that we can create a second machine. Let's call it replica, which will hold a second instance of our microservice:

We can check that our new virtual machine is ready and running by checking the following output once more:

docker-machine ls

Both of our machines should be ready and running:

Using our initial tab, where we have the manager host, let's create a swarm. We're indicating the advertisement address because our virtual machines have more addresses, but we want the advertisement to be done on the network shown previously, so we're pointing out a specific IP address at the end:

Docker created our swarm and gave us instructions on how to make other hosts join the swarm using a kind of secret token. If you want to add more nodes later on, save that command.

Now, head to the second tab and let's change our Docker command to manage our replica host and then join the swarm:

We now have two nodes on our swarm. From this point forward, assume that the first tab of our console is the manager host and that the second tab is our replica host.

We can check that the swarm has our two active nodes by using the docker node ls command from our manager:

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

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