terraform-hcloud-kube-hetzner/README.md

188 lines
8.6 KiB
Markdown
Raw Normal View History

2021-07-30 10:12:37 +02:00
[![Contributors][contributors-shield]][contributors-url]
[![Forks][forks-shield]][forks-url]
[![Stargazers][stars-shield]][stars-url]
[![Issues][issues-shield]][issues-url]
[![MIT License][license-shield]][license-url]
<!-- PROJECT LOGO -->
<br />
<p align="center">
<a href="https://github.com/mysticaltech/kube-hetzner">
<img src=".images/kube-hetzner-logo.png" alt="Logo" width="112" height="112">
</a>
<h2 align="center">Kube-Hetzner</h2>
<p align="center">
2022-01-05 15:50:00 +01:00
A highly optimized and auto-upgradable, HA-able & Load-Balanced, Kubernetes cluster powered by k3s-on-k3os deployed for peanuts on <a href="https://hetzner.com" target="_blank">Hetzner Cloud</a> 🤑 🚀
2021-07-30 10:12:37 +02:00
</p>
<hr />
<br />
</p>
## About The Project
2022-01-06 06:27:54 +01:00
[Hetzner Cloud](https://hetzner.com) is a good cloud provider that offers very affordable prices for cloud instances, with data center locations in both Europe and the US. The goal of this project is to create an optimal and highly optimized Kubernetes installation that is easily maintained, secure, and automatically upgrades. We aimed for functionality as close as possible to GKE's auto-pilot.
2021-07-30 10:12:37 +02:00
2021-12-13 14:05:16 +01:00
_Please note that we are not affiliated to Hetzner, this is just an open source project striving to be an optimal solution for deploying and maintaining Kubernetes on Hetzner Cloud._
2021-12-10 01:24:20 +01:00
2021-12-05 11:09:48 +01:00
### Features
2021-07-30 10:12:37 +02:00
2021-12-06 00:24:18 +01:00
- Lightweight and resource-efficient Kubernetes powered by [k3s](https://github.com/k3s-io/k3s) on [k3os](https://github.com/rancher/k3os) nodes.
2022-01-06 10:56:40 +01:00
- Maintenance free with auto-upgrade to the latest version of k3os, k3s, Hetzner CCM and CSI.
2022-01-06 07:33:36 +01:00
- Proper use of the underlying Hetzner private network to remove the need for encryption and make the cluster both fast and secure.
- Automatic HA with the default setting of two control-plane and agents nodes.
- Ability to add or remove as many nodes as you want while the cluster stays running.
2022-01-05 16:28:19 +01:00
- Automatic Traefik ingress controller attached to a Hetzner load balancer with proxy protocol turned on.
2021-07-30 10:12:37 +02:00
2021-12-05 10:50:51 +01:00
_It uses Terraform to deploy as it's easy to use, and Hetzner provides a great [Hetzner Terraform Provider](https://registry.terraform.io/providers/hetznercloud/hcloud/latest/docs)._
2021-07-30 10:12:37 +02:00
2021-12-05 11:09:48 +01:00
![Product Name Screen Shot][product-screenshot]
2021-07-30 10:12:37 +02:00
<!-- GETTING STARTED -->
## Getting started
2021-12-06 00:24:18 +01:00
Follow those simple steps, and your world's cheapest Kube cluster will be up and running in no time.
2021-07-30 10:12:37 +02:00
2022-01-05 16:28:19 +01:00
### ✔️ Prerequisites
2021-07-30 10:12:37 +02:00
First and foremost, you need to have a Hetzner Cloud account. You can sign up for free [here](https://hetzner.com/cloud/).
2022-01-06 06:27:54 +01:00
Then you'll need to have [terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli) and [kubectl](https://kubernetes.io/docs/tasks/tools/) cli installed. The easiest way is to use the [gofish](https://gofi.sh/#install) package manager to install them.
2021-07-30 10:12:37 +02:00
```sh
2022-01-06 06:27:54 +01:00
gofish install terraform
gofish install kubectl
2021-07-30 10:12:37 +02:00
```
2022-01-06 06:27:54 +01:00
_The Hetzner cli `hcloud` is also useful to have, mainly for debugging without having to use the Hetzner website. See how to install it [here](https://github.com/hetznercloud/cli)._
2022-01-06 07:16:18 +01:00
### 💡 [Do not skip] Creating the terraform.tfvars file
2021-07-30 10:12:37 +02:00
2022-01-11 22:33:00 +01:00
1. Create a project in your [Hetzner Cloud Console](https://console.hetzner.cloud/), and go to **Security > API Tokens** of that project to grab the API key. Take note of the key! ✅
2022-01-25 21:41:01 +01:00
2. Either, generate a passphrase-less ed25519 SSH key-pair for your cluster, unless you already have one that you'd like to use. Take note of the respective paths of your private and public keys. Or, for a key-pair with passphrase or a device like a Yubikey, make sure you have got an SSH agent running and your key is loaded (`ssh-add -L` to verify) and set `private_key = null` in the following step. ✅
2022-01-13 10:22:54 +01:00
3. Copy `terraform.tfvars.example` to `terraform.tfvars`, and replace the values from steps 1 and 2. ✅
2022-01-06 07:16:18 +01:00
4. (Optional) There are other variables in `terraform.tfvars` that could be customized, like Hetzner region, and the node counts and sizes.
2021-07-30 10:12:37 +02:00
2022-01-05 16:28:19 +01:00
### 🎯 Installation
2021-07-30 10:12:37 +02:00
```sh
terraform init
terraform apply -auto-approve
```
2022-01-06 07:23:04 +01:00
It will take around 12 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).
2022-01-06 06:27:54 +01:00
_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 (you can get the path of kubeconfig.yaml by running `pwd`):_
```sh
export KUBECONFIG=/<path-to>/kubeconfig.yaml
```
2022-01-05 16:28:19 +01:00
<!-- USAGE EXAMPLES -->
## Usage
2022-01-06 06:27:54 +01:00
When the cluster is up and running, you can do whatever you wish with it! 🎉
2022-01-05 16:28:19 +01:00
2022-01-06 06:27:54 +01:00
### Scaling nodes
2021-12-13 14:05:16 +01:00
2022-01-18 07:52:06 +01:00
You can scale the number of nodes up and down without any issues. If you are going to scale down, just make sure to properly `kubectl drain` the nodes in question first. Then just edit these variables in `terraform.tfvars` and re-apply terraform with `terraform apply -auto-approve`.
2022-01-06 07:16:18 +01:00
For instance:
2021-12-13 14:05:16 +01:00
```tfvars
servers_num = 2
agents_num = 3
```
2021-07-30 10:12:37 +02:00
### Useful commands
- List your nodes IPs, with either of those:
```sh
terraform outputs
hcloud server list
```
- See the Hetzner network config:
```sh
hcloud network describe k3s-net
```
- Log into one of your nodes (replace the location of your private key if needed):
```sh
2021-12-05 10:50:51 +01:00
ssh rancher@xxx.xxx.xxx.xxx -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no
2021-07-30 10:12:37 +02:00
```
2022-01-06 07:39:53 +01:00
## Automatic upgrade
2021-07-30 10:12:37 +02:00
By default, k3os and its embedded k3s instance get upgraded automatically on each node, thanks to its embedded system upgrade controller.
_You can also choose to automatically kustomize the Hetzner CCM and CSI to set their container images to "latest" with an imagePullPolicy of "Always". That means that when the nodes upgrade, these container images will be automatically upgraded too. For more info on this, see [terraform.tfvars.example](terraform.tfvars.example)._
2021-07-30 10:12:37 +02:00
2022-01-06 08:00:34 +01:00
_If you wish to turn off automatic upgrade on a specific node, you need to take out the label `k3os.io/upgrade=latest`. It can be done with the following command:_
2022-01-06 07:59:13 +01:00
```sh
kubectl label node <nodename> 'k3os.io/upgrade'-
```
2021-07-30 10:12:37 +02:00
2021-07-30 10:32:25 +02:00
## Takedown
2022-01-05 16:28:19 +01:00
If you want to takedown the cluster, you can proceed as follows:
2021-07-30 10:32:25 +02:00
```sh
2022-01-13 15:10:53 +01:00
kubectl delete -k hetzner/csi
kubectl delete -k hetzner/ccm
2022-01-05 15:04:22 +01:00
hcloud load-balancer delete traefik
2021-07-30 10:32:25 +02:00
terraform destroy -auto-approve
```
2022-01-06 08:02:52 +01:00
_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._
2021-10-07 22:53:00 +02:00
2021-07-30 10:12:37 +02:00
<!-- CONTRIBUTING -->
## 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
<!-- CONTACT -->
2021-12-10 01:10:46 +01:00
## Contributors
2021-07-30 10:12:37 +02:00
2021-12-10 01:10:46 +01:00
- Karim Naufal - [@mysticaltech](https://github.com/mysticaltech)
2022-01-13 22:26:03 +01:00
- Dennis Hoppe - [@dhoppe](https://github.com/dhoppe)
2022-01-25 21:28:25 +01:00
- Paul Härle - [@phaer](https://github.com/phaer)
2021-07-30 10:12:37 +02:00
<!-- ACKNOWLEDGEMENTS -->
## Acknowledgements
2021-07-30 10:47:26 +02:00
- [k-andy](https://github.com/StarpTech/k-andy) was the starting point for this project. It wouldn't have been possible without it.
- [Best-README-Template](https://github.com/othneildrew/Best-README-Template) that made writing this readme a lot easier.
2021-12-09 13:03:42 +01:00
- [k3os-hetzner](https://github.com/hughobrien/k3os-hetzner) was the inspiration for the k3os installation method.
2021-12-10 01:26:04 +01:00
- [Hetzner Cloud](https://www.hetzner.com) for providing a solid infrastructure and terraform package.
2021-12-10 01:24:20 +01:00
- [Hashicorp](https://www.hashicorp.com) for the amazing terraform framework that makes all the magic happen.
- [Rancher](https://www.rancher.com) for k3s and k3os, robust and innovative technologies that are the very core engine of this project.
2021-07-30 10:12:37 +02:00
[contributors-shield]: https://img.shields.io/github/contributors/mysticaltech/kube-hetzner.svg?style=for-the-badge
[contributors-url]: https://github.com/mysticaltech/kube-hetzner/graphs/contributors
[forks-shield]: https://img.shields.io/github/forks/mysticaltech/kube-hetzner.svg?style=for-the-badge
[forks-url]: https://github.com/mysticaltech/kube-hetzner/network/members
[stars-shield]: https://img.shields.io/github/stars/mysticaltech/kube-hetzner.svg?style=for-the-badge
[stars-url]: https://github.com/mysticaltech/kube-hetzner/stargazers
[issues-shield]: https://img.shields.io/github/issues/mysticaltech/kube-hetzner.svg?style=for-the-badge
[issues-url]: https://github.com/mysticaltech/kube-hetzner/issues
[license-shield]: https://img.shields.io/github/license/mysticaltech/kube-hetzner.svg?style=for-the-badge
[license-url]: https://github.com/mysticaltech/kube-hetzner/blob/master/LICENSE.txt
2022-01-05 16:28:19 +01:00
[product-screenshot]: .images/kubectl-pod-all.png