DEV Community

Imran
Imran

Posted on

🧠 The Ultimate Kubernetes Guide — From Zero to Pro (With Hands-On YAML)

Kubernetes is the operating system of the cloud—and understanding it is essential for modern DevOps, SaaS, and full-stack development. Whether you're a beginner or looking to deepen your knowledge, this tutorial will walk you through every core concept with real-world examples.

📦 What is Kubernetes?

Kubernetes (K8s) is an open-source container orchestration platform that helps you deploy, scale, and manage containerized applications.

Imagine running 100+ microservices. How do you keep them alive, ensure networking, balance traffic, and roll out updates safely? That’s what Kubernetes automates.

🔧 Why Use Kubernetes?

  • 🚀 Auto-scaling and load balancing
  • 🔁 Self-healing (crash recovery, restart)
  • 🛠️ Rolling updates with zero downtime
  • 🔐 Secrets & configuration management
  • 🗃️ Persistent storage
  • 🌐 Ingress + traffic routing
  • 🧱 Infrastructure-as-Code (YAML)

🛠️ Kubernetes Core Components (Visualized)

Component Description
Pod Smallest unit in K8s. Runs 1+ containers.
Deployment Manages pods & versions over time.
Service Exposes your pods within/outside the cluster.
Ingress Manages HTTP/HTTPS routing to services.
ConfigMap Externalizes config data (non-sensitive).
Secret Injects sensitive data (passwords, tokens).
PVC Persistent storage requests for stateful apps.
Node A VM or physical machine in the cluster.

🧱 Hands-On: Create a Complete K8s App Stack

We'll now walk through creating:

  1. A simple Node.js app
  2. A Deployment to manage it
  3. A Service to expose it
  4. A ConfigMap and Secret
  5. A PersistentVolumeClaim

📁 Step 1: deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-app
          image: node:18
          command: ["node", "-e", "require('http').createServer((_,res)=>res.end('Hi')).listen(3000)"]
          ports:
            - containerPort: 3000
Enter fullscreen mode Exit fullscreen mode

✅ What it does:

  • Launches 2 pods of a Node.js app
  • Uses node:18 as base image
  • Exposes port 3000 inside the container

🌐 Step 2: service.yaml

apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  type: NodePort
  selector:
    app: my-app
  ports:
    - port: 80
      targetPort: 3000
      nodePort: 30080

Enter fullscreen mode Exit fullscreen mode

✅ What it does:

  • Makes the app available via port 30080on your node
  • Load-balances between the pods
  • Accepts requests on port 80, routes to 3000

⚙️ Step 3: configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  APP_ENV: production

Enter fullscreen mode Exit fullscreen mode

✅ What it does:

  • Stores non-sensitive environment variables
  • Can be injected into your pod like this:
envFrom:
  - configMapRef:
      name: app-config
Enter fullscreen mode Exit fullscreen mode

🔐 Step 4: secret.yaml

apiVersion: v1
kind: Secret
metadata:
  name: db-secret
type: Opaque
data:
  DB_PASSWORD: bXlTZWNyZXRQYXNz

Enter fullscreen mode Exit fullscreen mode

✅ What it does:

  • Encodes secrets in base64 (e.g. mySecretPass)
  • You can inject it in your pod:
env:
  - name: DB_PASSWORD
    valueFrom:
      secretKeyRef:
        name: db-secret
        key: DB_PASSWORD
Enter fullscreen mode Exit fullscreen mode

💾 Step 5:pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Enter fullscreen mode Exit fullscreen mode

✅ What it does:

  • Requests 1 GB of storage
  • You can mount it into your pod at /data:
volumes:
  - name: storage
    persistentVolumeClaim:
      claimName: my-pvc

volumeMounts:
  - name: storage
    mountPath: /data

Enter fullscreen mode Exit fullscreen mode

🚀 Running Everything

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f configmap.yaml
kubectl apply -f secret.yaml
kubectl apply -f pvc.yaml

Enter fullscreen mode Exit fullscreen mode

📚 Bonus: Useful K8s Commands

kubectl get pods
kubectl get svc
kubectl describe pod <pod-name>
kubectl logs <pod-name>
kubectl delete -f deployment.yaml

Enter fullscreen mode Exit fullscreen mode

❤️ Support & Follow

If you found this helpful:

  • 🧠 Follow me for more DevOps + Cloud tutorials
  • 💬 Comment your Kubernetes use-case
  • ⭐ Save this post for later

Top comments (0)