Skip to main content

Deploy a Service with k3s and Quickly Publish to the Internet

1. Introduction​

When learning something new, I always like to get a minimal demo running first. This way, I can continuously build upon it with new knowledge - a manifestation of "success breeds success."

This blog post is an example of my minimal demo while learning Kubernetes. I chose k3s because it can run on lightweight servers, and Cloudflare to avoid the hassle of SSL certificate configuration.

The following content is based on these prerequisites:

  • A Hong Kong cloud server with at least 1 CPU core and 1GB RAM
    • Server OS: Debian
Akile Aff | [HKLite-One CPU 1 Core | RAM 1024 M Disk 5 GB | Bandwidth 5000M 1000G/month | Throttled to shared 10Mbps after quota Traffic Reset ¥7.00 IPv4 1 | IPv6 1 ¥9.99/month]

[HKLite-One CPU 1 Core | RAM 1024 M Disk 5 GB | Bandwidth 5000M 1000G/month | Throttled to shared 10Mbps after quota Traffic Reset ¥7.00 IPv4 1 | IPv6 1 ¥9.99/month]

Order link: https://akile.io/shop/server?type=traffic&areaId=3&nodeId=1&planId=819&aff_code=c29f0219-83cb-4abf-88b2-be17da2c1987

  • Cloudflare account + a domain name
    • Domain uses Cloudflare for DNS resolution

Let's get started!

2. Installing k3s​

Why k3s?

Since this is just a minimal demo, one command gets it done!

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

After execution, check k3s status with systemctl status k3s

"Running" status indicates the service is working properly

What if something goes wrong and you want to reinstall?

Execute /usr/local/bin/k3s-uninstall.sh to uninstall directly.

Both /usr/local/bin/k3s-uninstall.sh and /usr/local/bin/k3s-killall.sh are useful when you can't resolve various issues and want to start fresh.

3. Deploy a Simple HTTP Service in k3s​

3.1 Create a Test Deployment​

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

apiVersion: apps/v1
kind: Deployment
metadata:
name: http-test
spec:
replicas: 1
selector:
matchLabels:
app: http-test
template:
metadata:
labels:
app: http-test
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80

3.2 Create a Service to Expose the Port​

Create a file test-service.yaml with the following content:

apiVersion: v1
kind: Service
metadata:
name: http-test-service
spec:
selector:
app: http-test
ports:
- protocol: TCP
port: 80
targetPort: 80

3.3 Apply the Configuration​

kubectl apply -f test-deployment.yaml
kubectl apply -f test-service.yaml

Check if the service deployed successfully - "running" status indicates success

kubectl get pods

3.4 Configure Ingress Routing (Using Traefik)​

k3s comes with Traefik as the default Ingress Controller, so you can create Ingress resources directly.

3.4.1 Create Ingress Rules​

Create a file test-ingress.yaml with the following content:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: http-test-ingress
spec:
rules:
- host: your-domain.com # Replace with your domain (e.g., test.example.com)
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: http-test-service
port:
number: 80

3.5 Apply Ingress​

kubectl apply -f test-ingress.yaml

The test service is now deployed, but accessing via IP will still show "404 page not found"

Don't worry - next we need to configure DNS resolution on Cloudflare

4. Configure DNS Resolution on Cloudflare​

Add a DNS record

Wait about 30 seconds, then access via domain name to see the nginx welcome page