Building Cloud Apps with Civo and Docker Part I: Setting Up the Cluster

Written by: Lee Sylvester

This article was originally published by Lee Sylvester on his personal blog. With his kind permission, we’re sharing it here for Codeship readers.

As a beta tester for the cloud platform by Civo, a UK-based company providing services similar to Digital Ocean, I wanted to give their servers a hammering with my cloud experiments and decided it would be a good idea to document what I know.

This post, therefore, is the first of three articles outlining how to create a distributed application load balanced on the Civo platform.

You’ll start with provisioning servers running the Docker engine and linking them together to form a network ready for deploying your application services. Then, in later articles, I will walk you through creating your application, deploying it and working with more difficult concepts, such as distributing application state and static resources.

Setting Up Your Civo Account

Civo provides a very easy to use web-based dashboard which makes server instantiation very simple. Before you can do this, however, you will first need to set up your Civo account; the most important element of this is your public encryption key, which will be used to authenticate your development machine with your provisioned servers. Doing this will make it much easier to work with your servers once they’re up.

I won’t go into detail on how to do this, as Civo has already done the hard work here.

Creating the Server Instances on Civo

Once your account is set up, you will then need to create your servers. To do this, simply log into Civo and navigate to Dashboard > Instances, then click Create Instance. You should be presented with the page below.

For cloud applications, it’s best to have at least three servers. The smallest size instance should be sufficient to start, and you can always increase these in time, once you are happy with the setup.

When running Docker, I prefer to use an Ubuntu image, mostly because SELinux flavors, such as CentOS, often provide headaches when dealing with some Docker images which utilize known techniques, such as Docker-in-Docker. The restricted authentication system provided in SELinux distros can become a nuisance. Therefore, while experimenting, I would recommend Ubuntu. There is nothing to stop you changing your mind further down the road, once you know what you are doing.

With this in mind, go ahead and create three £5 Ubuntu instances, using the root user and remembering to select your uploaded SSH key for each instance. I named mine cloud-manager, cloud-worker1, and cloud-worker2.

Setting Up Docker

Now that you have your instances, go back to Instances on the Dashboard and note down the IP addresses of each server. You’ll need these in order to configure Docker on your local machine.

If you do not have Docker installed locally, go ahead and do that now. You can find instructions on how to do that here.

Now, bring up your devices Terminal window. This will be Cmd on Windows, Terminal on MacOS, and probably Bash on Linux (and if it isn’t, you clearly know what you’re doing enough to figure it out).

With the Terminal open, enter the following, replacing the IP and name of your instance where applicable:

docker-machine create --driver generic --generic-ip-address --generic-ssh-user root --engine-install-url= cloud-manager

This will install Docker onto your new server. The --engine-install-url flag may not be required. However, at the time of writing, the latest build of Docker Engine was a little buggy and corrupted when installed remotely. Therefore, providing this flag as shown above ensures that a slightly older version of Docker is used for the install, which doesn’t fail.

It may take a little while, but after some time, you should see some text output, such as the following:

Running pre-create checks...
Creating machine...
(cloud-manager) No SSH key specified. Assuming an existing key at the default location.
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with ubuntu(systemd)...
Installing Docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env cloud-manager

Note that the name used as the last parameter of this execution is the name you will call this specific server from now on when sending it requests. Therefore, be sure to call it something you’ll easily remember.

Repeat this step for each of your server instances, replacing the appropriate values for each server.

Linking the Server Instances

Your servers are now Docker ready and are neatly configured on your local machine for direct access. However, none of the servers are aware of each other. You’ll fix that with this next step.

Linking servers is necessary as you’ll normally only ever want to communicate with a single server; the controller. If you remember, I said I named my instances cloud-manager, cloud-worker1, and cloud-worker2. Therefore, I normally send all my commands to cloud-manager and have that server direct the other two as appropriate. It doesn’t matter to me how it does this, so long as I know that it will.

To link your servers, you will first need to SSH into your controller/manager server. Since you have already remotely installed Docker to your instances, this is actually pretty easy and is done by simply entering:

docker-machine ssh cloud-manager

After a few seconds, you should be presented with the command prompt of the remote instance.

Next, you need to tell this instance that it is the controller/manager of a Docker cluster or swarm. You do this by initializing the server using:

docker swarm init

This will output the command that needs to be run on the other two server instances, such as:

docker swarm join \
    --token SMWTNG-1-3rthbtaxer51v3[snip]6zg1rydadi \

Go ahead and exit the controller server and SSH into each of the other servers, executing the join code you were given on each one. The execution should present you with the message This node joined a swarm as a worker.

Congratulations! You now have a Cloud ready server cluster on the Civo platform.

That’s all I’m going to cover in this article. In the next article, you’ll see how to utilise Docker Compose files to deploy a simple application to your cloud.

Stay up to date

We'll never share your email address and you can opt out at any time, we promise.