← Home

Setting up Kubernetes with Rancher on Hetzner

By Ian - Tue Feb 25 2020 - 10 min read

One of the big advantages of using Docker is the promise of an easy deployment process. And while this is definitely the case for smaller application with, for instance, docker compose, it's not really a production ready solution. Docker compose also doesn't have the (subjectively) very awesome autoscaling features and automatic rollbacks. Lots of organizations are choosing either a managed kubernetes solution by gcloud/Amazon, or choosing for a massive vendor lock in with Amazon's ECS or Fargate.

I'm going to show you a very easy way to launch your stack on kubernetes with a beautiful UI, and no vendor lock in using Rancher. And don't worry, it's a lot easier than you think!

In this tutorial we're just going to create a Kubernetes cluster with Rancher running on Hetzner servers, for a grand total of €5,- a month. No fancy stuff just yet. In another tutorial I'll show you how to run your services on the cluster, including a VPN and automatic SSL.

I went with Hetzner because they offer very affordable servers at a prime location for european users. You also pay by the minute with Hetzner. So following along with this tutorial is going to cost you in the range of cents.

Setting stuff up

Okay first off you're going to need a few things:

  • A hetzner account
  • ssh keys
  • A new Hetzner project.
  • Hetzner API Key (Sign in into the Hetzner Cloud Console choose a project, go to AccessTokens, and create a new token.)

Installing Rancher

  1. Add SSH key

    • Access → add ssh key ssh key screenshot
    • Use the following command to copy your ssh keys to clipboard: (it should end in something like "[user]@[machine]")

      cat ~/.ssh/id_rsa.pub | pbcopy
    • Paste your clipboard into the text field and you're all set.
  2. Add a server. (go to project → server → add server)

    • It doesn't really matter what kind of server. I'd recommend the smallest one in Falkenstein.
    • Be sure to select the SSH key we just added add ssh key to server
  3. SSH into your newly created server (you can find the IP address in the server tab of the Hetzner console):

    ssh root@[IP_ADDRESS]
  4. Setup Docker on your remote host.

    sudo apt-get update
    sudo apt-get install \
       apt-transport-https \
       ca-certificates \
       curl \
       gnupg-agent \
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    sudo add-apt-repository \
      "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
      $(lsb_release -cs) \
    sudo apt-get update
    sudo apt-get install docker-ce docker-ce-cli containerd.io
  5. You can do two things right now:

    1. Setup a domain and point it to your server IP or,
    2. Just continue with your server IP and don't bother with a domain.

    I would recommend setting up a domain if you're planning on running a production build based on this tutorial. Mainly because using a domain enables SSL on your Rancher WebUI. If you want to get through this tutorial and go for production later: just use the IP and figure out the domain once you're doing production level stuff.

  6. Run the Rancher image!

    • For no domain run:

      sudo docker run -d --restart=unless-stopped -p 80:80 -p 443:443 rancher/rancher
    • For rancher with domain run:

      sudo docker run -d --restart=unless-stopped -p 80:80 -p 443:443 rancher/rancher --acme-domain <YOUR_DOMAIN>
  7. Go to https://[YOUR-SERVER-IP OR YOUR-DOMAIN] and set a strong admin Password. It will start bitching about SSL if you use the IP. Just accept or setup a domain. Whatever, there are no rules.

Setting up Rancher to be able to provision Hetzner nodes

  1. In Rancher go to ToolsDriversAdd Node Driver

    KeyValueDownload URLhttps://github.com/JonasProgrammer/docker-machine-driver-hetzner/releases/download/2.1.0/docker-machine-driver-hetzner_2.1.0_linux_amd64.tar.gzCustom UI URLhttps://storage.googleapis.com/hcloud-rancher-v2-ui-driver/component.jsWhitelist Domainsstorage.googleapis.com
  2. Go to ClustersAdd Cluster in Rancher and you should see Hetzner as an infrastructure supplier.
  3. This should prompt the Add Cluster screen Create Cluster
  4. Click on Add Node Template and enter the API Token we created in the 'setting stuff up' step
  5. Create a template and name it by the type of instance you choose. Create Node Template
  6. Enter a cluster name, select etcd, Control Pane and Worker for your node and press create

    • It's not recommended to have one node manage the etcd, control pane and be a worker. This is because if you overload the cluster, the node will fail and the cluster will go down. It's better to separate the etcd + control pane from the worker.

Create Cluster

Awww yisss! You've just created a fully operational Kubernetes cluster on Rancher!🚀 You can manually scale instances up and down.

If you're already familliar with Kubernetes, you can find the .kube/config file under ClusterKubeconfig.

If you're not: don't worry! We're going to walk you through setting up some extra cool features (like a VPN) and launching your first application on the cluster later.