All articles
Article 3 min read

Building a Robust Self-Hosted Environment with Docker and Traefik

Transforming a VPS into a self-hosted powerhouse using Docker, K3s, and Traefik simplifies application management while ensuring secure HTTPS connectivity.

Introduction

In recent years, self-hosting has gained traction as individuals and businesses seek to regain control over their data and applications. Utilizing technologies such as Docker, K3s, and Traefik allows you to create an efficient and resilient cloud infrastructure on a budget. In this post, we'll guide you through setting up a self-hosted environment on a VPS, ensuring automatic HTTPS for secure connections.

Why Choose Docker and K3s?

Docker enables developers to package applications and their dependencies into containers, streamlining deployment across environments. K3s, a lightweight Kubernetes distribution, simplifies the orchestration of these containers. This combination allows for easy scaling and management of multiple services. Together, they provide a robust solution for self-hosting various applications.

Key Benefits:

Portability: Docker containers can run anywhere, ensuring consistency across development and production.

Scalability: K3s simplifies managing containerized applications by allowing you to scale services with minimal effort.

Resource Efficiency: Both Docker and K3s are designed to be lightweight, making them ideal for VPS environments.

Getting Started with Your VPS

Assuming you’re working with a VPS provider that gives you root access, here’s how to prepare your server for Docker and K3s.

1. Update Your VPS

Before installation, ensure your server’s software packages are up to date. You can achieve this with the following commands:

bash
sudo apt update
sudo apt upgrade -y

2. Install Docker

Docker can be installed easily using the official installation script. Run the following command to install Docker on your server:

bash
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

Once installed, add your user to the Docker group for non-root access:

bash
sudo usermod -aG docker $USER

Log out and back in to ensure the group change takes effect.

3. Install K3s

K3s installation is straightforward. The following command installs K3s as a service:

bash
curl -sfL https://get.k3s.io | sh -

To check if K3s is running correctly, use the command:

bash
sudo systemctl status k3s

Deploying Applications with K3s

After setting up K3s, you can begin deploying applications using Kubernetes manifests or Helm charts. For this demonstration, let’s deploy an example application using a YAML file.

Example Application Manifest

Create a file named app-deployment.yaml with the following content:

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: nginx:latest
        ports:
        - containerPort: 80

Apply the Manifest

Use the kubectl command to deploy the application:

bash
kubectl apply -f app-deployment.yaml

To expose the application, create a service manifest:

yaml
apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  type: ClusterIP
  selector:
    app: my-app
  ports:
    - port: 80
      targetPort: 80

Apply the service manifest:

bash
kubectl apply -f service.yaml

Setting Up Traefik for Automatic HTTPS

Traefik is an essential tool that acts as a reverse proxy and load balancer. It can automatically obtain and renew SSL certificates via Let's Encrypt, enhancing the security of your self-hosted applications.

1. Traefik Configuration

Create a traefik.yaml configuration file:

yaml
apiVersion: helm.cattle.io/v1
kind: HelmChart
metadata:
  name: traefik
  namespace: kube-system
spec:
  chart: traefik
  version: 10.0.0
  repo: https://helm.traefik.io/traefik
  valuesContent: |-
    providers:
      kubernetesIngress:
        ingressClass: traefik
    certificatesResolvers:
      myresolver:
        acme:
          email: your-email@example.com
          storage: /letsencrypt/acme.json
          httpChallenge:
            entryPoint: web

2. Deploy Traefik

Deploy Traefik using the Helm command:

bash
helm install traefik tra