=== Setup Kubernetes

In this guide we are using microk8s to setup a Kubernetes cluster.
To setup a Micro K8S Cluster on you need ubuntu maschines with static IP addresses, a working internet connection and optional for metallb "loadbalancer" an extra IP range for the services.

The network configuration for ubuntu 20.04+ is done with netplan.
Config in /etc/netplan/00-installer-config.yaml and apply with sudo netplan apply

Example Configuration:
```
# This is the network config written by 'subiquity'
network:
  ethernets:
    ens18:
      addresses:
      - 141.57.9.180/22
      nameservers:
        addresses:
        - 141.57.1.94
        search:
        - imn.htwk-leipzig.de
      routes:
      - to: default
        via: 141.57.11.253
  version: 2
```

[cols="1,1,1,1,1", options="header"]
|===
| Name       | IP             | SSH Port | Proxmox Node | Config

| k8s-nfs    | 141.57.9.171   | 23505    | yprox        | 4 Core + 4 GB RAM
| rancher    | 141.57.9.170   | 23505    | xprox        | 4 Core + 4 GB RAM
| k8s-node1  | 141.57.9.180   | 23505    | yprox        | 4 Core + 8 GB RAM
| k8s-node2  | 141.57.9.181   | 23505    | zprox        | 4 Core + 8 GB RAM
| k8s-node3  | 141.57.9.182   | 23505    | yprox        | 8 Core + 32 GB RAM
|===


We installed Ubuntu 22.04 on the nodes and configured the network with netplan.
The current snap version for microk8s is 1.28.0 wich is supported by the current rancher release.

```
snap install microk8s --classic --channel=1.28/stable
```

Join the groups and rights so without root

```
sudo usermod -a -G microk8s $USER

sudo mkdir -p ~/.kube

sudo chown -f -R $USER ~/.kube

newgrp microk8s # to reload shell groups without close and open
```

Check the status while Kubernetes starts

```
microk8s status --wait-ready
```

Turn on the services you want
```
microk8s enable dns
```

```
microk8s enable registry
```

```
microk8s enable rbac
```

```
microk8s enable ingress
```

Start using Kubernetes

```
microk8s kubectl get all --all-namespaces
```

Um weitere Nodes mit microk8s zu verbinden auf dem main node:

```bash
microk8s add-node
```

Example output:

```bash
From the node you wish to join to this cluster, run the following:

microk8s join 192.168.122.210:25000/b346782cc8956830924c04f2cf1b1745/dadf654db615



Use the '--worker' flag to join a node as a worker not running the control plane, eg:

microk8s join 192.168.122.210:25000/b346782cc8956830924c04f2cf1b1745/dadf654db615 --worker



If the node you are adding is not reachable through the default interface you can use one of the following:

microk8s join 192.168.122.210:25000/b346782cc8956830924c04f2cf1b1745/dadf654db615

microk8s join 192.168.123.1:25000/b346782cc8956830924c04f2cf1b1745/dadf654db615

microk8s join 172.17.0.1:25000/b346782cc8956830924c04f2cf1b1745/dadf654db615
```

Alias for easy setup copy paste commands:

```
alias helm="microk8s helm"
```

```
alias kubectl="microk8s kubectl"
```

  
If cattle-cluster-agent in cattle-system reboots multiple times and has dns resolving problems change the config.

```
kubectl edit deployment cattle-cluster-agent -n cattle-system
...
dnsPolicy: Default
```