# Kubernetes

Before beginning, make sure you have `kubectl` and `helm` installed.

This README describes the process of setting up the Kubernetes infrastructure used for Gotography.
It should be read from top to bottom and executed in the same order.

## Permissions (permissions.yaml)

Using the administrator's certificate (found on any of the control plane nodes), create the roles to allow administration of the cluster by the team members.

```bash
    kubectl apply -f permissions.yaml
```

```bash
    # Create certificate signing request (client)
    openssl req -new -key <key>.pem -out <user>.csr -subj "/CN=<user>"

    # Sign certificate (server)
    openssl x509 -req -in <user>.csr -CA /etc/kubernetes/pki/ca.crt -CAkey /etc/kubernetes/pki/ca.key -CAcreateserial -out <user>.crt -days 300
```

The user certificate and key can then be compiled into a `kubectl` compatible configuration.

## Labels for correct database placement

Next, add labels `DC1` and `DC2` to the respective nodes `worker01` and `worker02`.
This is used to determine where to schedule the ScyllaDB pods so the NetworkTopologyStrategy can make decisions where to replicate data to.
(It is called `DC1` and `DC2` for legacy reasons, `rack1` and `rack2` would be correct as only rack-aware replication is used.)

```bash
    kubectl label node worker01.dsy dc=DC1
    kubectl label node worker02.dsy dc=DC2
```

## Initial configuration (init.yaml)

Create separate namespaces for Gotography, MetalLB and Traefik.

```bash
    kubectl apply -f init.yaml
```

## Installation of MetalLB

As we're running on bare-metal, an ingress load balancer is needed.
For this, we'll use [MetalLB](https://metallb.universe.tf/).

```bash
helm repo add metallb https://metallb.github.io/metallb
helm install metallb metallb/metallb --version 0.13.9 --namespace metallb-system
```

## Installation of Traefik

We are using [Traefik](https://traefik.io/traefik/) as our proxy, installed using [helm](https://helm.sh/).

```bash
helm repo add traefik https://traefik.github.io/charts
helm install traefik traefik/traefik --version 23.0.1 --namespace traefik-system --values traefik-values.yaml
```

## Deployment of resources

Now everything is ready for the applications.
It is recommended to follow the file order, especially to provide some local storage to ScyllaDB before deploying that.

```bash
    kubectl apply -f storage.yaml    # Local storage for ScyllaDB
    kubectl apply -f scylla.yaml     # 2x2 Scylla instances
    kubectl apply -f classify.yaml   # 2 Classifier instances
    kubectl apply -f gotography.yaml # 2 Backend instances
    kubectl apply -f web.yaml        # 2 Frontend instances
    kubectl apply -f ingress.yaml    # 1 Ingress to the frontend
```
