Simple bare metal Kubernetes Cluster on Hetzner

If you don't feel like paying at least 70 USD per month for your AWS EKS Cluster, there is a much cheaper alternative. The cluster won't be managed by Amazon then of course but maybe you're a CKA and don't actually want this.
Talking about CKA, here are the requirements for you to make use of this because we're going to automate this setup of course:
- Basic knowledge of the following tools: Linux shell scripting, Terraform, Kubernetes, Ansible, Makefile
- Hetzner Account and API key for the cloud API
Hetzner is a great German hoster for different cloud services with a nice Terraform provider. They don't offer the depth and breadth of other providers but maybe that's alright for your use case.
And maybe the price tag of just 4,51 EUR per month per node (two or three should be fine in the beginning) is what you're looking for.
So let's take a quick look at what we can specify using Terraform:
- Servers (with all their attributes)
- Firewalls
- Networks
- Provisioners for configuration (which scripts to execute once the servers are online)
- DNS zone and records
- Utilities like ssh key etc.
What's still missing is how we actually configure the servers to be used by our cluster. And here Ansible plays it's role with it's concept of playbooks. This is pretty important and requires a considerable amount of work to prepare but here we go:
Installation and setup
Make sure you have Terraform and Ansible installed and don't forget to store your Hetzner API Key in the .env
(rename it like that from the dot.env.example), then:
mv terraform/terraform.tfvars.example terraform/terraform.tfvars
-> change the values if you want toterraform -chdir=terraform init
make plan
-> shows what is going to be done by Terraformmake apply
-> WARNING ⚠️: this also runsmake adjust-kubeconfig
which will overwrite your current kubeconfig at~/.kube/config
; then wait about 5 minutes, also after the command finished so that the kubelets have time to initializekubectl get nodes
-> There goes your fresh cluster!
If you don't need the cluster any more and don't want to keep paying for used resources, just run make destroy
. But be careful to take backups of your work outside of the servers because it destroys everything that Terraform set up!
Of course there is a bunch of choices that I already made for you to keep it simple, secure and easy to extend it with additional components:
- current Kubernetes v1.30 (configurable in prepare-kubeadm.yml)
- Calico CNI
- private nodes (using the private IPs for communication between the nodes)
- SSH and control plane access only enabled from your own computer (see security.tf)
- Ubuntu 20.04 for the servers (for some reason, this worked best, I'll surely update this at some point)
- Local storage of the Terraform state file
- Datacenter in Nuremberg
You can always go ahead, fork the project and make your own choices or make it more configurable. Open Source is great.
Let me know if it works for you, I'm using a very similar setup for many k8s projects. If you're rather opting for an out of the box preconfigured HA cluster, AWS can still be a great choice.
If you have any questions or suggestions about the setup, feel free to leave a comment. Thanks for reading!