How to create a Kubernetes cluster with Rancher on Hetzner

TL;DR: In 15 minutes you can have a lab cluster ready to test or to deploy your projects cheap and easily.

The 3 stars of our story

About Kubernetes, Rancher and Hetzner

Rancher is a kubernetes management tool. Its simplifies the cluster management. With a rich UI for web browser, we can setup a cluster within minutes. Thanks to docker-machine and its driver for the most important cloud providers (AWS, GCP, Azure, …) with just a click we can scale up our cluster.

Hetzner is an inexpensive cloud services provider. Based in Germany, it offers great VPS servers and very cheap prices. It’s ideal for setting up a Kubernetes Cluster Lab like the one we’ll create here.

Why Rancher?

Rancher’s UI allows you to easily create and manage a Kubernetes cluster without learning the Kubernentes API syntax.

Project management or the ability to do CI/CD with Rancher pipelines provide added value to the need to use Rancher to create your cluster.

Let us begin!

0. Prerequisites

Here you can view how to install it for you OS.

You need to know how to login with ssh and basic concepts of Docker.

1. Prepare your Hetzner Account and get API token

2. Install Rancher

First, we need to create a private network in Hetzner. We’ll put all our VMs there.

Click first on “Networks” and then “Create network” red button.

Hetzner’s create private network wizard

Now, we continue creating a small VM where we’ll install our Rancher’s server.

In our case, we use an CX21 instance (2 vCPUs / 4 GB) with Ubuntu 18.04. Don’t forget to check on your private network created before!

When the VM is up, we log in through SSH and install Docker there.

Prior to install Rancher Server, it’s recommended to attach a DNS entry for our server for a easy access.

We are ready to install Rancher as simple container.

Docker creates and runs a Rancher server accesible from HTTP and HTTPS ports, where the data is stored in /root/rancher and if we have assigned a DNS entry to our server’s IP, with “ — acme-domain” Rancher auto generates a Let’s encrypt SSL certificate.

Now we can log in into our Rancher’s server.

After that, enter in http://<YOUR-DOMAIN-FOR-RANCHER-SERVER> and complete the setup with your password. Please use a strong password!

It’s time to create a new cluster!

3. Install Hetzner Cloud driver

For that, we go to “Tools → Drivers”. Then select “Node drivers” tab.

Oh damn it! Where is Hetzner?

As you can see, there are many pre-loaded drivers for the main cloud providers: AWS, Azure, Digitalocean, …

But no Hetzner. Let’s go to add it. Click “Add node driver”.

We fulfill the dialog as follows

After pressing “Create”, the driver is downloaded and we can create a cluster in Hetzner. Well almost.

4. Cluster node template

Go to “Access”, select “API Tokens” tab and click “Generate API Token

List of enabled Hetzner API tokens

Enter a brief description like “Rancher Token” and press “Generate API token

Hetzner Generate API token dialog

Annotate the API Key and put in safe place. Hetzner will not show this token anymore.

5. Create the cluster

Hetzner driver ready to create our cluster!

In this screen we need to give a name to the cluster and specify a node template.

We push in “+” besides the “Choose a Template…

Add cluster in Hetzner screen

At this moment, Rancher asks us for the API Hetzner token.

Enter the Hetzner token dialog

Paste the Hetzner token and click “Configure Server”.

Node template dialog

Specify the properties for the nodes. In our case, we define two templates:

  • Ubuntu 18.04 with CX21 (2 vCPUS / 4 GB RAM) for master node (etcd, control panel)
  • Ubuntu 18.04 with CX31 (2 vCPUS / 8 GB RAM) for worker node (workload pods)

In both cases, select your private network and check “Use private network”.

After that, we can create a cluster as the following:

  • 1 Master node named “master” with CX21 template based.
  • 1 Worker node named “worker” with CX31 template based.

Press “Create” and the magic starts!

During the process, Rancher will notify you about the events. If you are logged into Hetzer, you can see how automatically the VM are created.

After several minutes… Congrats! Your cluster is up :)

Master & Comand Panel

6. Configuring your cluster

One place to control all your cluster

Click “Kubeconfig file” and then in “donwload”. Save the file into “~/.kube/config”. If you execute “kubectl version” in a terminal you see something similar to:

Kubectl works perfectly

If all went well it will show the local and cluster versions

Volume class driver

First we create secret where we put Hetzner API token again (replace YOURTOKEN with the real token)

Apply the secret with

kubectl apply -f <secret.yml>

Now we can install the CSI driver

kubectl apply -f https://raw.githubusercontent.com/hetznercloud/csi-driver/v1.2.3/deploy/kubernetes/hcloud-csi.yml

Done! Your cluster is ready to persist your data.

Native Load Balancer

(Soft) Load Balancer with MetalLB

In other services like as GCP, AWS or Azure, if you need an external IP for your service, the driver automatically creates and assigns a load balancer.

For situations like this, MetalLB was created.

MetalLB lets us to assign automatically an IP address for accessing a service from a manual defined “IP pool”.

The installation and setup is pretty straightforward.

  1. We install it using manifests

2. Get the IP (or IP range) to use (IE: 123.123.123.123) and apply a ConfigMap on the fly. In our case, get the worker’s external IP address.

3. Done!

Now every time you need a load balancer, Kubernetes automatically pick-up an IP address from the pool. In our case, it’ll be always the same.

Conclusion

You can continue adding new features like free SSL certificates with Let’s Encrypt.

Related posts

Please, if you liked it give me a round of applause. And if you want to know more about DevOps, Kubernetes, Docker, etc … follow me! KR

Some claps, pls? Thanks you! :)

CTO @ Digitalilusion.com & DigitalSecured.net Beyond-Full-stack developer #go #python #kubernetes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store