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:
sudo apt update
sudo apt upgrade -y2. Install Docker
Docker can be installed easily using the official installation script. Run the following command to install Docker on your server:
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.shOnce installed, add your user to the Docker group for non-root access:
sudo usermod -aG docker $USERLog 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:
curl -sfL https://get.k3s.io | sh -To check if K3s is running correctly, use the command:
sudo systemctl status k3sDeploying 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:
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: 80Apply the Manifest
Use the kubectl command to deploy the application:
kubectl apply -f app-deployment.yamlTo expose the application, create a service manifest:
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
type: ClusterIP
selector:
app: my-app
ports:
- port: 80
targetPort: 80Apply the service manifest:
kubectl apply -f service.yamlSetting 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:
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: web2. Deploy Traefik
Deploy Traefik using the Helm command:
helm install traefik tra