DEV Community

Shelner
Shelner

Posted on

K3s server setup guide using Ubuntu22

I explain what each command does and focusing on k3s and Kubernetes concepts.

Set up Ubuntu

  • Login using SSH
  • Update & Install tools: sudo apt update && sudo apt install -y curl vim git

Disable Swap (Required for Kubernetes)

sudo swapoff -a
Enter fullscreen mode Exit fullscreen mode
  • swap: Disk space used as virtual memory.
  • Why disable swap? Kubernetes requires swap to be disabled because it expects full control over memory management.
sudo sed -i '/ swap / s/^/#/' /etc/fstab
Enter fullscreen mode Exit fullscreen mode
  • /etc/fstab: File that defines filesystems mounted at boot.
  • sed -i: Edits the file in place.
  • This command comments out swap entries, so swap stays disabled after reboot.

Install K3s

curl -sfL https://get.k3s.io | sh -
Enter fullscreen mode Exit fullscreen mode
  • curl -sfL:
    • -s: silent
    • -f: fail on error
    • -L: follow redirects
  • https://get.k3s.io: Official k3s installation script.
  • | sh -: Pipes the script directly into the shell and executes it.

This installs:

  • K3s server
  • containerd
  • kubectl (boundled)

Check K3s Service

sudo systemctl status k3s
Enter fullscreen mode Exit fullscreen mode
  • status k3s: Confirms that the k3s server is running correctly.

Set kubeconfig

export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
Enter fullscreen mode Exit fullscreen mode
  • KUBECONFIG: File that tells kubectl how to connect to the cluster.
  • /etc/rancher/k3s/k3s.yaml: Default kubeconfig generated by k3s.

※Without this, kubectl may not work for non-root users.


Verify Cluster

sudo kubectl get nodes
Enter fullscreen mode Exit fullscreen mode
  • kubectl: Kubernetes command-line tool.
  • get nodes: Shows all nodes in the cluster.
  • Output confirms:
    • k3s is running
    • this VM is acting as a control-plane + worker node

Create Nginx Pod

vim ~/nginx-pod.yaml
Enter fullscreen mode Exit fullscreen mode

Pod Yaml

apiVersion: v1
kind: Pod
metadata:
  name: hi-nginx
spec:
  containers:
  - name: nginx
    image: nginx:stable-alpine
    ports:
    - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

Explanation

  • apiVersion: v1: Core Kubernetes API.
  • kind: Pod: Smallest deployable unit in Kubernetes.
  • metadata.name: Pod name.
  • spec.containers: List of containers inside the pod
  • image: nginx:stable-alpine: Lightweight Nginx image
  • containerPort: 80: Port exposed inside the container.

Create Pod

sudo kubectl apply -f ~/nginx-pod.yaml
Enter fullscreen mode Exit fullscreen mode
  • apply: Creates or updates resources declaratively.
  • -f: Uses the YAML file.

Check Pods

sudo kubectl get pods
Enter fullscreen mode Exit fullscreen mode
  • Shows pod status (Running, Pending, etc.).

Organaize Files

mkdir ~/k3s-pod
mv ~/nginx-pod.yaml ~/k3s-pod/
cd ~/k3s-pod/
Enter fullscreen mode Exit fullscreen mode
  • Creates a directory to organize Kubernetes manifests.

Create Service (NodePort)

vim ~/k3s-pod/nginx-service.yaml
Enter fullscreen mode Exit fullscreen mode

Service YAML

apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30090
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • kind: Service: Exposes pods to the network.
  • type: NodePort: Opens a port on every node's IP.
  • selector: Finds pods with label app=nginx.
  • port: Service port.
  • targetPort: Container port.
  • nodePort: 30090: External port accessible via NodeIP:30090.

Add Label to Pod

sudo kubectl label pod hi-nginx app=nginx
Enter fullscreen mode Exit fullscreen mode
  • Labels are key-vakye pairs used by Services and Deployments.
  • Required so the Service can find the Pod.

Create Service

sudo kubectl apply -f ~/k3s-pod/nginx-service.yaml
Enter fullscreen mode Exit fullscreen mode

Find IP Address

ip a
Enter fullscreen mode Exit fullscreen mode
  • Shows network interfaces.
  • eth0 usually holds the node IP.

Private IP Ranges

10.0.0.0 - 10.255.255.255
172.16.0.0 - 172.31.255.255
192.168.0.0 - 192.168.255.255
Enter fullscreen mode Exit fullscreen mode
  • These are private IP addresses.
  • Not reachable directly from the internet.

Access Nginx

curl http://10.140.11.1:30090
Enter fullscreen mode Exit fullscreen mode
  • Access Nginx via:
    • Node.IP
    • NodePort

You can laso use:

curl http://<HOST_NAME>:30090
curl http://<Global_IP>:30090
Enter fullscreen mode Exit fullscreen mode

Create Deployment

vim ~/k3s-pod/nginx-deploy.yaml
Enter fullscreen mode Exit fullscreen mode

Deployment YAML

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deploy
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:stable-alpine
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • kind: Deployment: Manages Pods automatically.
  • replicas: 3: Runs 3 indentical pods.
  • selector.matchLabels: Connects Deployment <-> Pods
  • template: Pod definition used by Deployment.

Deploy Deployment

sudo kubectl apply -f ~/k3s-pod/nginx-deploy.yaml
Enter fullscreen mode Exit fullscreen mode

Check Deployment & Pods

sudo kubectl get deployments,pods
Enter fullscreen mode Exit fullscreen mode
  • Confirms:
    • All replicas are running
    • Deployment is healthy

Rolling Update

vim ~/k3s-pod/nginx-deploy.yaml
Enter fullscreen mode Exit fullscreen mode

Change image version:

image: nginx:1.21-alpine
Enter fullscreen mode Exit fullscreen mode
sudo kubectl apply -f nginx-deploy.yaml
sudo kubectl rollout status deployment/nginx-deploy
Enter fullscreen mode Exit fullscreen mode
  • Rolling update:
    • Old pods are replaced gradually
    • Zero downtime
  • rollout status: Shows update progress.

Summary

You have successfully learned:

  • k3s installation
  • Pod vs Deployment
  • Service (NodePort)
  • Labels & selectors
  • Rolling updates
  • Accessing services from outside the cluster

Top comments (0)