1. Introduction

Kubernetes (often abbreviated as K8s) is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications.

While Docker helps you create and run containers, Kubernetes helps you run and manage containers across multiple servers (a cluster) efficiently. It eliminates manual work such as restarting crashed containers, load balancing traffic, or scaling the number of containers up and down.

In short:

2. Why Kubernetes Exists

Running a few containers manually is easy. But managing hundreds or thousands of containers across multiple machines quickly becomes complex.
Kubernetes solves this by:

3. Core Concepts

a. Cluster

A Kubernetes cluster is the foundation. It has two types of machines:

b. Node

A node is a single machine (physical or virtual) that runs your containers.
Each node contains:

c. Pod

The smallest deployable unit in Kubernetes.

A pod wraps one or more containers that share storage, network, and configuration.

You rarely run individual containers directly in Kubernetes — you run them inside pods.

Example:
A web app and its helper logging container run together in one pod.

d. Deployment

Defines how many pods you want, what image to use, and how to update them.

Kubernetes automatically keeps the desired number of pods running.

Example:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: web
        image: nginx
        ports:
        - containerPort: 80

Run it:

kubectl apply -f deployment.yaml

e. Service

A service exposes pods to the network.
Pods are temporary — when they restart, their IPs change. A service provides a stable IP and DNS name to reach them.

Example:

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

f. Namespace

Used to divide cluster resources logically.

You can have different environments (e.g., dev, staging, prod) in the same cluster.

4. Kubernetes Architecture

Control Plane (Master Node Components):

Worker Node Components:

5. Basic Workflow

  1. You define what you want (desired state) in a YAML file (e.g., 3 replicas of a web app).

  2. You apply it:

    kubectl apply -f deployment.yaml
    
  3. Kubernetes schedules pods on nodes.

  4. If a pod crashes, the controller recreates it.

  5. If traffic increases, you scale replicas:

    kubectl scale deployment web-app --replicas=5
    
  6. The service ensures all pods are reachable through a single endpoint.

6. Common Kubernetes Objects

ObjectPurpose
PodSmallest deployable unit containing one or more containers
ReplicaSetEnsures a specified number of pods are running
DeploymentManages ReplicaSets and rolling updates
ServiceProvides stable networking for pods
ConfigMapStores configuration data
SecretStores sensitive data (passwords, keys)
IngressManages external HTTP/HTTPS access
VolumePersists data beyond container lifecycle
NamespaceLogical grouping of resources

7. Networking in Kubernetes

Example ingress:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: web-ingress
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: web-service
            port:
              number: 80

8. Scaling and Load Balancing

Kubernetes can automatically scale pods based on CPU or memory usage using Horizontal Pod Autoscaler (HPA).

Example:

kubectl autoscale deployment web-app --min=2 --max=10 --cpu-percent=80

Load balancing happens automatically through Services, distributing traffic evenly across pods.

9. Storage and Persistence

Containers are temporary. To persist data, Kubernetes provides Volumes and PersistentVolumes (PVs).
Developers request storage via PersistentVolumeClaims (PVCs).

Example PVC:

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

10. Monitoring and Logging

Kubernetes provides metrics and logging integration with tools like:

Command to check pod logs:

kubectl logs pod-name

11. Common Commands

CommandDescription
kubectl get podsList running pods
kubectl get deploymentsList deployments
kubectl get servicesList services
kubectl describe pod pod-nameShow detailed info
kubectl delete pod pod-nameDelete a pod
kubectl apply -f file.yamlApply configuration
kubectl exec -it pod-name -- bashAccess container shell

12. Kubernetes vs Docker

FeatureDockerKubernetes
FocusContainerizationOrchestration
ScopeSingle hostMulti-host cluster
ScalingManualAutomatic
Load BalancingManual setupBuilt-in
Fault ToleranceNoneAutomatic pod recovery

Note: Docker and Kubernetes are complementary, not competitors. Kubernetes runs Docker containers (or other runtimes) at scale.

13. Why Use Kubernetes

14. Best Practices

15. Summary

Kubernetes is the industry-standard platform for automating containerized applications.

It abstracts infrastructure complexity and ensures your apps are always available, scalable, and resilient.