
Kubernetes, part 2: putting k3s on one server, kubectl, access from a desktop PC, and first commands
🌐 Machine-translated from Russian. Show original
In this part:
Step 1. Server preparation#
Ubuntu Server 24.04 with static IP (examples — 192.168.1.40, k3s01name), updates, and SSH key login. The server name must be unique — it will become the node name.
sudo apt update && sudo apt full-upgrade -y
sudo hostnamectl set-hostname k3s01
If Docker is already running on the server, it's better to use a pure VM: k3s uses its own containerd, and two container engines on one machine only confuse.
Step 2. Firewall#
The k3s documentation recommends disabling ufw. If you leave it enabled (this is reasonable for a server accessible from other networks), allow APIs, pod and service networks, and HTTP(S):
sudo ufw allow from 192.168.1.0/24 to any port 6443 proto tcp # API Kubernetes — только из своей сети
sudo ufw allow from 10.42.0.0/16 to any # сеть подов k3s по умолчанию
sudo ufw allow from 10.43.0.0/16 to any # сеть сервисов k3s по умолчанию
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
Step 3. Installing k3s#
curl -sfL https://get.k3s.io | sh -s - --write-kubeconfig-mode 600
The script installs the latest version from the stable channel (as of November 2026 — v1.37.x). For production, commit the version so that all nodes are identical:
curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION="v1.37.2+k3s1" sh -s -
Current versions by channels are update.k3s.io/v1-release/channels, documentation is docs.k3s.io.
Check in a minute:
sudo k3s kubectl get nodes
# NAME STATUS ROLES AGE VERSION
# k3s01 Ready control-plane,master 1m v1.37.2+k3s1
What k3s installed#
| Component | Why |
|---|---|
| containerd | launching containers |
| Flannel | pod |
| CoreDNS | service names within the |
| Traefik | Ingress controller: accepts HTTP(S) on ports 80/443 |
| ServiceLB | a simple balancer for services like the LoadBalancer |
| local-path-provisioner | volume on the local disk of node |
| metrics-server | kubectl top: CPU and memory consumption |
| SQLite | cluster state store on a single server (in the cluster — etcd, part 5) |
Any component can be disabled during installation, for example, --disable=traefikif you want your own Ingress controller.
Files worth knowing:
| The Path | What's |
|---|---|
/etc/rancher/k3s/k3s.yaml | administrator kubeconfig — as the root password |
/etc/rancher/k3s/config.yaml | k3s parameters instead of command-line flags |
/var/lib/rancher/k3s/server/ | cluster status, tokens, certificates, |
/var/lib/rancher/k3s/server/manifests/ | manifests that K3s applies automatically |
Step 4. kubectl for your user#
mkdir -p ~/.kube
sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
sudo chown $USER: ~/.kube/config && chmod 600 ~/.kube/config
echo 'export KUBECONFIG=~/.kube/config' >> ~/.bashrc && source ~/.bashrc
echo 'source <(kubectl completion bash)' >> ~/.bashrc
kubectl get nodes
kubectl is already built into k3s (link to k3s kubectl).
Step 5. Access from a work computer#
- Install kubectl according to the official instructions (Windows:
winget install -e --id Kubernetes.kubectl). - Copy
~/.kube/configfrom the server to your%USERPROFILE%\.kube\config(Windows) or~/.kube/config(Linux/macOS) — for example, viascp. - In the file, replace
server: https://127.0.0.1:6443withhttps://192.168.1.40:6443.
kubectl get nodes -o wide
⚠️ The kubeconfig file gives full access to the cluster. Do not put it in Git, messengers, or shared folders. Do not publish port 6443 to the internet — external access is only via VPN.
A convenient terminal interface for everyday work — k9s: navigation by pods, logs, entry to the container with arrows and keys.
Step 6. First commands#
kubectl get pods -A # все поды во всех пространствах имен
kubectl create deployment hello --image=traefik/whoami --replicas=2
kubectl get pods -o wide # на каком узле, какой IP
kubectl expose deployment hello --port=80
kubectl port-forward svc/hello 8080:80 # откройте http://localhost:8080 на сервере
kubectl logs deploy/hello
kubectl delete pod -l app=hello # удалим поды — Deployment тут же создаст новые
kubectl get pods
kubectl delete deployment,service hello
Note the "self-heal": deleted pods recreate themselves because the desired state is 2 replicas.
Kubectl cheat sheet#
| Command | What does |
|---|---|
kubectl get pods,svc,ingress -n app | objects in the namespace |
kubectl describe pod имя | details and events are the first place to look in case of an error |
kubectl logs -f pod/имя (--previous) | container log (previous one, if it dropped) |
kubectl exec -it pod/имя -- sh | to enter the container |
kubectl apply -f файл.yaml | create or update objects from the |
kubectl get events -A --sort-by=.lastTimestamp | recent events of the |
kubectl top nodes / kubectl top pods -A | resource consumption |
kubectl explain deployment.spec | field help right in the |
Updating and removing k3s#
- Update is the same installation command with a new version. Before updating, read the release notes and do not skip through minor versions (1.35 → 1.37 — only through 1.36).
- Complete removal:
/usr/local/bin/k3s-uninstall.sh.
The cluster is ready. Next — Kubernetes, part 3: Pod, Deployment, Service, Ingress, ConfigMap, and Secret — deploying the real application.
Comments · 0
No comments yet.Sign in to comment, rate and see all comments.