terraform-hcloud-kube-hetzner/README.md

329 lines
16 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">
2022-03-19 12:05:16 +01:00
<img src="https://github.com/kube-hetzner/kube-hetzner/raw/master/.images/kube-hetzner-logo.png" alt="Logo" width="112" height="112">
2021-07-30 10:12:37 +02:00
</a>
<h2 align="center">Kube-Hetzner</h2>
<p align="center">
2022-02-18 09:20:49 +01:00
A highly optimized and auto-upgradable, HA-default & Load-Balanced, Kubernetes cluster powered by k3s-on-MicroOS and deployed for peanuts on <a href="https://hetzner.com" target="_blank">Hetzner Cloud</a> 🤑 🚀
2021-07-30 10:12:37 +02:00
</p>
<hr />
</p>
2022-02-22 04:23:15 +01:00
## About The Project
2022-02-10 03:52:08 +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.
2022-04-13 17:29:03 +02:00
This project aims to create an optimal and highly optimized Kubernetes installation that is easily maintained, secure and automatic upgrades. We aimed for functionality as close as possible to GKE's auto-pilot.
2022-02-17 22:52:39 +01:00
2022-04-13 17:29:03 +02:00
To achieve this, we built it on the shoulders of giants by choosing [openSUSE MicroOS](https://en.opensuse.org/Portal:MicroOS) as the base operating system and [k3s](https://k3s.io/) as the Kubernetes engine.
2022-04-13 17:29:03 +02:00
_Please note that we are not affiliates of 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
2022-04-13 17:29:03 +02:00
- Maintenance-free with auto-upgrade to the latest version of MicroOS and k3s.
2022-03-16 00:01:46 +01:00
- Proper use of the Hetzner private network to minimize latency and remove the need for encryption.
2022-04-12 07:40:54 +02:00
- Automatic HA with the default setting of three control-plane nodes and two agent nodes.
2022-04-12 19:31:31 +02:00
- Super-HA: Nodepools for both control-plane and agent nodes can be in different locations.
2022-04-13 15:59:03 +02:00
- Possibility to have a single node cluster with a proper ingress controller.
2022-04-13 17:29:03 +02:00
- Ability to add nodes and nodepools when the cluster is running.
2022-04-12 07:40:54 +02:00
- Traefik ingress controller attached to a Hetzner load balancer with proxy protocol turned on.
2022-05-04 03:18:47 +02:00
- Possibility to turn Longhorn on, and optionally also turn Hetzner CSI off.
- Ability to switch to Calico as CNI, and Cilium can also be easily added.
2022-04-13 17:29:03 +02:00
- Tons of flexible configuration options to suit all needs.
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 -->
2022-02-17 22:45:20 +01:00
## Getting Started
2021-07-30 10:12:37 +02:00
2022-04-13 17:29:03 +02:00
Follow those simple steps, and your world's cheapest Kube cluster will be up and running.
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/).
Then you'll need to have [terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli), [kubectl](https://kubernetes.io/docs/tasks/tools/) cli and [hcloud](<https://github.com/hetznercloud/cli>) the Hetzner cli. The easiest way is to use the [homebrew](https://brew.sh/) package manager to install them (available on Linux, Mac, and Windows Linux Subsystem).
2021-07-30 10:12:37 +02:00
2022-04-13 17:32:51 +02:00
```sh
2022-04-03 09:46:55 +02:00
brew install terraform
brew install kubectl
brew install hcloud
2022-04-13 17:29:03 +02:00
2021-07-30 10:12:37 +02:00
```
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-04-13 17:29:03 +02:00
2. Generate a passphrase-less ed25519 SSH key pair for your cluster; take note of the respective paths of your private and public keys. Or, see our detailed [SSH options](https://github.com/kube-hetzner/kube-hetzner/blob/master/docs/ssh.md). ✅
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-02-27 10:08:08 +01:00
4. Make sure you have the latest Terraform version, ideally at least 1.1.0. You can check with `terraform -v`. ✅
2022-04-13 17:29:03 +02:00
5. (Optional) Other variables in `terraform.tfvars` can be customized, like the Hetzner region and the node counts and sizes.
2021-07-30 10:12:37 +02:00
2022-04-13 17:29:03 +02:00
_One of the easiest ways to use this project is as a Terraform module; see the [examples](#examples) section or the [Kube-Hetzner Terraform module](https://registry.terraform.io/modules/kube-hetzner/kube-hetzner/hcloud/latest) page._
2022-03-21 23:30:46 +01:00
2022-01-05 16:28:19 +01:00
### 🎯 Installation
2021-07-30 10:12:37 +02:00
```sh
2022-02-27 10:35:29 +01:00
terraform init --upgrade
2021-07-30 10:12:37 +02:00
terraform apply -auto-approve
```
2022-02-27 10:36:06 +01:00
It will take around 5 minutes to complete, and then you should see a green output with the IP addresses of the nodes.
2022-02-27 10:35:29 +01:00
## Usage
2022-04-13 17:29:03 +02:00
When your brand new cluster is up and running, the sky is your limit! 🎉
2022-02-27 10:35:29 +01:00
2022-04-13 17:29:03 +02:00
You can immediately kubectl into it (using the `kubeconfig.yaml` saved to the project's directory after the installation). By doing `kubectl --kubeconfig kubeconfig.yaml`, 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-04-13 17:29:03 +02:00
_Once you start with Terraform, it's best not to change the state manually in Hetzner; otherwise, you'll get an error when you try to scale up or down or even destroy the cluster._
2022-01-05 16:28:19 +01:00
2022-02-17 22:45:20 +01:00
### Scaling Nodes
2021-12-13 14:05:16 +01:00
2022-04-13 17:29:03 +02:00
Two things can be scaled: the number of nodepools or the number of nodes in these nodepools. You have two lists of nodepools you can add to in `terraform.tfvars`, the control plane nodepool and the agent nodepool list. Combined, they cannot exceed 255 nodepools (you are extremely unlikely to reach this limit). As for the count of nodes per nodepools, if you raise your limits in Hetzner, you can have up to 64,670 nodes per nodepool (also very unlikely to need that much).
2022-02-10 03:01:40 +01:00
2022-04-12 19:31:31 +02:00
There are some limitations (to scaling down mainly) that you need to be aware of:
2022-04-13 17:29:03 +02:00
_Once the cluster is up; you can change any nodepool count and even set it to 0 (in the case of the first control-plane nodepool, the minimum is 1); you can also rename a nodepool (if the count is to 0), but should not remove a nodepool from the list after once the cluster is up. That is due to how subnets and IPs get allocated. The only nodepools you can remove are those at the end of each list of nodepools._
2022-04-12 19:31:31 +02:00
2022-04-13 17:29:03 +02:00
_However, you can freely add other nodepools at the end of the list, increasing the node count. You can also decrease the node count, but make sure you drain the node in question before; otherwise, it will leave your cluster in a bad state. For obvious reasons, the only nodepool that needs at least to have a count of 1 always is the first control-plane nodepool._
2022-04-12 19:31:31 +02:00
2022-02-17 22:45:20 +01:00
## High Availability
2022-02-11 13:28:27 +01:00
2022-04-13 17:29:03 +02:00
By default, we have three control planes and three agents configured, with automatic upgrades and reboots of the nodes.
2022-02-11 13:28:27 +01:00
2022-04-13 17:29:03 +02:00
If you want to remain HA (no downtime), it's essential to **keep a count of control planes nodes of at least three** (two minimum to maintain quorum when one goes down for automated upgrades and reboot), see [Rancher's doc on HA](https://rancher.com/docs/k3s/latest/en/installation/ha-embedded/).
2022-02-11 13:28:27 +01:00
2022-04-13 17:29:03 +02:00
Otherwise, it's essential to turn off automatic OS upgrades (k3s can continue to update without issue) for the control-plane nodes (when two or fewer control-plane nodes) and do the maintenance yourself.
2022-02-11 13:28:27 +01:00
2022-02-17 22:45:20 +01:00
## Automatic Upgrade
2021-07-30 10:12:37 +02:00
2022-04-14 13:59:05 +02:00
### The Default Setting
2022-04-13 17:29:03 +02:00
By default, MicroOS gets upgraded automatically on each node and reboot safely via [Kured](https://github.com/weaveworks/kured) installed in the cluster.
2022-04-13 17:29:03 +02:00
As for k3s, it also automatically upgrades thanks to Rancher's [system upgrade controller](https://github.com/rancher/system-upgrade-controller). By default, it follows the k3s `stable` channel, but you can also change to the `latest` one if needed or specify a target version to upgrade to via the upgrade plan.
2022-02-17 23:24:23 +01:00
2022-04-13 17:29:03 +02:00
You can copy and modify the [one in the templates](https://github.com/kube-hetzner/kube-hetzner/blob/master/templates/plans.yaml.tpl) for that! More on the subject in [k3s upgrades](https://rancher.com/docs/k3s/latest/en/upgrades/basic/).
2022-02-08 00:53:08 +01:00
2022-04-14 13:59:05 +02:00
### Turning Off Automatic Upgrade
_If you wish to turn off automatic MicroOS upgrades (Important if you are not launching an HA setup which requires at least 3 control-plane nodes), you need to ssh into each node and issue the following command:_
2022-01-06 07:59:13 +01:00
2022-04-13 17:32:51 +02:00
```sh
2022-02-08 00:53:08 +01:00
systemctl --now disable transactional-update.timer
2022-04-13 17:29:03 +02:00
2022-01-06 07:59:13 +01:00
```
2021-07-30 10:12:37 +02:00
2022-04-14 13:59:05 +02:00
_To turn off k3s upgrades, you can either remove the `k3s_upgrade=true` label or set it to `false`. This needs to happen for all the nodes too! To remove it, apply:_
2022-02-16 12:12:05 +01:00
```sh
kubectl -n system-upgrade label node <node-name> k3s_upgrade-
```
2022-04-27 09:27:57 +02:00
Alternatively, you can disable the k3s automatic upgrade without individually editing the labels on the nodes. Instead you can just delete the two system controller upgrade plans with:
```sh
kubectl delete plan k3s-agent -n system-upgrade
kubectl delete plan k3s-server -n system-upgrade
```
2022-04-14 13:59:05 +02:00
### Individual Components Upgrade
Rarely needed, but can be handy in the long run. During the installation, we automatically download a backup of the kustomization to a `kustomization_backup.yaml` file. You will find it next to your `kubeconfig.yaml` at the root of your project.
1. First create a duplicate of that file and name it `kustomization.yaml`, keeping the original file intact, in case you need to restore the old config.
2022-04-14 14:08:03 +02:00
2. Edit the `kustomization.yaml` file; you want to go to the very bottom where you have the links to the different source files; grab the latest versions for each on Github, and replace. If present, remove any local reference to traefik_config.yaml, as Traefik is updated automatically by the system upgrade controller.
3. Apply the the updated `kustomization.yaml` with `kubectl apply -k ./`.
2022-04-14 13:59:05 +02:00
2022-02-23 19:18:10 +01:00
## Examples
<details>
2022-02-23 19:19:44 +01:00
2022-02-23 19:18:10 +01:00
<summary>Ingress with TLS</summary>
Here is an example of an ingress to run an application with TLS, change the host to fit your need in `examples/tls/ingress.yaml` and then deploy the example:
2022-04-13 17:32:51 +02:00
```sh
kubectl apply -f examples/tls/.
2022-04-13 17:29:03 +02:00
```
2022-02-23 19:18:10 +01:00
2022-04-13 17:32:51 +02:00
```yml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
2022-04-13 17:29:03 +02:00
name: Nginx-ingress
annotations:
traefik.ingress.kubernetes.io/router.tls: "true"
traefik.ingress.kubernetes.io/router.tls.certresolver: le
spec:
tls:
- hosts:
- example.com
rules:
- host: example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-service
port:
number: 80
```
2022-02-23 19:18:10 +01:00
</details>
<details>
2022-03-09 03:51:19 +01:00
<summary>Single-node cluster</summary>
2022-04-13 17:29:03 +02:00
Running a development cluster on a single node without any high availability is also possible. You need one control plane nodepool with a count of 1 and one agent nodepool with a count of 0.
2022-04-13 17:29:03 +02:00
In this case, we don't deploy an external load-balancer but use the default [k3s service load balancer](https://rancher.com/docs/k3s/latest/en/networking/#service-load-balancer) on the host itself and open up port 80 & 443 in the firewall (done automatically).
</details>
2022-03-16 00:01:46 +01:00
<details>
<summary>Use as Terraform module</summary>
It is easy to use Kube-Hetzner as a Terraform module. To do so:
``` terraform
2022-03-19 22:48:40 +01:00
module "kube-hetzner" {
source = "kube-hetzner/kube-hetzner/hcloud"
2022-05-02 16:03:15 +02:00
2022-03-19 22:48:40 +01:00
# insert the required variables here found in terraform.tfvars.example
2022-03-16 00:01:46 +01:00
}
```
</details>
2022-04-27 09:27:57 +02:00
<details>
<summary>Use in Terraform cloud</summary>
2022-04-27 09:29:58 +02:00
To use Kube-Hetzner on Terraform cloud, use as a Terraform module as mentioned above, but also change the execution mode from `remote` to `local`.
2022-04-27 09:27:57 +02:00
</details>
2022-05-04 21:39:58 +02:00
<details>
<summary>Configure add-ons with HelmChartConfig</summary>
For instance, to customize the Rancher install, if you choose to enable it, you can create and apply the following `HelmChartConfig`:
```yaml
apiVersion: helm.cattle.io/v1
kind: HelmChartConfig
metadata:
name: rancher
namespace: kube-system
spec:
valuesContent: |-
**values.yaml content you want to customize**
```
The helm options for Rancher can be seen here <https://github.com/rancher/rancher/blob/release/v2.6/chart/values.yaml>.
Same goes for all add-ons, like Longhorn, Cert-manager, and Traefik.
</details>
2022-02-27 10:28:16 +01:00
## Debugging
2022-04-13 17:29:03 +02:00
First and foremost, it depends, but it's always good to have a quick look into Hetzner quickly without logging in to the UI. That is where the `hcloud` cli comes in.
2022-02-27 10:28:16 +01:00
2022-04-13 17:29:03 +02:00
- Activate it with `hcloud context create Kube-hetzner`; it will prompt for your Hetzner API token, paste that, and hit `enter`.
- To check the nodes, if they are running, use `hcloud server list`.
- To check the network, use `hcloud network describe k3s`.
- To look at the LB, use `hcloud loadbalancer describe traefik`.
2022-02-27 10:28:16 +01:00
Then for the rest, you'll often need to login to your cluster via ssh, to do that, use:
2022-04-13 17:32:51 +02:00
```sh
2022-02-27 10:28:16 +01:00
ssh root@xxx.xxx.xxx.xxx -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no
2022-04-13 17:29:03 +02:00
2022-02-27 10:28:16 +01:00
```
2022-02-27 10:39:45 +01:00
Then, for control-plane nodes, use `journalctl -u k3s` to see the k3s logs, and for agents, use `journalctl -u k3s-agent` instead.
2022-02-27 10:28:16 +01:00
2022-04-13 17:29:03 +02:00
Last but not least, to see when the previous reboot took place, you can use both `last reboot` and `uptime`.
2022-02-27 10:28:16 +01:00
2021-07-30 10:32:25 +02:00
## Takedown
2022-04-13 17:29:03 +02:00
If you want to take down the cluster, you can proceed as follows:
2021-07-30 10:32:25 +02:00
2022-04-13 17:32:51 +02:00
```sh
2021-07-30 10:32:25 +02:00
terraform destroy -auto-approve
```
2022-02-25 15:56:18 +01:00
And if the network is slow to delete, just issue `hcloud network delete k3s` to speed things up!
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 -->
2022-02-12 02:04:41 +01:00
## History
2022-02-10 22:13:16 +01:00
2022-02-12 02:04:41 +01:00
This project has tried two other OS flavors before settling on MicroOS. Fedora Server, and k3OS. The latter, k3OS, is now defunct! However, our code base for it lives on in the [k3os branch](https://github.com/kube-hetzner/kube-hetzner/tree/k3os). Do not hesitate to check it out, it should still work.
2022-02-10 22:13:16 +01:00
2022-02-17 22:35:43 +01:00
There is also a branch where openSUSE MicroOS came preinstalled with the k3s RPM from devel:kubic/k3s, but we moved away from that solution as the k3s version was rarely getting updates. See the [microOS-k3s-rpm](https://github.com/kube-hetzner/kube-hetzner/tree/microOS-k3s-rpm) branch for more.
2021-07-30 10:12:37 +02:00
## Contributing
2022-04-13 17:29:03 +02:00
🌱 This project currently installs openSUSE MicroOS via the Hetzner rescue mode, making things a few minutes slower. If you could **take a few minutes to send a support request to Hetzner, asking them to please add openSUSE MicroOS as a default image**, not just an ISO. The more requests they receive, the likelier they are to add support for it, and if they do, that will cut the deployment time by half. The official link to openSUSE MicroOS is <https://get.opensuse.org/microos>, and their `OpenStack Cloud` image has full support for Cloud-init, which would probably suit the Hetzner Ops team!
2022-02-23 19:18:10 +01:00
2022-04-13 17:29:03 +02:00
Code contributions are very much **welcome**.
2021-07-30 10:12:37 +02:00
1. Fork the Project
2. Create your Branch (`git checkout -b AmazingFeature`)
2022-04-13 17:29:03 +02:00
3. Commit your Changes (`git commit -m 'Add some AmazingFeature")
2021-07-30 10:12:37 +02:00
4. Push to the Branch (`git push origin AmazingFeature`)
2022-04-13 17:29:03 +02:00
5. Open a Pull Request targetting the `staging` branch.
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.
2022-04-13 17:29:03 +02:00
- [Best-README-Template](https://github.com/othneildrew/Best-README-Template) made writing this readme a lot easier.
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.
2022-04-13 17:29:03 +02:00
- [Rancher](https://www.rancher.com) for k3s, an amazing Kube distribution that is the core engine of this project.
2022-02-10 13:31:31 +01:00
- [openSUSE](https://www.opensuse.org) for MicroOS, which is just next level Container OS technology.
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-05-02 16:03:15 +02:00
[product-screenshot]: https://github.com/kube-hetzner/kube-hetzner/raw/master/.images/kubectl-pod-all-17022022.png