Xenia's Website

Deploying to Kubernetes

I have a small addiction with Kubernetes. I run K8S on my homelab of 3 Dell Optiplexes, as well as doing K8S all day at my job. It's a lot of fun. So when I needed to host this website, I decided to do it on my cluster rather than on some cloud provider. Just for starters, I have Cloudflare with DDNS setup to my home IP address.

My first step is containerising the website to run with Docker. I'll use Nginx for my webserver, and the config is pretty simple. Here!

nginx.conf
🔴 🟡 🟢

server { listen 80; server_name localhost; location / { if ($request_uri ~ ^/(.*)\.html(\?|$)) { return 302 /$1; } try_files $uri $uri.html $uri/ =404; root /www/data/web; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }

Now that works, all I need to do is create the Dockerfile.

Dockerfile
🔴 🟡 🟢

FROM nginx:perl RUN mkdir -p /www/data/web COPY ./ /www/data/web/ COPY ./website.conf /etc/nginx/conf.d/default.conf

This isn't exactly best practices as I have to rebuild the docker image everytime I make a change, and redeploy it. But, it's not like this site is huge either, I could probably run it from a MilkV Duo :P.
At some point in the future, I'll create a Github action to auto-build the Docker image and push to Dockerhub. However, for now, it's manual (which is fine for me anyway).

The last step is deploying the image to Kubernetes. I do this through a handful of manifests which dictate the state of the deployment.

Deployment.yaml (i got too lazy to add colours)
🔴 🟡 🟢

apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: nyanyamachine name: nyanyamachine spec: replicas: 1 selector: matchLabels: app: nyanyamachine strategy: {} template: metadata: creationTimestamp: null labels: app: nyanyamachine spec: containers: - image: nyanyamachine:latest name: nyanyamachine resources: {} status: {}

And finally create the service to expose the deployment.

Service.yaml
🔴 🟡 🟢

apiVersion: v1 kind: Service metadata: name: nyanyamachine-service labels: app: nyanyamachine spec: selector: app: nyanyamachine ports: - protocol: TCP port: 80 targetPort: 80 type: LoadBalancer

At this point, I can create an Ingress, but instead I'll go ahead and make an entry in Caddy, my reverse proxy. This is more convenient for me than Ingress with Certmanager as I have had issues in the past.