Kubernetes  

Kubernetes for Beginners: A Complete Guide

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:

  • Docker = creates containers

  • Kubernetes = manages containers at scale

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:

  • Automatically restarting failed containers

  • Distributing load across nodes

  • Scaling containers up/down based on demand

  • Managing rolling updates without downtime

3. Core Concepts

a. Cluster

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

  • Master Node (Control Plane): Manages the cluster (scheduling, scaling, etc.)

  • Worker Nodes: Run the actual applications (containers)

b. Node

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

  • Kubelet (communicates with the master)

  • Container runtime (e.g., Docker, containerd)

  • Kube-proxy (manages networking)

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):

  • API Server: Entry point for all commands (kubectl talks to this).

  • Controller Manager: Ensures desired state (e.g., keeps 3 pods running).

  • Scheduler: Decides which node a pod should run on.

  • etcd: Key-value store for cluster configuration and state.

Worker Node Components:

  • Kubelet: Communicates with API server, manages pods on that node.

  • Kube-proxy: Manages network routing.

  • Container Runtime: Runs containers (Docker, containerd, etc.).

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

  • Pod-to-Pod communication: Managed internally by the cluster.

  • Service: Exposes pods within or outside the cluster.

  • Ingress: Routes external HTTP/HTTPS requests to services.

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:

  • Prometheus (metrics collection)

  • Grafana (visualization)

  • ELK Stack (logging)

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

  • Self-healing: Automatically restarts failed containers.

  • Scalability: Scale up/down based on load.

  • Zero downtime: Rolling updates and rollbacks.

  • Portability: Works on any cloud or on-premise environment.

  • Automation: Manages resource allocation and health checks automatically.

14. Best Practices

  • Use Namespaces to separate environments.

  • Store configs in ConfigMaps and secrets in Secrets.

  • Use readiness and liveness probes for health checks.

  • Keep YAML files version-controlled.

  • Use Resource Limits to prevent a pod from consuming too many resources.

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.