What do you do when you want to be able to deploy efficiently and intutively as many websites as possible, as many times as you want, using any language or technology, and without making a mess?

I have a couple of websites online and plan to make a several more, both personal and for clients. Being able to deploy those websites without having to deal with all the details of installation and management is a requirement if I don’t want to lose my sanity. Also I should be able to scale those websites when needed.

When I started with this idea, first thought about setting up a single server with several websites running behind a proxy like Nginx. It didn’t take too long before I realized that it was going to be a mess, and that eventually it wasn’t going to be practical to update those websites very often or scale them if necessary, unless I worked really hard on the set up.

But soon I saw this thing called Docker that made possible to create something like tiny and isolated virtual computers called containers (they are more lightweight than regular VMs) that could run inside a real server. These containers can keep our servers clean from any packages required for our websites to run, also they make easy to create specific environments as often as we want, anywhere we want. With this, there are also tools for automating the management of containers and their hosts, probably the most famous one was created by Google and it’s called Kubernetes.

I’m not going to lie though, getting your head around those things can be a little overwelming. They solve a lot of problems like the mentioned at the beginning, but making the jump isn’t something you usually do inmediately.

Fortunatelly, I discovered this company called Rancher Labs that has a project to make all this so much easier and intuitive. Rancher basically handles everything you might initially need inside a UI.

Services running in Rancher

And also allows you to manage multiple server without worrying about details of server management. Just add a Host and let it run.

Hosts running in Rancher

The following is a simple guide to set up a two-server cluster like the one I have for my personal projects, which is running this blog right now.

Requirements

The requirements will almost completely depent on your needs. But the set up that I’m using is composed by two Ubuntu 17.04 servers with 2GB or RAM and 50GB of HDD. Also, because we will access to the Rancher UI using HTTPS, we need a valid domain name that points to the IP Address of the server that will run it, the SSL certificate is provided by Let’s Encrypt.

Don’t worry about manually getting the certificates, we will use a server called Caddy as a proxy that provides HTTPS support without touching anything.

This guide assumes that you have root access to the two servers via SSH.

Installing Docker

Depending on your provider, you might need to manually install Docker in your servers. In my case, I’m using two simple Ubuntu servers, so I have to install Docker by myself. Fortunatelly, Rancher provides some scripts that can help you in this step.

You can find a list of compatible versions of docker and their corresponding script in the Rancher documentation site.

I have installed Docker 17.09 with the following command:

curl https://releases.rancher.com/install-docker/17.09.sh | sh

Note: All the servers must have Docker installed.

Installing Rancher

Our container management platform is a container itself, how nice is that. So we don’t have to worry about making a mess in our server while we test things. If something goes wrong just delete the container and create a new one.

Running the following command creates a new container called rancher-server using a stable version of the rancher/server image. It’s recommended by Rancher to set the --restart option as unless-stopped which Restart[s] the container unless it is explicitly stopped or Docker itself is stopped or restarted, as noted by the Docker documentation site.

sudo docker run -d --restart=unless-stopped --name=rancher-server rancher/server:stable

Note that the Rancher UI can’t be accessed outside of the server (entering the server IP in the browser won’t work); it can only be accesed by other linked container, which is our proxy. So you need to complete the next step before you can see something.

Setting up Caddy as a Reverse Proxy

Caddy makes extremely easy to set up a server with built-in HTTPS support using Let’s Encrypt.

Also, unlike other servers like Nginx or Apache, Caddy requires very little configuration. Before starting this service, we have to create the settings file called Caddyfile in $HOME$ with some simple rules:

example.domain.com {
    tls your@email.com
    proxy / rancher-server:8080 {
        transparent
        websocket
    }
    log stdout
}

The block indicates the domain where we can access our Rancher installation, and inside we find the email used for the SSL certificate, the proxy setting that redirects traffic to rancher-server—which is the name of our Rancher container—in port 8080 (by default).

It’s important to set the two setting presets: transparent which Passes thru host information from the original request as most backend apps would expect and websocket which indicates this proxy is forwarding WebSocket connections. Otherwise you will get errors.

Once you have that file saved, executing this command will start the server, ready to take requests.

docker run -d -v $HOME/Caddyfile:/etc/Caddyfile -v $HOME/.caddy:/root/.caddy -p 80:80 -p 443:443 --link=rancher-server --name=caddy abiosoft/caddy

This starts a container called caddy using the image abiosoft/caddy. It also mounts two volumes inside the container, the first one which is our Caddyfile containing the server settings and the second one a directory .caddy that contains the SSL certificates. We also expose port 80 for HTTP (to be redirected to HTTPS) and 443 for HTTPS.

After the service has started, you can access to your domain name and it should show the UI of Rancher.

The last step is to add the other server as a host, this is where all the application containers are stored. It should automatically show a message at the top of the page, so just follow the instructions and you are ready to go.

What to do next

This guide only covers how to set up a web server cluster with Rancher, but not how to prepare our websites to be deployed on the cluster. This is for a future article.