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.

More stories from our blog

Unikernel Vs Container Vs VMs: Here is what you should use

Unikernel Vs Container Vs VMs: Here is what you should use

If you’d gone through Containers, Unikernels and VMs, I would bet you’re confused about which one to try for your new venture. It’s normal and happens to everyone while experimenting with adopting new technology. Remember the age-old dilemma of you thinking which...

The DevOps Roadmap: Docker

The DevOps Roadmap: Docker

The containerization revolution has just begun, which means you have heard about docker at least once in your professional life. Containerization has made our apps’ deployment cycle faster and efficient. Leading the containerization wave is docker, the most popular...

Why you should focus on enough instead of more?

Why you should focus on enough instead of more?

Time is a precious commodity, and you might have heard this a thousand times now. But the stuff more important than time is the focus. I have seen people achieve more in less time due to the exceptional focus skills they have. Focus leads to productivity, and...

CNCF Meetup Saar #1

CNCF Meetup Saar #1

The first edition of our CNCF Meetup Saar was on February 25th from 11:00 to 13:00 CET. It was a very fun event with enlightening talks and a few quirks. You can enjoy a recap of the event and the talks below. Recap Full Event...

Why overstimulation sucks your happiness?

Why overstimulation sucks your happiness?

It’s small-time I have been here on this planet, and a trait is occurring for the last few years. I am not so joyful I used to be. It’s hard to pinpoint some reasons, but when a thing bothers you every day and keeps you awake with heartache, it must become your...

Serverless, FaaS and why do you need them?

Serverless, FaaS and why do you need them?

In recent years, serverless adoption has started, with more and more individuals depending on serverless technology to meet organizations’ specific needs. A survey conducted by Serverless Inc showed in 2018 that half of the respondents used serverless in their job,...

The DevOps Roadmap: Unikernels

The DevOps Roadmap: Unikernels

Containerization is one of the core building principles of clouds and DevOps, but traditional VMs and containers lack the security and agility that modern infrastructure craves. We are moving towards workloads that are smaller, faster, and more secure than the...

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