# Kubernetes as IoT Base (for Home Assistant, etc.)

I have quite a few applications that are already containerized and managed by Docker Compose. Maintaining the application-level Docker Compose YAML files is a minor task. However, there are a few small pieces of the puzzle that still require manual updates or upgrades from time to time. So, I've made the decision to migrate, and this is my first blog post documenting the process.

---
### Install k3s

```shell
curl -sfL https://get.k3s.io | sh -s - --no-deploy=traefik --default-local-storage-path=/opt/k3s
# setup kubectl autocompletion
echo "source <(kubectl completion bash)" >> ~/.bashrc
```

I have enabled only the minimum services:

```bash
kubectl get pod -n kube-system
NAME                                      READY   STATUS    RESTARTS   AGE
local-path-provisioner-58fb86bdfd-n2znx   1/1     Running   0          45d
coredns-d798c9dd-d7tcx                    1/1     Running   0          45d
svclb-traefik-frmgz                       2/2     Running   0          42d
traefik-667499d69f-jtkbk                  1/1     Running   0          42d
```

### Install Helm

```shell
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
echo "export KUBECONFIG=/etc/rancher/k3s/k3s.yaml" >> ~/.bashrc
helm repo add stable https://kubernetes-charts.storage.googleapis.com
# (optional) add emqx charts repo
helm repo add emqx https://repos.emqx.io/charts
```

### Install Traefik

```bash
helm install traefik stable/traefik --set dashboard.enabled=true,dashboard.domain=traefik-dash.example.com,serviceType=LoadBalancer,rbac.enabled=true,dashboard.auth.basic.admin='x2@*#$*******####',acme.enabled=true,acme.domains.enabled=true,acme.challengeType=dns-01 --namespace=kube-system
```

For my actual setup, I use a `values.yaml` as shown below:

```yaml
# Example values for my ingress
# This is a YAML-formatted file.
# Tip: use htpasswd to generate passwords. e.g. htpasswd -nb admin admin
dashboard:
  enabled: true
  domain: traefik-dash.example.com
  auth:
    basic:
      admin: 'x2@*#$*******####'
serviceType: LoadBalancer
rbac:
  enabled: true
ssl:
  enabled: true
  enforced: false
acme:
  enabled: true
  email: my.email@gmail.com
  staging: false
  logging: false
  persistence:
    storageClass: local-path
  domains:
    enabled: true
    domainsList:
      - main: "*.example.com"
  challengeType: dns-01
  dnsProvider:
    name: cloudflare
    cloudflare:
      CLOUDFLARE_EMAIL: "my.email@gmail.com"
      CLOUDFLARE_API_KEY: "32xxx****##########$$$$"
```

### Install Home Assistant

```bash
# (optional) install emqx for mqtt service
helm install emqx stable/emqx 
helm install hass stable/home-assistant --set ingress.enabled=true,ingress.hosts={'hass.example.com'},configurator.enabled=true,configurator.ingress.enabled=true,configurator.ingress.hosts={'hass-config.example.com'},hostNetwork=true
```

### TODO

k3s uses containerd as the default container runtime, so Docker and Docker Compose (for applications not yet migrated to k8s) can coexist on the same node. I plan to migrate Pi-hole to the cluster later.
