Create a Lightweight k3s Lab for Self-Hosted Services
By following this guide, you will set up a lightweight k3s cluster capable of running self-hosted services with ingress and persistent volumes.
Expected Outcome
A fully functional k3s cluster with ingress capabilities and persistent storage for hosting self-managed applications.
Assumptions
- A machine with at least 2GB of RAM and 2 CPU cores
- Ubuntu 20.04 or later installed
- Basic knowledge of terminal commands
Bill of Materials
- Docker installed on the host machine
- kubectl command-line tool
- k3s installation script
Build Steps
- Install k3s
Run the k3s installation script to set up the lightweight Kubernetes cluster.
Safe to run: read-only
curl -sfL https://get.k3s.io | sh -
- Verify k3s installation
Check that k3s is running and accessible.
Safe to run: read-only
sudo k3s kubectl get nodes
- Install ingress controller
Deploy Traefik as the ingress controller for managing external access to your services.
Changes system state: review before running
kubectl apply -f https://raw.githubusercontent.com/traefik/traefik/v2.5/docs/content/reference/dynamic-configuration/kubernetes-ingress.yaml
- Set up persistent volumes
Create a persistent volume and persistent volume claim for your applications.
Changes system state: review before running
kubectl apply -f - <<EOF apiVersion: v1 kind: PersistentVolume metadata: name: my-pv spec: capacity: storage: 1Gi accessModes: - ReadWriteOnce hostPath: path: /mnt/data EOF kubectl apply -f - <<EOF apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-pvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi EOF - Deploy a sample application
Deploy a sample self-hosted application that uses the persistent volume and ingress.
Changes system state: review before running
kubectl apply -f - <<EOF apiVersion: apps/v1 kind: Deployment metadata: name: my-app spec: replicas: 1 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: my-app image: nginx:1.21.6 volumeMounts: - mountPath: /usr/share/nginx/html name: my-storage volumes: - name: my-storage persistentVolumeClaim: claimName: my-pvc EOF kubectl apply -f - <<EOF apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: my-app-ingress spec: rules: - host: my-app.local http: paths: - path: / pathType: Prefix backend: service: name: my-app port: number: 80 EOF
Validation
- Check that the application is running using 'kubectl get pods'.
- Access the application via the ingress URL (http://my-app.local) after updating your /etc/hosts file.
Troubleshooting
- If the pods are not running, check the logs with 'kubectl logs <pod-name>'.
- Ensure that the ingress controller is properly configured and running.
Cleanup or Rollback
- To remove the application, run 'kubectl delete -f <your-app-deployment-file.yaml>'.
- To uninstall k3s, run 'sudo /usr/local/bin/k3s-uninstall.sh'.
Next Improvements
- Explore deploying additional self-hosted services like a recipe management system.
- Consider setting up a CI/CD pipeline for automated deployments.