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
-
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
-
/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 -
-
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
-
status k3s: Confirms that the k3s server is running correctly.
Set kubeconfig
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
-
KUBECONFIG: File that tellskubectlhow 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
-
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
Pod Yaml
apiVersion: v1
kind: Pod
metadata:
name: hi-nginx
spec:
containers:
- name: nginx
image: nginx:stable-alpine
ports:
- containerPort: 80
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
-
apply: Creates or updates resources declaratively. -
-f: Uses the YAML file.
Check Pods
sudo kubectl get pods
- Shows pod status (
Running,Pending, etc.).
Organaize Files
mkdir ~/k3s-pod
mv ~/nginx-pod.yaml ~/k3s-pod/
cd ~/k3s-pod/
- Creates a directory to organize Kubernetes manifests.
Create Service (NodePort)
vim ~/k3s-pod/nginx-service.yaml
Service YAML
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
spec:
type: NodePort
selector:
app: nginx
ports:
- port: 80
targetPort: 80
nodePort: 30090
Explanation:
-
kind: Service: Exposes pods to the network. -
type: NodePort: Opens a port on every node's IP. -
selector: Finds pods with labelapp=nginx. -
port: Service port. -
targetPort: Container port. -
nodePort: 30090: External port accessible viaNodeIP:30090.
Add Label to Pod
sudo kubectl label pod hi-nginx app=nginx
- 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
Find IP Address
ip a
- Shows network interfaces.
-
eth0usually 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
- These are private IP addresses.
- Not reachable directly from the internet.
Access Nginx
curl http://10.140.11.1:30090
- Access Nginx via:
- Node.IP
- NodePort
You can laso use:
curl http://<HOST_NAME>:30090
curl http://<Global_IP>:30090
Create Deployment
vim ~/k3s-pod/nginx-deploy.yaml
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
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
Check Deployment & Pods
sudo kubectl get deployments,pods
- Confirms:
- All replicas are running
- Deployment is healthy
Rolling Update
vim ~/k3s-pod/nginx-deploy.yaml
Change image version:
image: nginx:1.21-alpine
sudo kubectl apply -f nginx-deploy.yaml
sudo kubectl rollout status deployment/nginx-deploy
- 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)