Skip to content

Kubernetes

K3s Cluster Forming (Server & Agent Nodes)

K3s makes it easy to form a lightweight Kubernetes cluster by running a server node (control plane) and connecting agent nodes (workers) to it.


Prerequisite:

  • All nodes must have network connectivity to each other.

  • Install script: https://get.k3s.io

  • Required ports (6443, 8472/UDP, etc.) must be open.


Command Syntax for Server (Control Plane):

Terminal window
curl -sfL https://get.k3s.io | sh -s - server \
--node-name <server-name> \
--cluster-init

Parameters & Options (Server):

ParameterTypeDescription
--node-nameflagCustom name for the server node
--cluster-initflagInitializes a new K3s cluster (1st node)

Common Use Case:

Terminal window
curl -sfL https://get.k3s.io | sh -s - server --node-name master-1 --cluster-init

Sample Output:

[INFO] Starting k3s server...
[INFO] k3s is up and running on https://<server-ip>:6443

Command Syntax for Agent (Worker Node):

Terminal window
curl -sfL https://get.k3s.io | K3S_URL=https://<server-ip>:6443 \
K3S_TOKEN=<token> \
sh -s - agent \
--node-name <agent-name>

Parameters & Options (Agent):

ParameterTypeDescription
K3S_URLenvAPI server URL (control plane)
K3S_TOKENenvCluster join token from server
--node-nameflagName of the agent node

Get the Token (on Server):

Terminal window
sudo cat /var/lib/rancher/k3s/server/node-token

Example (Agent):

Terminal window
curl -sfL https://get.k3s.io | K3S_URL=https://192.168.1.10:6443 \
K3S_TOKEN=K1057c7... \
sh -s - agent --node-name worker-1

Sample Output (Agent):

[INFO] Starting k3s agent...
[INFO] Connecting to https://192.168.1.10:6443
[INFO] k3s agent is up and running

Managing K3s Service with systemctl

Use these commands to check the status, restart, stop, or start the K3s service on your node.

Helpful for troubleshooting, service maintenance, or applying configuration changes.


Prerequisite:

You need sudo privileges on the node where K3s is installed.

Command Syntax & Usage:

CommandDescription
sudo systemctl status k3sCheck the current status of K3s service
sudo systemctl restart k3sRestart the K3s service
sudo systemctl stop k3sStop the K3s service
sudo systemctl start k3sStart the K3s service

Common Patterns or Use Cases:

  • Check if K3s is running:
Terminal window
sudo systemctl status k3s
  • Restart K3s to apply config changes or recover from issues:
Terminal window
sudo systemctl restart k3s
  • Stop K3s service when maintenance is needed:
Terminal window
sudo systemctl stop k3s
  • Start K3s after it has been stopped:
Terminal window
sudo systemctl start k3s

Sample Output for status:

● k3s.service - Lightweight Kubernetes
Loaded: loaded (/etc/systemd/system/k3s.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2025-05-28 09:10:01 UTC; 2h 15min ago
Main PID: 1234 (k3s-server)
Tasks: 34 (limit: 4915)
Memory: 250.0M
CPU: 3min 22.567s

Applying Kubernetes Configuration in K3s

Applies or updates resources defined in a YAML/JSON manifest file.

Used to create, update, or delete Kubernetes resources declaratively.

Prerequisite:

Have the manifest file (file.yaml) ready locally or accessible by your terminal.

Ensure correct namespace is specified if resource is namespace-scoped.

Command Syntax:

Terminal window
kubectl apply -f <file.yaml> -n <namespace>

Parameters & Options:

ParameterTypeDescription
-f <file.yaml>flagPath to the YAML or JSON manifest file
-nflagNamespace to apply the resource to

Common Patterns or Use Cases:

Apply a deployment manifest to a namespace:

Terminal window
kubectl apply -f deployment.yaml -n app

Sample Output:

deployment.apps/myapp created
service/myservice configured
configmap/myconfig unchanged

Getting Namespaces in K3s

List all namespaces in the cluster. Namespaces help organize and isolate resources.

Command Syntax:

Terminal window
kubectl get ns

Parameters & Options:

ParameterTypeDescription
nsargShort form for namespace

Common Use Case:

  • List all namespaces:
Terminal window
kubectl get ns

Sample Output:

NAME STATUS AGE
default Active 10d
kube-system Active 10d
kube-public Active 10d
kube-node-lease Active 10d

Getting Kubernetes Resources in K3s

Displays a list of commonly used Kubernetes resources such as Pods, Nodes, Services, and Endpoints. Helps monitor resource status, troubleshoot issues, and verify deployments.

Prerequisite:

None — just ensure your cluster is running and kubectl is configured (e.g., k3s kubectl or alias).

Command Syntax:

Terminal window
kubectl get <resource> -n <namespace> [flags]

Parameters & Options:

ParameterTypeDescription
<resource>argResource type like pods, svc, nodes
-nflagSpecifies the namespace (where applicable)
-o wideflagShows more details like IP, node, etc.
-A or --all-namespacesflagLists resources across all namespaces

Common Patterns or Use Cases:

Get all pods in a specific namespace

Terminal window
kubectl get pods -n default

Get all nodes in the cluster

Terminal window
kubectl get nodes

Get all services in a namespace

Terminal window
kubectl get svc -n kube-system

Get endpoints in a namespace

Terminal window
kubectl get endpoints -n app

Get pods with extended info (e.g., IPs, node names)

Terminal window
kubectl get pods -o wide -n erpnext

Get all services across all namespaces

Terminal window
kubectl get svc -A

Sample Output:

Pods

Terminal window
NAME READY STATUS IP NODE AGE
nginx-deployment-65fdb85b49-7jdt4 1/1 Running 10.42.0.5 k3s-node1 5m

Nodes

Terminal window
NAME STATUS ROLES AGE VERSION
k3s-master Ready control-plane,master 20h v1.27.3+k3s1

Services

Terminal window
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 20h

Endpoints

Terminal window
NAME ENDPOINTS AGE
nginx 10.42.1.23:80 4m

Describing a Pod in K3s

Displays detailed information about a specific pod, including container specs, events, volume mounts, and state transitions.

Useful for debugging issues like scheduling problems, container crashes, and init delays.

Prerequisite:

You need the pod name and namespace

Command Syntax:

Terminal window
kubectl describe pod <pod-name> -n <namespace>

Parameters & Options:

ParameterTypeDescription
<pod-name>argName of the pod to describe
-nflagSpecifies the namespace of the pod

Common Patterns or Use Cases:

Describe a pod to troubleshoot issues like CrashLoopBackOff:

Terminal window
kubectl describe pod nginx-5d5f74c8f9-abcde -n web

Sample Output:

Name: nginx-5d5f74c8f9-abcde
Namespace: web
Priority: 0
Node: k3s-node/192.168.1.100
Start Time: Tue, 28 May 2025 10:21:01 +0000
Labels: app=nginx
Status: Running
IP: 10.42.0.12
Containers:
nginx:
Image: nginx:latest
Port: 80/TCP
State: Running
Ready: True
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m default-scheduler Successfully assigned web/nginx-xxx to k3s-node

Getting Pod Logs in K3s

Displays the logs of a specific pod’s container.

Helps identify errors, monitor application output, and debug running containers.

Prerequisite:

You need the pod name and namespace

Command Syntax:

Terminal window
kubectl logs <pod-name> -n <namespace> [flags]

Parameters & Options:

ParameterTypeDescription
<pod-name>argName of the pod
-nflagSpecifies the namespace
-c <container>flagSpecify container name if multiple exist
-fflagFollow the logs output (stream logs)

Common Patterns or Use Cases:

  • Get logs of a pod:
Terminal window
kubectl logs myapp-pod-12345 -n mynamespace
  • Follow logs live:
Terminal window
kubectl logs -f myapp-pod-12345 -n mynamespace
  • Get logs from a specific container in a pod:
Terminal window
kubectl logs myapp-pod-12345 -c sidecar-container -n mynamespace

Sample Output:

2025-05-28T12:00:01.123Z INFO Starting server on port 8080
2025-05-28T12:00:05.456Z INFO Connection established with database
2025-05-28T12:05:12.789Z ERROR Failed to fetch user data

Executing Commands Inside a Pod in K3s

Allows you to run commands interactively or non-interactively inside a container of a pod.

Useful for troubleshooting, debugging, or running administrative commands inside the pod.

Prerequisite:

You need the pod name and namespace

Get pod name with:

Terminal window
kubectl get pods -n <namespace>

Command Syntax:

Terminal window
kubectl exec -it <pod-name> -n <namespace> -- <command>

Parameters & Options:

ParameterTypeDescription
<pod-name>argName of the pod
-nflagNamespace where the pod resides
-iflagPass stdin to the container (interactive mode)
-tflagAllocate a pseudo-TTY (for terminal)
<command>argThe command to run inside the pod’s container

Common Patterns or Use Cases:

  • Run a single command (e.g., list files):
Terminal window
k3s kubectl exec -it mypod -n default -- ls /app
  • Run interactive bash shell:
Terminal window
k3s kubectl exec -it mypod -n default -- /bin/bash

Sample Output:

# /bin/sh inside nginx-pod
$ ls -l /usr/share/nginx/html
total 4
-rw-r--r-- 1 root root 612 May 28 12:00 index.html

Port Forwarding to a Pod in K3s

Allows you to forward one or more local ports to a port on a pod.

Useful for debugging or accessing a pod’s service locally without exposing it via a service or ingress.

Prerequisite:

You need the pod name , namespace and the ports you want to forward.

Command Syntax:

Terminal window
kubectl port-forward pod/<pod-name> <local-port>:<pod-port> -n <namespace>

Parameters & Options:

ParameterTypeDescription
pod/<pod-name>argName of the pod
<local-port>argLocal machine port to listen on
<pod-port>argPort on the pod to forward traffic to
-nflagNamespace

Common Patterns or Use Cases:

Forward local port 8080 to pod port 80:

Terminal window
kubectl port-forward pod/myapp-pod 8080:80 -n default

Sample Output:

Forwarding from 127.0.0.1:8080 -> 80
Handling connection for 8080

Viewing Pod Performance Metrics in K3s

Displays CPU and memory usage of pods to monitor resource consumption and troubleshoot performance issues.

Prerequisite:

Metrics Server must be installed and running in the cluster.

You can verify with:

Terminal window
kubectl get deployment metrics-server -n kube-system

Command Syntax:

Terminal window
kubectl top pods -n <namespace> [flags]

Parameters & Options:

ParameterTypeDescription
-nflagNamespace to list pod metrics from
--containersflagShow metrics per container (optional)

Common Patterns or Use Cases:

  • Show CPU and memory usage of all pods in a namespace:
Terminal window
kubectl top pods -n default
  • Show metrics per container inside pods:
Terminal window
kubectl top pods -n default --containers

Sample Output:

NAME CPU(cores) MEMORY(bytes)
nginx-5d5f74c8f9-abcde 10m 20Mi
redis-master-765d459796-xyz 5m 15Mi

Getting Pod Details in YAML Format in K3s

Outputs the full detailed configuration and status of a pod in YAML format.

Useful for deep inspection, debugging, or exporting pod specs.

Prerequisite:

You need the pod name and namespace

Command Syntax:

Terminal window
kubectl get pod <pod-name> -n <namespace> -o yaml

Parameters & Options:

ParameterTypeDescription
<pod-name>argName of the pod
-nflagNamespace of the pod
-o yamlflagOutput format in YAML

Common Patterns or Use Cases:

  • Get detailed YAML description of a pod:
Terminal window
kubectl get pod myapp-pod -n default -o yaml

Sample Output:

apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
namespace: default
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: myapp:latest
ports:
- containerPort: 8080
status:
phase: Running
podIP: 10.42.1.15

Deleting Pods in K3s

Deletes one or more pods by name or label selector.

Useful for removing faulty or unneeded pods manually.

Prerequisite:

You need the pod name and namespace


Command Syntax:

Terminal window
kubectl delete pod <pod-name> -n <namespace>

Or delete multiple pods by label selector:

Terminal window
kubectl delete pod -l <label-selector> -n <namespace>

Parameters & Options:

ParameterTypeDescription
<pod-name>argName of the pod to delete
-nflagNamespace of the pod
-l <selector>flagLabel selector to delete matching pods

Common Patterns or Use Cases:

  • Delete a single pod:
Terminal window
kubectl delete pod myapp-pod -n default
  • Delete all pods with label app=nginx:
Terminal window
kubectl delete pod -l app=nginx -n default

Sample Output:

pod "myapp-pod" deleted

Pruning Resources in Kubernetes (Kubectl)

Kubernetes can achieve pruning (cleaning up unused resources) with:

  • kubectl apply --prune — deletes resources not defined in your current manifests.

  • Manual deletion using label selectors.

Command Syntax:

Terminal window
kubectl apply -f <directory-or-file> --prune -l <label-selector> -n <namespace>

Parameters & Options:

ParameterTypeDescription
-fflagDirectory or file path containing manifests
--pruneflagEnables pruning of resources not in the manifest
-l <label>flagLabel selector to identify resources to prune
-nflagNamespace to apply the pruning

Common Patterns or Use Cases:

  • Apply manifests and prune resources without the label app=myapp in namespace default:
Terminal window
kubectl apply -f ./manifests --prune -l app=myapp -n default

Sample Output:

deployment.apps/myapp configured
service/myapp-service configured
pod/myapp-pod deleted
configmap/old-config deleted

Scaling Deployments in K3s

Use kubectl scale to manually increase or decrease the number of pod replicas in a deployment or other scalable resource.

Command Syntax:

Terminal window
kubectl scale deployment <deployment-name> --replicas=<count> -n <namespace>

Parameters & Options:

ParameterTypeDescription
<deployment-name>argName of the deployment
--replicasflagNumber of desired replicas
-nflagNamespace where the deployment is located

Common Patterns or Use Cases:

  • Scale a deployment to 5 replicas:
Terminal window
kubectl scale deployment myapp-deployment --replicas=5 -n default
  • Scale down to a single replica:
Terminal window
kubectl scale deployment myapp-deployment --replicas=1 -n default

Sample Output:

deployment.apps/myapp-deployment scaled

Cordoning & Uncordoning Nodes in K3s

Used to safely manage node scheduling during maintenance. Cordoning prevents new pods from being scheduled on the node, while uncordoning re-enables scheduling.

Cordon a Node

Prevents new pods from being scheduled on a specific node. Does not affect running pods.

Command Syntax:

Terminal window
kubectl cordon <node-name>

Parameters & Options:

ParameterTypeDescription
<node-name>argName of the node to cordon

Common Use Case:

Terminal window
kubectl cordon worker-node-1

Sample Output:

node/worker-node-1 cordoned

Uncordon a Node

Re-enables scheduling of new pods on the node.

Command Syntax:

Terminal window
kubectl uncordon <node-name>

Parameters & Options:

ParameterTypeDescription
<node-name>argName of the node to uncordon

Common Use Case:

Terminal window
kubectl uncordon worker-node-1

Sample Output:

node/worker-node-1 uncordoned