|
| 1 | +[](https://github.com/axgkl/pyhk3/actions/workflows/test.yml) |
| 2 | +# Hetzner K3s - Pythonic |
| 3 | + |
| 4 | + |
| 5 | +> A collection of functions to setup K3s clusters on [Hetzner Cloud][hcloud], based on vitobotta's [hetzner-k3s][hk3s] |
| 6 | +
|
| 7 | +## About |
| 8 | + |
| 9 | +[Hetzner-k3s][hk3s] is nicely engineered general k3s installation tool on Hetzner, with a large degree of declarative possibilities for customization. As terraform, it is a single static binary and idempotent, with a single source of truth. In contrast to terraform it is straightforward to use, with far less abstractions but a lot of built in best practices, incl CNI and autoscaling, plus faster. |
| 10 | + |
| 11 | +This repo here provides a set of **python functions**, incl. possibly useful support tools to organize them, in order to further automate _around_ the pure k3s installation, which hetzner-k3s provides. |
| 12 | + |
| 13 | + |
| 14 | +## Features |
| 15 | + |
| 16 | +### Pre K3s Installation |
| 17 | + |
| 18 | +Focus is on creating the cluster with private IPs only, and a _proxy_ server in front of them: |
| 19 | + |
| 20 | +```mermaid |
| 21 | +flowchart LR |
| 22 | + A[World] --> B[Bastion Proxy<br/>IP pub<br/>Opt.LoadBalancer] |
| 23 | + B --priv net--> M1[Master 1<br/>...<br/>Master 3] |
| 24 | + B --priv net--> w1[Worker 1<br/>...<br/>Worker n] |
| 25 | + B --priv net--> a1[Autoscaled 1<br/>...<br/>Autoscaled n] |
| 26 | +``` |
| 27 | + |
| 28 | +That bastion server is the only one with a public IP, and is equipped with a l4 loadbalancer, forwarding the traffic into the cluster, like a hetzner loadbalancer would do. |
| 29 | + |
| 30 | +💡 Using the bastion node as loadbalancer is optional. [hetzner-k3s][hk3s] does by default create hetzner loadbalancers for you, using the hetzner cloud controller manager (ccm). |
| 31 | + |
| 32 | +[Here](./docs/l4lb.md) is a detailed description of the loadbalancer setup, incl. some reasons for it. |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +We provide the functions necessary to |
| 37 | + |
| 38 | +- create the private network |
| 39 | +- bastion node itself, incl. ssh key and make it know to hetzner. |
| 40 | +- tools (hetzner-k3s, kubectl, helm) and [load balancer service](./docs/l4lb.md) on it |
| 41 | +- cloud init config for hetzner-k3s, so that the priv ip nodes can reach the internet |
| 42 | + |
| 43 | +Then hetzner-k3s can be run from there, to create the cluster. |
| 44 | + |
| 45 | +### K3s Installation |
| 46 | + |
| 47 | +From the proxy server, we then kick off hetzner-k3s, using a config we synthesized from the environ. |
| 48 | + |
| 49 | +[Here](./docs/privip.md) are the details regarding a private IP only cluster setup. |
| 50 | + |
| 51 | +### Post K3s Installation |
| 52 | + |
| 53 | +We provide functions to |
| 54 | + |
| 55 | +- transfer kubeconfig from the bastion to the local machine |
| 56 | +- configure local ssh |
| 57 | +- install cert-manager into the cluster |
| 58 | +- install ingress-nginx into the cluster |
| 59 | +- install services using that ingress into the cluster, in a way so that https traffic from the world can reach the application pods with |
| 60 | + - working certificates |
| 61 | + - optional pod affinity via cookie ("sticky sessions") |
| 62 | + - source ip preservation (using [proxy protocol](https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt)) |
| 63 | + - autoscaling support |
| 64 | + |
| 65 | +## Usage |
| 66 | + |
| 67 | +In general the script provides its functions after being sourced from a bash script _you_ provide and make executable. |
| 68 | + |
| 69 | +See the ci [../tests/setup.sh](../tests/setup.sh) script for an example, which installs the full cluster from scratch. |
| 70 | + |
| 71 | + |
| 72 | +💡 When you pass _arguments_ to that script, this results in an execution of the given function and exit of the script, w/o running the subsequent functions after sourcing. |
| 73 | + |
| 74 | + |
| 75 | +General layout of your script is therefore: |
| 76 | + |
| 77 | +```bash |
| 78 | +CONFIGVAR1=CONFIGVAL1 |
| 79 | +... |
| 80 | +source <dir>/main.sh "$@" # exits after execution of any given arguments in $@ (e.g. funcname, params). |
| 81 | +#Otherwise continues with the functions below: |
| 82 | +setup_function1 |
| 83 | +setup_function2 |
| 84 | +... |
| 85 | +``` |
| 86 | + |
| 87 | +`yourscript -h` lists all available functions. |
| 88 | + |
| 89 | +## CI Automation |
| 90 | + |
| 91 | +See [here](./docs/ci.md) |
| 92 | + |
| 93 | + |
| 94 | +## Customization |
| 95 | + |
| 96 | +See [here](./docs/customization.md) |
| 97 | + |
| 98 | +## Dev Details |
| 99 | + |
| 100 | +[here](./docs/customization.md) |
| 101 | + |
| 102 | +--- |
| 103 | + |
| 104 | +#### Local kubectl/helm support |
| 105 | + |
| 106 | +You want to copy the kubeconfig file, which the installer script created on the bastion node to your local machine, so that you can manage the cluster from there. |
| 107 | + |
| 108 | +I change the server line within the copied local kubeconfig to this: |
| 109 | + |
| 110 | +```yaml |
| 111 | +server: https://127.0.0.1:16443 |
| 112 | +``` |
| 113 | +
|
| 114 | +and configure ssh like this: |
| 115 | +
|
| 116 | +```config |
| 117 | +# ---- cluster citest |
| 118 | +Host citest-proxy |
| 119 | + HostName 37.27.... # pub ip of the bastion node |
| 120 | + User root |
| 121 | + Port 22 |
| 122 | + LocalForward 16443 10.1.0.3:6443 # first master |
| 123 | +Host citest-m1 |
| 124 | + HostName 10.1.0.3 |
| 125 | + User root |
| 126 | + Port 22 |
| 127 | + ProxyCommand ssh -W %h:%p citest-proxy |
| 128 | +Host citest-m2 |
| 129 | + HostName 10.1.0.4 |
| 130 | + User root |
| 131 | + Port 22 |
| 132 | + ProxyCommand ssh -W %h:%p citest-proxy |
| 133 | +Host citest-m3 |
| 134 | + HostName 10.1.0.5 |
| 135 | + User root |
| 136 | + Port 22 |
| 137 | + ProxyCommand ssh -W %h:%p citest-proxy |
| 138 | +# ---- cluster citest |
| 139 | +``` |
| 140 | + |
| 141 | +#### Load Balancer on Bastion Node |
| 142 | + |
| 143 | +You can install a layer 4 load balancer on bastion, turning it into a full proxy into your cluster, eradicating the need for a hetzner load balancer. |
| 144 | + |
| 145 | +[This repo](https://github.com/axgkl/hk3sf) explains how to do that. |
| 146 | + |
| 147 | +## Refs |
| 148 | + |
| 149 | +- [notes](./docs/knowledge.md) |
| 150 | + |
| 151 | +- <https://community.hetzner.com/tutorials/how-to-set-up-nat-for-cloud-networks> |
| 152 | +- <https://github.com/vitobotta/hetzner-k3s> |
| 153 | +- <https://github.com/vitobotta/hetzner-k3s/issues/379> |
| 154 | +- <https://www.youtube.com/watch?v=u5l-F8nPumE&t=466s> |
| 155 | +- <https://gimlet.io> |
| 156 | + |
| 157 | +--- |
| 158 | + |
| 159 | +K3s with: HA + AutoScaling + GitOps from scratch. 💗 For < 20€/month if wanted. |
| 160 | + |
| 161 | +[hk3s]: https://github.com/vitobotta/hetzner-k3s |
| 162 | +[hcloud]: https://docs.hetzner.cloud/ |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | + |
0 commit comments