Go to file
2021-12-09 13:03:42 +01:00
.images Improved readme 2021-12-05 11:09:48 +01:00
manifests Switched to k3os and removed cilium 2021-12-05 10:50:51 +01:00
templates Switched to k3os and removed cilium 2021-12-05 10:50:51 +01:00
.gitignore initial commit 2021-07-30 10:12:37 +02:00
agents.tf k3os ok 2021-12-03 02:11:52 +01:00
LICENSE initial commit 2021-07-30 10:12:37 +02:00
main.tf k3os ok 2021-12-03 02:11:52 +01:00
master.tf Switched to k3os and removed cilium 2021-12-05 10:50:51 +01:00
output.tf k3os ok 2021-12-03 02:11:52 +01:00
README.md Update README.md 2021-12-09 13:03:42 +01:00
servers.tf k3os ok 2021-12-03 02:11:52 +01:00
terraform.tfvars.example initial commit 2021-07-30 10:12:37 +02:00
variables.tf k3os master ok 2021-11-30 23:09:34 +01:00
versions.tf initial commit 2021-07-30 10:12:37 +02:00

Contributors Forks Stargazers Issues MIT License LinkedIn


Logo

Kube-Hetzner

A highly optimized and auto-upgradable, HA-able, Kubernetes cluster powered by k3s on k3os on Hetzner Cloud 🤑



About The Project

Hetzner Cloud is a good cloud provider that offers very affordable prices for cloud instances, with data center locations in both Europe and America. The goal of this project was to create an optimal and highly optimized Kubernetes installation that is easily maintained, secure, and automatically upgrades itself. We aimed for functionality as close as possible to GKE's auto-pilot!

Features

  • Lightweight and resource-efficient Kubernetes powered by k3s on k3os nodes.
  • Automatic HA by setting the required number of servers and agents nodes.
  • (Optional) Nginx ingress controller that will automatically use Hetzner's private network to allocate a Hetzner load balancer.

It uses Terraform to deploy as it's easy to use, and Hetzner provides a great Hetzner Terraform Provider.

Product Name Screen Shot

Getting started

Follow those simple steps, and your world's cheapest Kube cluster will be up and running in no time.

Prerequisites

First and foremost, you need to have a Hetzner Cloud account. You can sign up for free here.

Then you'll need you have the terraform, helm, and kubectl cli installed. The easiest way is to use the gofish package manager to install them.

gofish install terraform && gofish install kubectl && gofish install helm

Creating terraform.tfvars

  1. Create a project in your Hetzner Cloud Console, and go to Security > API Tokens of that project to grab the API key.
  2. Generate an ssh key pair for your cluster, unless you already have one that you'd like to use.
  3. Rename terraform.tfvars.example to terraform.tfvars, and replace the values from steps 1 and 2.

Customize other variables (Optional)

The number of control plane nodes and worker nodes, the Hetzner datacenter location (.i.e. ngb1, fsn1, hel1 ...etc.), and the Hetzner server types (i.e. cpx31, cpx41 ...etc.) can be customized by adding the corresponding variables to your newly created terraform.tfvars file.

See the default values in the variables.tf file, they correspond to (you can copy-paste and customize):

servers_num = 2
agents_num = 2
location = "fsn1"
agent_server_type = "cpx21"
control_plane_server_type = "cpx11"

Installation

terraform init
terraform apply -auto-approve

It will take a few minutes to complete, and then you should see a green output with the IP addresses of the nodes. Then you can immediately kubectl into it (using the kubeconfig.yaml saved to the project's directory after the install).

Just using the command kubectl --kubeconfig kubeconfig.yaml would work, but for more convenience, either create a symlink from ~/.kube/config to kubeconfig.yaml, or add an export statement to your ~/.bashrc or ~/.zshrc file, as follows:

export KUBECONFIG=/<path-to>/kubeconfig.yaml

Of course, to get the path, you could use the pwd command.

Ingress Controller (Optional)

When using Kubernetes, it is ideal to have an ingress controller to expose services to the outside world. And it turns out that the Hetzner Cloud Controller allows us to automatically deploy a Hetzner Load Balancer that the ingress controller can use. You can install the Nginx ingress controller with the following command:

helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
helm install --values=manifests/helm/nginx/values.yaml ingress-nginx ingress-nginx/ingress-nginx -n kube-system --kubeconfig kubeconfig.yaml

Please note that the load balancer's geographic location and instance type are editable in values.yaml.

Usage

When the cluster is up and running, you can do whatever you wish with it. Enjoy! 🎉

Useful commands

  • List your nodes IPs, with either of those:
terraform outputs
hcloud server list
  • See the Hetzner network config:
hcloud network describe k3s-net
  • Log into one of your nodes (replace the location of your private key if needed):
ssh rancher@xxx.xxx.xxx.xxx -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no

Automatic upgrade

By default, k3os and its embedded k3s instance get upgraded automatically on each node, thanks to its embedded system upgrade controller. If you wish to turn that feature off, please remove the following label k3os.io/upgrade=latest with the following command:

kubectl label node <nodename> 'k3os.io/upgrade'- --kubeconfig kubeconfig.yaml

Individual components upgrade

To upgrade individual components, you can use the following commands:

  • Hetzner CCM and CSI
kubectl apply -f https://raw.githubusercontent.com/mysticaltech/kube-hetzner/master/manifests/hcloud-ccm-net.yaml --kubeconfig kubeconfig.yaml
kubectl apply -f https://raw.githubusercontent.com/hetznercloud/csi-driver/master/deploy/kubernetes/hcloud-csi.yml --kubeconfig kubeconfig.yaml
  • (Optional, if installed) Nginx ingress controller
helm repo update
helm upgrade --values=manifests/helm/nginx/values.yaml ingress-nginx ingress-nginx/ingress-nginx -n kube-system --kubeconfig kubeconfig.yaml

Takedown

If you choose to install the Nginx ingress controller, you need to delete it first to release the load balancer, as follows:

helm delete ingress-nginx -n kube-system --kubeconfig kubeconfig.yaml

Then you can proceed to take down the rest of the cluster with:

kubectl delete -f https://raw.githubusercontent.com/mysticaltech/kube-hetzner/master/manifests/hcloud-ccm-net.yaml --kubeconfig kubeconfig.yaml
kubectl delete -f https://raw.githubusercontent.com/hetznercloud/csi-driver/master/deploy/kubernetes/hcloud-csi.yml --kubeconfig kubeconfig.yaml
terraform destroy -auto-approve

Also, if you had a full-blown cluster in use, it would be best to delete the whole project in your Hetzner account directly as operators or deployments may create other resources during regular operation.

Roadmap

See the open issues for a list of proposed features (and known issues).

Contributing

Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Branch (git checkout -b AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin AmazingFeature)
  5. Open a Pull Request

License

The following code is distributed as-is and under the MIT License. See LICENSE for more information.

Contact

Karim Naufal - @mysticaltech - karim.naufal@me.com

Project Link: https://github.com/mysticaltech/kube-hetzner

Acknowledgements

  • k-andy was the starting point for this project. It wouldn't have been possible without it.
  • Best-README-Template that made writing this readme a lot easier.
  • k3os-hetzner was the inspiration for the k3os installation method.