Deploying a Kubernetes Cluster on Hetzner in 2022 using Ubuntu 20.04
This post is kind of an homage to the post by shibumi.dev, which has been originally posted in January 2021.
As I’ve been running this setup on Ubuntu and some things have changed, I will
show in this post how to set up a Kubernetes Cluster on Hetzner using kubeone
and terraform
in 2022.
Preparation
First of all, as mentioned also in the original post, you need prepare the following things:
- A Hetzner Cloud Account
- Install Kubectl
- Install KubeOne
- Install Terraform
- Have a basic Kubernetes and Linux understanding. We’re not going into the details here.
Next we’re installing kubectl
, kubeone
and terraform
on our local (Ubuntu 20.04)
machine.
Kubectl
Kubectl can be installed via the curl
installer and is required to run Kubernetes
related commands on your command line.
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
Terraform
There is some official guidance on the Terraform website. Below you’ll find the commands to install Terraform on Ubuntu 20.04:
# Download HashiCorp APT repo GPG key
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
# Add the HashiCorp APT repository to the local machine
sudo apt-add-repository "deb [arch=$(dpkg --print-architecture)] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
# Install Terraform
sudo apt install terraform
KubeOne
The easiest way to install KubeOne seems to be via their installer script as mentioned on their GitHub repo.
curl -sfL get.kubeone.io | sh
Create read-write API token on Hetzner Cloud
In order to run the infrastructure setup with Terraform, we need a read-write Hetzner API token, which is allowed to create resources on Hetzner.
After generating the token, make sure to keep it for the installation part.
Installation
Create Infrastructure using Terraform
This repository contains good terraform templates, which we can use to create our
Kubernetes Cluster.
This contains the infrastructure definition, which creates the specific instances,
load balancer and network configuratin that is required to set up a Kubernetes Cluster on Hetzner.
# Clone KubeOne repository
git clone https://github.com/kubermatic/kubeone
# Switch to Hetzner template directory
cd kubeone/examples/terraform/hetzner
# Export Hetzner Cloud read-write API token to environment variable for Terraform
export HCLOUD_TOKEN="<YOUR HCLOUD TOKEN>"
# Create infrastructure using Terraform
terraform init
terraform apply
Create Kubernetes Cluster using KubeOne
Now we’ll create our kubeone.yaml
in order to create our Kubernetes Cluster.
apiVersion: kubeone.k8c.io/v1beta2
kind: KubeOneCluster
versions:
kubernetes: 1.23.5
cloudProvider:
hetzner: {}
external: true
To create our Kubernetes Cluster we’ll run the following commands:
# Store Terraform infrastructure state to output.json to pass it to KubeOne
terraform output -json > output.json
# Create Kubernetes Cluster using KubeOne
kubeone apply --manifest kubeone.yaml --tfjson output.json
# After the cluster has been created, you can copy your kube config to ~/.kube/,
# but make sure that it doesn't overwrite any configurations you may still need!
cp *-kubeconfig ~/.kube/config
# Verify that the nodes have been properly deployed and all pods are running
kubectl get nodes
# Check that all pods are running (all namespaces)
kubectl get pods -A
Uninstall
If you just wanted to run this as kind of a playground and want to save money on
the infrastructure again, I would suggest to uninstall the whole setup again.
You can also do that by making use of terraform.
# Destroy infrastructure (Warning! This destroys the entire setup/deletes everything.)
terraform destroy
Hopefully, this helps to replicate the setup and that you’ll be able to set up a Kubernetes Cluster on Hetzner using Ubuntu 20.40 in 2022.
Again, a shoutout to shibumi.dev!