Creating services

You may have noticed how we're using the Docker service command to scale our instances. This is a command that is similar to the basic Docker command, but with services and scaling in mind. We can use this command to create containers and scale them easily.

MySQL has no simple replication mechanism. There are only two ways of doing it and both have disadvantages:

  • Creating a set of nodes as masters of each other, creating a complex mesh of connections that will eventually lead to chaos
  • Creating a cluster, which involves more nodes and still leads to a complex deployment, which is easier to manage but resource-intensive to maintain

Looking back at our examples from previous chapters, we could change our service to use another database server. More specifically, RethinkDB has a much friendlier cluster mechanism.

So, just replace our imagini service with the one we used with RethinkDB. This time, let's use the Docker Service commands manually and deploy our microservice step by step.

To begin with, let's prepare a RethinkDB cluster. To make it resilient, we will do the following:

  1. Create an instance called db-primary.
  2. Create another instance called db-secondary and instruct it to join db-primary.
  3. Scale db-secondary to two instances so that we have a proper three-node cluster.
  4. Remove and recreate db-primary to instruct it to join db-secondary this time.
  5. Scale db-primary to two instances.

This will give you four nodes, two of each kind, that are formatted to join the other kind when they fail, and swarm restarts them. This is the definition of no single point of failure.

Because we're doing everything manually, we need to start by creating a network. Let's create the network imagini:

docker network create --driver overlay imagini

Then, create db-primary with only 1 replica as we're removing it later on:

docker service create --name db-primary 
--network imagini
--replicas 1
rethinkdb:latest
rethinkdb --bind all --no-http-admin

Then, create the first db-secondary with only 1 replica and tell it to join db-primary:

docker service create --name db-secondary 
--network imagini
--replicas 1
rethinkdb:latest
rethinkdb --bind all --no-http-admin --join db-primary

You should now have something like this:

Similar to what we did before, let's scale db-secondary to two instances:

docker service scale db-secondary=2

Then, remove the db-primary and recreate it:

docker service create --name db-primary 
--network imagini
--replicas 1
rethinkdb:latest
rethinkdb --bind all --no-http-admin --join db-secondary

Then, scale it to two instances:

docker service scale db-primary=2

You should now have four instances. List the services and you should see something like the following screenshot:

If you try to stop the containers, you'll notice that they will restart without you doing anything. Just give it a little time and they'll be up and running again:

Now that we have our database cluster ready, we just need a proxy to be able to access the cluster:

docker service create --name database 
--network imagini
--publish 8080:8080
--publish 28015:28015
rethinkdb:latest
rethinkdb proxy --bind all --join db-primary

If we look at the services, we can now see that we have our four cluster nodes and a proxy called database, which we'll use to manage and access the data:

We can also see that we have the management port exposed, 8080. Head to the browser at any of the swarm addresses on that port and see if you can see the RethinkDB management interface:

Great! We can see that there's a 4 servers connected under the Servers label. On top, there's also a Servers section that you can look into and see that our instances are all there:

Use this interface and create a database called imagini in the Tables section.

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

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