How to build a Kubernetes cluster on HETZNER with k3s

Published 28.03.2021

Author Fabian Peter

Categories Engineering

HETZNER is a german cloud provider having very competitive prices for linux-based VMs. It’s a great place to run Kubernetes, as HETZNER provides many integrations with their systems we can leverage when running Kubernetes:

  • CSI Integration
  • Terraform integration
  • Loadbalancers

At p3r we’re building many tools for Kubernetes – one of them is apollo, which we will use to build a k3s cluster with hcloud Volumes as our default StorageClass using the hcloud csi-driver.

Provision the VMs

First, create 3 Ubuntu 20.04 VMs at HETZNER with a size that fits your needs (CX31 is our default), name them master-0, node-0 and node-1 and add your SSH key to them.

Download apollo

You need git, python3 and pip3 for the following steps.

git clone https://gitlab.com/p3r.one/apollo
cd apollo
pip3 install -r requirements.txt
mkdir -p inventory/hcloud

Configure your Kubernetes cluster

Add the following configuration to inventory/hcloud/hosts.yml:

all:
  hosts:
    master-0:
      ansible_host: IP_OF_MASTER0
    node-0:
      ansible_host: IP_OF_NODE0
    node-1:
      ansible_host: IP_OF_NODE1
  children:
    master:
      hosts:
        master-0:
    node:
      hosts:
        node-0:        
        node-1:
    k3s_cluster:
      children:
        master:
        node:

Add the following configuration to inventory/hcloud/vars.yml:

ansible_ssh_user: root
csi:
  provider: hcloud

Set your Hetzner API key (needed for the CSI integration):

export HCLOUD_TOKEN=MYHCLOUDTOKEN

Install your cluster

Run the following command to setup your k3s cluster:

ansible-playbook apollo.yml -e @inventory/hcloud/vars.yml -i inventory/hcloud/hosts.yml --flush-cache

The kubeconfig for your new cluster can be found in inventory/kubeconfig.yml. You can use Lens to work with your new Kubernetes cluster.

Join 100+ cloud native enthusiasts

and stay in the loop on modern software development.

Sign up to receive exclusive content around cloud native software development right into your inbox.

We don’t spam! Read our privacy policy for more info.

More stories from our blog

What’s new in Kubernetes v1.21.2?

What’s new in Kubernetes v1.21.2?

It's June, and Kubernetes has released a new update with version 1.21.2. We will have a look in brief at the changes that came along with this update. We will also have a look at the bugs that Kubernetes removed ahead with the few things added. Let's roll. Changes...

Chaos Engineering: Not so Chaotic

Chaos Engineering: Not so Chaotic

It feels very complex when we talk a lot about cloud computing and developer operations. Furthermore, certain things look complicated, but they are not so if we easily understand those concepts. Today, we will discuss such a thing that sounds complex but is simple and...

On Charming Engineering Culture: My Notes

On Charming Engineering Culture: My Notes

Engineering teams are at the core of any modern organisation. They break/make an organisation, and empowering them is critical to any modern companies’ success. A motivated engineer brings more value than a ‘whatever’ engineer. Its high time managers and leaders focus...

Observability: Your Eyes in Cloud

Observability: Your Eyes in Cloud

Observability is all around the cloud. You might come across the term while exploring the vast stretches of documentations or blog posts, maybe videos or streams too. Well, from far you might have seen that this is a very broad term, and it’s expected. The topic is...

Cloud Firewalls Simplified: Beginners  Edition

Cloud Firewalls Simplified: Beginners Edition

Cloud technology is everywhere. From your photos to big corporations carrying out their day to day operations. But have you ever thought about the security needed to protect this vast pile of data? Security from external attacks by threat detection and elimination is...

Object and Block Storage: How They Differ?

Object and Block Storage: How They Differ?

The difference between block and file storage makes heads spin due to the complexity of definitions and technical jargon across the internet. Even a technical person sometimes forgets the business value and makes decision fatigue their best friend when trying to...

Helm: Why DevOps Engineers Love it?

Helm: Why DevOps Engineers Love it?

Kubernetes doesn’t have reproducibility built-in. At least, that’s what we hear most people complain as a cloud native consultation firm serving both startups and enterprises. I have been using Kubernetes for a while now, and it stands up to the mark of being a gold...

Interested in what we do? Looking for help? Wanna talk about software strategy?