Post

k3s Setup

k3s Setup

Prerequisites

Before starting, ensure you have:

  1. 3+ Virtual Machines (VMs)

K3s Setup Walkthrough

1. Initial Server Configuration

For our cluster, we’ll use 3 VMs running Ubuntu. Start with the first VM that will act as the master node.

2. Install K3s Master Node

Follow the official K3s quick start guide:

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

Reboot the VM after installation completes:

1
sudo reboot

3. Retrieve Node Token

After reboot, get the cluster token from the master node:

1
cat /var/lib/rancher/k3s/server/node-token

4. Join Worker Nodes

On each worker VM (second and third nodes), run:

1
2
3
curl -sfL https://get.k3s.io | \
K3S_URL=https://<MASTER_NODE_IP>:6443 \
K3S_TOKEN=<YOUR_NODE_TOKEN> sh -

Replace:

  • <MASTER_NODE_IP> with your master node’s public IP
  • <YOUR_NODE_TOKEN> with the token from Step 3

5. Verify Cluster Nodes

Return to the master node and check node status:

1
sudo kubectl get nodes

Cluster Access Configuration

1. Retrieve Kubeconfig

On the master node, display the cluster configuration:

1
sudo cat /etc/rancher/k3s/k3s.yaml

2. Local Machine Setup

  1. Install kubectl on your local machine
  2. Create ~/.kube/config file
  3. Update server field with master node’s IP:
1
2
3
clusters:
- cluster:
    server: https://<MASTER_NODE_IP>:6443

3. Environment Configuration

Set kubectl context:

1
export KUBECONFIG=~/.kube/config

Application Deployment

1. Create Deployment Manifest

Save this as nginx.yaml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
apiVersion: v1
kind: Namespace
metadata:
  name: nginx

---

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

---

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

2. Deploy Application

Apply the manifest:

1
kubectl apply -f nginx.yaml

3. Verify Deployment

Check created resources:

1
kubectl get all -n nginx

4. Access Application

Forward local port to service:

1
kubectl port-forward svc/nginx-service 8080:80 -n nginx

Visit http://localhost:8080 in your browser to verify.


Maintenance Operations

To remove deployed resources:

1
kubectl delete -f nginx.yaml

🎉 Congratulations! You’ve successfully deployed a K3s cluster and running NGINX service!


Useful References

This post is licensed under CC BY 4.0 by the author.