Back to Kubernetes: Core Concepts
Q6Question 6 of 19

What are Pods, Deployments, and Services in Kubernetes

Answer: A Pod runs your container, a Deployment manages how many Pods run and how they update, and a Service gives those Pods a stable network address so other services can reach them.

Key Concepts
  • A Pod is the smallest deployable unit in Kubernetes, wrapping one or more containers that share the same network and storage.
  • A Deployment manages Pod replicas, handles rolling updates, and rolls back automatically if a new version fails.
  • A Service provides a stable network endpoint for a set of Pods, using label selectors to route traffic.
  • ClusterIP exposes a Service internally, NodePort exposes it on each node, and LoadBalancer provisions a cloud load balancer.
  • Liveness probes detect stuck containers and restart them, while readiness probes remove unhealthy Pods from Service traffic.
  • Rolling updates replace Pods one at a time with zero downtime, controlled by maxSurge and maxUnavailable settings.
  • Labels and selectors are the glue that connects Deployments to Pods and Services to Pods.

Definition

A Pod runs your container, a Deployment manages how many Pods run and how they update, and a Service gives those Pods a stable network address so other services can reach them.
  • Pod is a group of one or more containers that share the same IP address, storage volumes, and lifecycle.
  • Deployment is a controller that maintains a desired number of Pod replicas, handles rolling updates, and supports rollback.
  • ReplicaSet is created by a Deployment to ensure the specified number of Pod replicas are running at all times.
  • Service is a stable network abstraction that routes traffic to a set of Pods selected by labels.
  • Labels are key-value pairs attached to objects (Pods, Deployments) used for selection and organization.

Think of the National Bank of India call center. A Pod is a single teller station: a desk, a phone, and one or two screens. A Deployment is the staffing policy that says "keep 5 teller stations active at all times." If a teller goes on break, the Deployment automatically seats a replacement. A Service is the main phone number customers dial. No matter which teller picks up, the customer reaches the call center. The phone number does not change when tellers rotate.

Understanding the Concepts

What is a Pod?

A Pod is the smallest unit you deploy in Kubernetes. It wraps one or more containers that need to run together. All containers in a Pod share the same network namespace (same IP address), the same storage volumes, and the same lifecycle.

In most cases, a Pod runs a single container. Multi-container Pods are used for sidecar patterns: a main application container plus a logging agent or a service mesh proxy.

YAML
# pod.yaml: a simple Pod running the transfer service
apiVersion: v1
kind: Pod
metadata:
  name: transfer-pod
  namespace: banking
  labels:
    app: transfer-service
    version: v1
spec:
  containers:
  - name: transfer
    image: national-bank/transfer-service:1.0.0
    ports:
    - containerPort: 8080
    resources:
      requests:
        cpu: "250m"
        memory: "512Mi"
      limits:
        cpu: "500m"
        memory: "1Gi"
    env:
    - name: DB_URL
      value: "jdbc:postgresql://bank-db:5432/bankdb"
BASH
# Apply and inspect
kubectl apply -f pod.yaml
kubectl get pods -n banking
kubectl describe pod transfer-pod -n banking
kubectl logs transfer-pod -n banking
TEXT
NAME            READY   STATUS    RESTARTS   AGE
transfer-pod    1/1     Running   0          30s
JAVA
package com.bank.k8s;

public class PodDemo {
    public static void main(String[] args) {
        System.out.println("Pod: transfer-pod");
        System.out.println("  Container: transfer (national-bank/transfer-service:1.0.0)");
        System.out.println("  IP: 10.244.1.15 (cluster-internal)");
        System.out.println("  Port: 8080");
        System.out.println("  CPU request: 250m, limit: 500m");
        System.out.println("  Memory request: 512Mi, limit: 1Gi");
        System.out.println();
        System.out.println("Pod characteristics:");
        System.out.println("  - Ephemeral: when a Pod dies, it is gone");
        System.out.println("  - IP changes: new Pod gets a new IP");
        System.out.println("  - Never create Pods directly in production");
        System.out.println("  - Use Deployments to manage Pods");
    }
}
TEXT
Pod: transfer-pod
  Container: transfer (national-bank/transfer-service:1.0.0)
  IP: 10.244.1.15 (cluster-internal)
  Port: 8080
  CPU request: 250m, limit: 500m
  Memory request: 512Mi, limit: 1Gi

Pod characteristics:
  - Ephemeral: when a Pod dies, it is gone
  - IP changes: new Pod gets a new IP
  - Never create Pods directly in production
  - Use Deployments to manage Pods

What is a Deployment?

A Deployment tells Kubernetes: "I want 3 replicas of the transfer service running at all times." The Deployment creates a ReplicaSet, which creates the Pods. If a Pod crashes, the ReplicaSet creates a replacement. If you update the image version, the Deployment performs a rolling update.

YAML
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: transfer-service
  namespace: banking
spec:
  replicas: 3
  selector:
    matchLabels:
      app: transfer-service
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: transfer-service
        version: v1
    spec:
      containers:
      - name: transfer
        image: national-bank/transfer-service:1.0.0
        ports:
        - containerPort: 8080
        resources:
          requests:
            cpu: "250m"
            memory: "512Mi"
          limits:
            cpu: "500m"
            memory: "1Gi"
        livenessProbe:
          httpGet:
            path: /actuator/health/liveness
            port: 8080
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /actuator/health/readiness
            port: 8080
          initialDelaySeconds: 10
          periodSeconds: 5
BASH
kubectl apply -f deployment.yaml
kubectl get deployments -n banking
kubectl get replicasets -n banking
kubectl get pods -n banking
TEXT
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
transfer-service   3/3     3            3           60s

NAME                          DESIRED   CURRENT   READY   AGE
transfer-service-7d8f9b6c4f   3         3         3       60s

NAME                                READY   STATUS    AGE
transfer-service-7d8f9b6c4f-abc12   1/1     Running   60s
transfer-service-7d8f9b6c4f-def34   1/1     Running   60s
transfer-service-7d8f9b6c4f-ghi56   1/1     Running   60s
JAVA
package com.bank.k8s;

public class DeploymentDemo {
    public static void main(String[] args) {
        System.out.println("Deployment: transfer-service");
        System.out.println("  Replicas: 3");
        System.out.println("  Strategy: RollingUpdate");
        System.out.println("  maxSurge: 1 (create 1 new Pod before removing old)");
        System.out.println("  maxUnavailable: 0 (never have fewer than 3 running)");
        System.out.println();
        System.out.println("Hierarchy:");
        System.out.println("  Deployment -> ReplicaSet -> Pods");
        System.out.println("  Deployment manages the ReplicaSet");
        System.out.println("  ReplicaSet manages the Pods");
    }
}
TEXT
Deployment: transfer-service
  Replicas: 3
  Strategy: RollingUpdate
  maxSurge: 1 (create 1 new Pod before removing old)
  maxUnavailable: 0 (never have fewer than 3 running)

Hierarchy:
  Deployment -> ReplicaSet -> Pods
  Deployment manages the ReplicaSet
  ReplicaSet manages the Pods

What is a Service?

A Service provides a stable IP address and DNS name for a set of Pods. Pods are ephemeral: they get new IPs when recreated. A Service's IP never changes, and it routes traffic to healthy Pods using label selectors.

YAML
# service.yaml
apiVersion: v1
kind: Service
metadata:
  name: transfer-svc
  namespace: banking
spec:
  type: ClusterIP
  selector:
    app: transfer-service  # Routes to Pods with this label
  ports:
  - port: 80           # Service port (what callers use)
    targetPort: 8080    # Pod port (where traffic is sent)
    protocol: TCP
BASH
kubectl apply -f service.yaml
kubectl get svc -n banking

# Other services access it as:
# http://transfer-svc.banking.svc.cluster.local:80
# or: http://transfer-svc:80 (within same namespace)
TEXT
NAME           TYPE        CLUSTER-IP     PORT(S)   AGE
transfer-svc   ClusterIP   10.96.120.45   80/TCP    15s
JAVA
package com.bank.k8s;

public class ServiceDemo {
    public static void main(String[] args) {
        System.out.println("Service: transfer-svc");
        System.out.println("  Type: ClusterIP (internal only)");
        System.out.println("  ClusterIP: 10.96.120.45 (stable, never changes)");
        System.out.println("  Port: 80 -> targetPort: 8080");
        System.out.println("  Selector: app=transfer-service");
        System.out.println();
        System.out.println("How other services call it:");
        System.out.println("  http://transfer-svc:80/api/transfers");
        System.out.println("  http://transfer-svc.banking.svc.cluster.local:80");
        System.out.println();
        System.out.println("Service types:");
        System.out.println("  ClusterIP  -> internal only (default)");
        System.out.println("  NodePort   -> exposes on each node's IP:port");
        System.out.println("  LoadBalancer -> provisions a cloud load balancer");
    }
}
TEXT
Service: transfer-svc
  Type: ClusterIP (internal only)
  ClusterIP: 10.96.120.45 (stable, never changes)
  Port: 80 -> targetPort: 8080
  Selector: app=transfer-service

How other services call it:
  http://transfer-svc:80/api/transfers
  http://transfer-svc.banking.svc.cluster.local:80

Service types:
  ClusterIP  -> internal only (default)
  NodePort   -> exposes on each node's IP:port
  LoadBalancer -> provisions a cloud load balancer

How do liveness and readiness probes work?

A liveness probe checks if the container is alive. If it fails, Kubernetes restarts the container. A readiness probe checks if the container is ready to accept traffic. If it fails, the Pod is removed from the Service's endpoints.

ProbePurposeFailure action
LivenessIs the container alive?Restart the container
ReadinessCan the container handle traffic?Remove from Service endpoints
StartupHas the container finished starting?Delay liveness checks
JAVA
package com.bank.k8s;

public class ProbeDemo {
    public static void main(String[] args) {
        System.out.println("Liveness probe:");
        System.out.println("  GET /actuator/health/liveness every 10s");
        System.out.println("  If 3 consecutive failures -> restart container");
        System.out.println("  Detects: deadlocks, infinite loops, hung threads");
        System.out.println();
        System.out.println("Readiness probe:");
        System.out.println("  GET /actuator/health/readiness every 5s");
        System.out.println("  If fails -> remove Pod from Service traffic");
        System.out.println("  Detects: DB connection lost, cache warming");
        System.out.println();
        System.out.println("Startup probe:");
        System.out.println("  GET /actuator/health/liveness every 5s");
        System.out.println("  failureThreshold: 30 (allows 150s for startup)");
        System.out.println("  Protects slow-starting banking apps");
    }
}
TEXT
Liveness probe:
  GET /actuator/health/liveness every 10s
  If 3 consecutive failures -> restart container
  Detects: deadlocks, infinite loops, hung threads

Readiness probe:
  GET /actuator/health/readiness every 5s
  If fails -> remove Pod from Service traffic
  Detects: DB connection lost, cache warming

Startup probe:
  GET /actuator/health/liveness every 5s
  failureThreshold: 30 (allows 150s for startup)
  Protects slow-starting banking apps

How do rolling updates work?

When you update the image tag in a Deployment, Kubernetes performs a rolling update. It creates new Pods with the new version and terminates old Pods one at a time. Traffic shifts gradually from old to new.

BASH
# Update the image version
kubectl set image deployment/transfer-service \
  transfer=national-bank/transfer-service:2.0.0 -n banking

# Watch the rollout
kubectl rollout status deployment/transfer-service -n banking

# Rollback if something goes wrong
kubectl rollout undo deployment/transfer-service -n banking
JAVA
package com.bank.k8s;

public class RollingUpdateDemo {
    public static void main(String[] args) {
        System.out.println("Rolling update: v1.0.0 -> v2.0.0");
        System.out.println("maxSurge=1, maxUnavailable=0");
        System.out.println();
        System.out.println("Step 1: v1-pod-1(R) v1-pod-2(R) v1-pod-3(R)");
        System.out.println("  Create v2-pod-1");
        System.out.println();
        System.out.println("Step 2: v1-pod-1(R) v1-pod-2(R) v1-pod-3(R) v2-pod-1(R)");
        System.out.println("  v2-pod-1 passes readiness, terminate v1-pod-1");
        System.out.println();
        System.out.println("Step 3: v1-pod-2(R) v1-pod-3(R) v2-pod-1(R) v2-pod-2(R)");
        System.out.println("  Terminate v1-pod-2");
        System.out.println();
        System.out.println("Step 4: v1-pod-3(R) v2-pod-1(R) v2-pod-2(R) v2-pod-3(R)");
        System.out.println("  Terminate v1-pod-3");
        System.out.println();
        System.out.println("Done:   v2-pod-1(R) v2-pod-2(R) v2-pod-3(R)");
        System.out.println("Zero downtime throughout the update.");
    }
}
TEXT
Rolling update: v1.0.0 -> v2.0.0
maxSurge=1, maxUnavailable=0

Step 1: v1-pod-1(R) v1-pod-2(R) v1-pod-3(R)
  Create v2-pod-1

Step 2: v1-pod-1(R) v1-pod-2(R) v1-pod-3(R) v2-pod-1(R)
  v2-pod-1 passes readiness, terminate v1-pod-1

Step 3: v1-pod-2(R) v1-pod-3(R) v2-pod-1(R) v2-pod-2(R)
  Terminate v1-pod-2

Step 4: v1-pod-3(R) v2-pod-1(R) v2-pod-2(R) v2-pod-3(R)
  Terminate v1-pod-3

Done:   v2-pod-1(R) v2-pod-2(R) v2-pod-3(R)
Zero downtime throughout the update.

What are common mistakes with Kubernetes workloads?

Mistake 1: Creating naked Pods instead of Deployments

Wrong: creating a Pod directly. If it crashes or the node fails, it is not recreated.

BASH
# WRONG: naked Pod, no self-healing
kubectl run transfer-pod --image=national-bank/transfer-service:1.0.0
# If the Pod crashes, it stays dead.

Correct: always use a Deployment. It recreates Pods automatically.

BASH
# CORRECT: Deployment manages Pods
kubectl create deployment transfer-service \
  --image=national-bank/transfer-service:1.0.0 \
  --replicas=3
# If a Pod crashes, the Deployment creates a replacement.

Mistake 2: Not using readiness probes

Wrong: without a readiness probe, Kubernetes sends traffic to Pods that have not finished starting. Users see errors during deployments.

Correct: define a readiness probe so traffic only reaches Pods that are fully initialized.

What should a senior engineer know?

- Pod Disruption Budgets (PDB). Prevent voluntary disruptions (node drain, cluster upgrade) from taking down too many Pods at once. For banking services, set minAvailable: 2 so at least 2 Pods always handle traffic during maintenance.

- Horizontal Pod Autoscaler (HPA). Automatically scales the number of Pods based on CPU, memory, or custom metrics. During month-end salary processing at the bank, HPA can scale the transfer service from 3 to 10 Pods.

- Graceful shutdown. Configure terminationGracePeriodSeconds to give the Spring Boot application time to finish in-flight requests before the Pod is killed. For financial transactions, this prevents half-processed transfers.

- Anti-affinity rules. Spread Pods of the same Deployment across different nodes and availability zones. If a node fails, only one replica is lost, not all three.

- Init containers. Run setup tasks (database migration, config download) before the main container starts. The init container runs to completion, then the main container starts.

What are common interview follow-up questions?

  • What is the difference between a Deployment and a StatefulSet?
  • How does Kubernetes handle Pod restarts when the liveness probe fails?
  • What is a headless Service and when do you use it?
  • How do you rollback a failed deployment?
  • What is the difference between a DaemonSet and a Deployment?

Visual Overview

DIAGRAM
  Deployment -> ReplicaSet -> Pods
  =================================

  Deployment: transfer-service (replicas: 3)
       │
       └──> ReplicaSet: transfer-service-7d8f9b6c4f
                │
                ├──> Pod: transfer-...-abc12 (node: worker-1)
                ├──> Pod: transfer-...-def34 (node: worker-2)
                └──> Pod: transfer-...-ghi56 (node: worker-3)


  Service -> Pod Routing
  ========================

  Client request
       │
       ▼
  Service: transfer-svc (ClusterIP: 10.96.120.45:80)
       │
       │  selector: app=transfer-service
       │
       ├──> Pod abc12 (10.244.1.15:8080)  Ready
       ├──> Pod def34 (10.244.2.22:8080)  Ready
       └──> Pod ghi56 (10.244.3.10:8080)  Not Ready (removed)

  Service only routes to Ready Pods.


  Rolling Update: v1 -> v2
  =========================

  Step 1: [v1] [v1] [v1]        (3 old Pods)
  Step 2: [v1] [v1] [v1] [v2]   (create new)
  Step 3: [v1] [v1] [v2] [v2]   (v2 ready, remove v1)
  Step 4: [v1] [v2] [v2] [v2]   (continue)
  Step 5: [v2] [v2] [v2]        (done, zero downtime)

Key Takeaway

Pods, Deployments, and Services are the three objects you use every day in Kubernetes. A Pod runs your banking application container. A Deployment ensures the right number of Pods are always running, handles rolling updates when you ship a new version, and rolls back automatically if the new version fails. A Service gives those Pods a stable address so other services can call them without knowing which Pod or which node they are on.

Labels are the connection mechanism. The Deployment creates Pods with app: transfer-service. The Service selects Pods with app: transfer-service. When a Pod is created or removed, the Service automatically updates its routing. You never hardcode Pod IPs.

Liveness and readiness probes are essential for banking services. A liveness probe detects stuck containers and restarts them. A readiness probe detects containers that cannot handle traffic (database connection lost, cache warming) and removes them from the Service. Without probes, Kubernetes sends traffic to broken Pods and customers see errors.

Rolling updates give you zero-downtime deployments. Kubernetes creates Pods with the new version, waits for them to pass the readiness probe, and then removes the old Pods one by one. At no point does the number of healthy Pods drop below the desired count. If the new version fails, kubectl rollout undo reverts to the previous version in seconds.

One sentence to remember: a Pod runs the container, a Deployment keeps the right number of Pods running, and a Service gives them a stable network address.

---

Share
Use ← → arrow keys to navigate