Create a Kubernetes cluster on Hetzner in 5 minutes with Terraform and KubeOne

Create a Kubernetes cluster on Hetzner in 5 minutes with Terraform and KubeOne



Hi folks!

Let’s see how to create a Kubernetes Cluster on Hetzner Cloud with the help of Terraform and KubeOne

What you need:

  • A Hetzner Cloud account
  • KubeOne
  • Terraform

Setup pre-requisite software

Terraform

Following the Official Documentation we can install Terraform:

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=$(dpkg --print-architecture)] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

NOTE: If you are using Debian, probably you need to adjust the release!!!

sudo apt install terraform

KubeOne

Clone the KubeOne repository:

git clone https://github.com/kubermatic/kubeone.git

Navigate to the Hetzner example directory:

cd kubeone/examples/terraform/hetzner

NOTE: You can adjust your installation by modifying the variables in the variables.tf file.

Let’s go!

The first step is to create a Hetzner Cloud project and set an API TOKEN. API TOKEN that will be used on Terraform.

When you have your Hetzner API TOKEN, you can proceed:

export HCLOUD_TOKEN="<YOUR HCLOUD TOKEN>" && \
terraform init && \
terraform apply

You will be prompted to enter a cluster name and then confirm.

After that we need to instruct KubeOne to make it create the Kubernetes Cluster.

First, you should create a kubeone.yaml file with the following content:

apiVersion: kubeone.io/v1beta1
kind: KubeOneCluster

versions:
  kubernetes: '1.23.3'

cloudProvider:
  hetzner: {}
  external: true

then create the output.json file that KubeOne will need:

terraform output -json > output.json

and finally launch the KubeOne command:

kubeone apply --manifest kubeone.yaml --tfjson

It should prompt you for a confirmation again, and after a few minutes…

you’ll have your Kubernetes Cluster all set up!!!

A *-kubeconfig file will be created in the current directory.

After using this file in your ~/home/.kube/configs, you can use the kubectl command to connect to your cluster and see your nodes:

kubectl get nodes

That’s it! Enjoy your K8s cluster! ;)


Freely inspired by Christian Rebischke’s post