Sunday, June 28, 2026

Complete Guide to Kubernetes (K8s) with Real-Time Example | Beginner to Advanced

Introduction

Modern applications need to be scalable, highly available, fault tolerant, and easy to deploy. Running applications manually on servers is difficult because:

  • Servers can fail

  • Traffic increases unexpectedly

  • Deployments may cause downtime

  • Managing multiple servers becomes complicated

This is where Kubernetes (K8s) comes in.

Kubernetes is an open-source container orchestration platform that automates deployment, scaling, networking, and management of containerized applications.

If Docker packages your application into containers, Kubernetes manages those containers.


What is Kubernetes?

Kubernetes (commonly called K8s) is a container orchestration platform originally developed by Google and now maintained by the Cloud Native Computing Foundation (CNCF).

It automatically:

  • Deploys applications

  • Scales applications

  • Restarts failed containers

  • Performs rolling updates

  • Load balances traffic

  • Manages storage

  • Handles networking

Think of Kubernetes as the operating system for your data center.


Why Do We Need Kubernetes?

Imagine you have an ASP.NET Core Web API.

Initially:

1 Server
1 Docker Container

Everything works fine.

Now imagine:

  • 50,000 users

  • Traffic spikes

  • Server crashes

  • Need zero downtime deployment

Managing everything manually becomes impossible.

Kubernetes automates these tasks.


Without Kubernetes

Users

   |

Docker Container

   |

Linux Server

Problems:

  • Single point of failure

  • Manual scaling

  • Manual restart

  • Downtime during deployment


With Kubernetes

Users
   |
Load Balancer
   |
--------------------------
|     |      |          |
Pod1  Pod2   Pod3     Pod4
--------------------------
        |
    Kubernetes

Benefits:

  • Auto Healing

  • Auto Scaling

  • Load Balancing

  • High Availability


Kubernetes Architecture

graph TD

User --> API

API --> Scheduler
API --> Controller
API --> ETCD

Scheduler --> Worker1
Scheduler --> Worker2

Worker1 --> Pod1
Worker1 --> Pod2

Worker2 --> Pod3
Worker2 --> Pod4

Kubernetes Components

Master Node (Control Plane)

Responsible for managing the cluster.

Contains:

  • API Server

  • Scheduler

  • Controller Manager

  • ETCD


Worker Node

Runs the actual applications.

Contains:

  • Kubelet

  • Kube Proxy

  • Container Runtime

  • Pods


Kubernetes Objects

The most commonly used objects are:

  • Pod

  • ReplicaSet

  • Deployment

  • Service

  • ConfigMap

  • Secret

  • Namespace

  • Volume

  • StatefulSet

  • DaemonSet

  • Job

  • CronJob

  • Ingress

Let's learn each one.


Pod

A Pod is the smallest deployable unit in Kubernetes.

One Pod may contain:

  • One Container

  • Multiple Containers

Example:

Pod

|

Docker Container

|

ASP.NET Core API

Pod YAML

apiVersion: v1

kind: Pod

metadata:
  name: employee-api

spec:
  containers:
  - name: employee-api

    image: employeeapi:v1

    ports:
    - containerPort: 80

Deploy

kubectl apply -f pod.yaml

ReplicaSet

Maintains the desired number of Pods.

Example

Desired Pods = 3

Pod1

Pod2

Pod3

If Pod2 crashes

ReplicaSet creates another Pod automatically.


Deployment

Deployment manages ReplicaSets.

Features:

  • Rolling Updates

  • Rollback

  • Version Control

  • Scaling


Deployment YAML

apiVersion: apps/v1

kind: Deployment

metadata:
  name: employee-api

spec:

  replicas: 3

  selector:

    matchLabels:

      app: employee-api

  template:

    metadata:

      labels:

        app: employee-api

    spec:

      containers:

      - name: employee-api

        image: employeeapi:v1

        ports:

        - containerPort: 80

Deploy

kubectl apply -f deployment.yaml

Service

Pods have dynamic IP addresses.

A Service provides a stable endpoint.

Types:

  • ClusterIP

  • NodePort

  • LoadBalancer

  • ExternalName


Service YAML

apiVersion: v1

kind: Service

metadata:

  name: employee-service

spec:

  selector:

    app: employee-api

  ports:

  - port: 80

    targetPort: 80

  type: LoadBalancer

ConfigMap

Stores configuration values.

Example

Database Name

Environment

API URL

ConfigMap YAML

apiVersion: v1

kind: ConfigMap

metadata:

  name: employee-config

data:

  DB_NAME: EmployeeDB

  Environment: Production

Secret

Stores sensitive data.

Examples

  • Password

  • API Key

  • Connection String

Example

kind: Secret

apiVersion: v1

metadata:

  name: sql-secret

type: Opaque

data:

  Password: MTIzNDU2

Namespace

Used to isolate applications.

Example

Production

Development

Testing

Volume

Containers are temporary.

Data disappears after restart.

Volumes provide persistent storage.

Examples

  • Azure Disk

  • AWS EBS

  • NFS

  • Local Storage


StatefulSet

Used for:

  • SQL Server

  • MongoDB

  • Redis

  • Kafka

Provides:

  • Stable hostname

  • Persistent storage


DaemonSet

Runs one Pod on every node.

Examples:

  • Fluentd

  • Prometheus Node Exporter

  • Monitoring Agent


Job

Runs once and exits.

Example

Generate Monthly Report

CronJob

Runs on schedule.

Example

Every Midnight

Backup Database

Ingress

Acts like an HTTP Router.

Instead of exposing multiple LoadBalancers.

company.com/api

company.com/admin

company.com/orders

Kubernetes Networking

Internet

|

Ingress

|

Service

|

Pods

Every Pod gets:

  • Unique IP

  • Internal DNS


Kubernetes Scaling

Manual

kubectl scale deployment employee-api --replicas=5

Automatic

Horizontal Pod Autoscaler

CPU > 80%

Pods

3 → 6

Rolling Update

Current Version

v1

v1

v1

Deploy v2

v2

v1

v1

Then

v2

v2

v1

Finally

v2

v2

v2

No downtime.


Rollback

kubectl rollout undo deployment employee-api

Useful kubectl Commands

View Pods

kubectl get pods

View Deployments

kubectl get deployments

View Services

kubectl get svc

Describe Pod

kubectl describe pod employee-api

Delete Pod

kubectl delete pod employee-api

Logs

kubectl logs employee-api

Execute inside Pod

kubectl exec -it employee-api -- bash

Real-Time Example

Imagine an E-Commerce application.

Architecture

Internet

|

Load Balancer

|

Ingress

|

----------------------------------

Product API

Order API

Payment API

Authentication API

----------------------------------

|

SQL Server

Redis

RabbitMQ

Traffic:

Morning

200 Users

Afternoon Sale

15,000 Users

Kubernetes automatically

  • Creates more Pods

  • Distributes traffic

  • Removes unhealthy Pods

  • Performs rolling deployment

  • Maintains availability

When traffic decreases:

20 Pods

↓

5 Pods

saving infrastructure cost.


Kubernetes in Azure (AKS)

Azure Kubernetes Service (AKS) is Microsoft's managed Kubernetes offering.

Advantages:

  • Automatic upgrades

  • Integrated Azure Active Directory authentication

  • Azure Monitor integration

  • Azure Container Registry support

  • Autoscaling

  • Simplified cluster management


Kubernetes in AWS (EKS)

Amazon Elastic Kubernetes Service (EKS)

Features:

  • Managed Control Plane

  • IAM Integration

  • CloudWatch Monitoring

  • Elastic Load Balancer

  • Auto Scaling Groups


Kubernetes in Google Cloud (GKE)

Google Kubernetes Engine (GKE)

Features:

  • Auto Repair

  • Auto Upgrade

  • Cloud Logging

  • Cloud Monitoring

  • Cost Optimization


Kubernetes Best Practices

  • Use Deployments instead of directly creating Pods.

  • Store secrets in Kubernetes Secrets, not in YAML files or source code.

  • Use ConfigMaps for application configuration.

  • Set CPU and memory requests/limits for every container.

  • Implement Health Probes (Liveness and Readiness).

  • Organize workloads using Namespaces.

  • Apply Role-Based Access Control (RBAC) with least privilege.

  • Use Horizontal Pod Autoscaler for dynamic scaling.

  • Maintain multiple replicas for high availability.

  • Keep Kubernetes versions up to date and use rolling updates.


Common Mistakes Beginners Make

  • Running applications without replicas.

  • Storing passwords in plain text.

  • Forgetting resource limits.

  • Not using readiness and liveness probes.

  • Ignoring persistent storage for databases.

  • Deploying databases as standard Deployments instead of StatefulSets.

  • Skipping monitoring and logging.

  • Exposing services unnecessarily to the public internet.


Kubernetes Interview Questions

1. What is Kubernetes?

A container orchestration platform that automates deployment, scaling, networking, and management of containerized applications.

2. Difference between Pod and Deployment?

A Pod is the smallest deployable unit. A Deployment manages Pods, scaling, updates, and rollbacks.

3. What is ReplicaSet?

Ensures the specified number of Pod replicas are always running.

4. What is Ingress?

A resource that manages external HTTP/HTTPS access to services within the cluster.

5. Difference between ConfigMap and Secret?

ConfigMaps store non-sensitive configuration, while Secrets store sensitive information (such as passwords or API keys) in a form intended for secure handling.

6. What is Auto Scaling?

Automatically increases or decreases the number of Pods based on metrics such as CPU or memory usage.

7. What is ETCD?

A distributed key-value store that maintains the cluster's configuration and state.

8. What is Kubelet?

An agent that runs on each worker node and ensures containers are running as specified.

9. What is a StatefulSet?

A workload resource designed for stateful applications that require stable identities and persistent storage.

10. Why is Kubernetes popular?

Because it provides portability across cloud providers, high availability, automated scaling, self-healing, rolling updates, and a rich ecosystem for managing containerized applications.


Conclusion

Kubernetes has become the de facto standard for orchestrating containerized applications in modern cloud-native environments. By automating deployment, scaling, networking, self-healing, and updates, it helps teams build highly available and resilient applications with minimal manual intervention.

Whether you're deploying a simple ASP.NET Core Web API or managing a large-scale microservices platform with hundreds of services, understanding Kubernetes is an essential skill for today's DevOps engineers, cloud architects, and full-stack developers. Mastering its core concepts—Pods, Deployments, Services, ConfigMaps, Secrets, Ingress, StatefulSets, and Autoscaling—will enable you to build production-ready applications that can scale confidently as your business grows.


No comments:

Don't Copy

Protected by Copyscape Online Plagiarism Checker