Mastering Kubernetes: Set Up Your Own Cluster and Connect It to a Custom Domain
Have you ever felt the primal urge to run Kubernetes in your own homelab, like a tech wizard summoning magical clusters? Well, you’re in luck! In this guide, I’ll show you how to set up your own K3s cluster and connect it to your custom domain. By the end, you’ll be running a lightweight, high-availability Kubernetes cluster that’s ready for action. Let’s dive in.
Why K3s?
Let’s be honest—Kubernetes can be a bit… extra. It’s like a high-maintenance friend who needs a five-star setup just to hang out. K3s, on the other hand, is lightweight, efficient, and perfect for a homelab. It doesn’t eat up resources like a black hole devouring galaxies, making it the ideal choice for tinkering at home. Plus, installing K3s is absurdly easy, almost suspiciously so.
Installing K3s on Your Master Node
First, install K3s on your master node. For simplicity (and to avoid setting your kitchen on fire), we’ll stick to a single master node in this guide. If you want high availability, you can go multi-master later—just remember to give all your master nodes static IPs.
Here’s the magic command to get K3s running
curl -sfL https://get.k3s.io | sh -
Pro Tip: Give your master node a static IP, preferably one that’s not in your router’s DHCP range. Static IPs are like reserved parking spaces—they make life easier. Go into your router’s admin interface, find the DHCP settings, and match your node’s MAC address to an IP. Done!
To confirm the installation, run this
sudo k3s kubectl get nodes
You should see your master node listed as Ready Congrats, you’re halfway to Kubernetes greatness.
Adding Worker Nodes
Adding worker nodes is like throwing a party and inviting your buddies over to help out. Run this on each additional node:
curl -sfL https://get.k3s.io | K3S_URL=https://myserver:6443 K3S_TOKEN=mynodetoken sh -
Make Your Cluster Accessible from the Outside
Now, the real fun begins. If you want to reach your K3s cluster from anywhere (like when you’re sipping coffee at a café, feeling like a tech genius), there are three more steps:
- Port Forwarding
You need to forward traffic from port 6443 (Kubernetes’ default port) to your master node. This setting is usually hiding in your router’s admin interface under something likeAdvanced > NAT Forwarding > Port Forwarding
Every router is different, so expect a treasure hunt. - Now you need to add a a CNAME record for your domain, preferably to a random IP like
80.80.80.80
, if you have a Static IP from your ISP you can provide your Public IP(the reason I recommend a random IP is if future steps are not working, this'll let us know, if you give your correct public ip here, and there's a problem, you'll know it when your IP changes and thins are unreachable) - Set Up Dynamic DNS
Unless you’re one of the lucky few with a static IP from your ISP, you’ll need a Dynamic DNS (DDNS) solution. Why? Because your public IP changes more often than Netflix series recommendations.
For this, I useddclient
, which works with a ton of providers. Since I’m on Cloudflare, here’s my setup:
First we log in to the Cloudflare console and create an API token with zone and DNS privileges.
Then we add the token to your ddclient.conf mine looks like this
daemon=300 # Check every 300 seconds
syslog=yes # Log updates to syslog
pid=/var/run/ddclient.pid # Record PID
ssl=yes # Use TLS
use=web # Get IP from a website
web='https://cloudflare.com/cdn-cgi/trace'
web-skip='ip=' # The IP address above is after 'ip='
protocol=cloudflare, \
zone=yourdomain.com, \
ttl=1, \
password='YOUR_TOKEN', \
yourdomain.com
Now your domain will always point to the right IP. DDNS for the win!
- Configure K3s for External Access
Let K3s know it’s being served on a public address. Create a configuration file at/etc/rancher/k3s/config.yaml
(or edit it if it already exists)
tls-san:
yourdomain.com
Testing Your Setup
You’re almost there. To connect to your cluster from a client machine:
Copy /etc/rancher/k3s/k3s.yaml
from the master node to your client machine.
Open the file and replace server:
https://127.0.0.1:6443
with server:
https://yourdomain:6443
Install kubectl
on your client if you haven’t already (instructions here).
Run a quick test
kubectl get nodes
If everything’s set up correctly, you should see your cluster nodes from anywhere in the world. Cue the victory dance!
Wrapping Up
That’s it! You’ve got yourself a lightweight Kubernetes cluster, connected to your domain and accessible from the internet. It’s perfect for experimenting, learning, or deploying awesome projects.
Stay tuned for more posts where we’ll dive into configuring your K3s cluster and deploying applications. Until then, enjoy your new Kubernetes superpowers—just don’t let it go to your head.