Skip to main content

Command Palette

Search for a command to run...

Day 25 - Kubernetes Services

Updated
β€’3 min read

πŸš€ Why Do We Need a Service in Kubernetes?

In real production environments:

  • We don’t deploy pods directly.

  • We deploy deployments, which internally create a ReplicaSet, which finally creates pods.

Assume we have a deployment with 3 pods (replicas).

These replicas are required because:

  • One pod cannot handle many concurrent users.

  • For example: if 1 pod handles 10 users and you have 100 users β†’ you need ~10 pods.


⚠️ Problem: What if There Is No Service in Kubernetes?

Pods get dynamic IP addresses.

Example:

  • Pod 1 β†’ 172.16.3.4

  • Pod 2 β†’ 172.16.3.5

  • Pod 3 β†’ 172.16.3.6

If a pod crashes, Kubernetes auto-heals and creates a new pod β†’ new IP address.

Example:

  • Pod 1 dies

  • New Pod 1 β†’ 172.16.3.8 (IP changed)

Now the users/testers/other internal teams who were using 172.16.3.4 cannot access the application anymore.

So even though Kubernetes healed the pod correctly β†’ application becomes unreachable due to changed IP.

This is the core reason Kubernetes Services are required.


🟦 How a Service Solves This Problem

Instead of giving direct pod IPs to users, DevOps creates a Service on top of the Deployment.

Users will now access the application using:

payment.default.svc

The service:

  1. Acts as a load balancer

  2. Performs service discovery

  3. Can expose applications externally


1️⃣ Load Balancing

The service receives traffic and distributes it across all pods.

Example:

  • 10 requests β†’ Pod1

  • 10 requests β†’ Pod2

  • 10 requests β†’ Pod3

So all pods handle load evenly.


2️⃣ Service Discovery (VERY IMPORTANT)

A service does NOT track pod IPs.

Instead, it uses:

βœ”οΈ Labels

βœ”οΈ Selectors

Example label on pods:

app: payment

If pods die and new ones are created:

  • IP address changes

  • Label remains the same

  • Service always finds the correct pods because it filters by labels.

This solves the problem of constantly changing IPs.


3️⃣ Exposing Applications Externally

Services can expose your app inside or outside the cluster depending on the service type.

There are 3 main service types:


πŸ”Ή Type 1 β€” ClusterIP (default)

  • Application accessible only inside the Kubernetes cluster

  • Provides load balancing + service discovery

  • Not accessible from outside


πŸ”Ή Type 2 β€” NodePort

  • Application accessible inside your organization / VPC

  • Anyone who can reach Worker Node IP can access the app

  • Useful for internal company apps


πŸ”Ή Type 3 β€” LoadBalancer

  • Application accessible from the internet (publicly)

  • Creates a Cloud Load Balancer (ELB on AWS)

  • Best for apps like:

Note:

  • LoadBalancer works only on cloud providers (AWS, GCP, Azure)

  • Does NOT work by default on Minikube (needs addons/metallb)


🌐 Example Summary (From Your Content)

Service TypeWho Can AccessPurpose
ClusterIPOnly inside KubernetesInternal services
NodePortAnyone with access to Node IPInternal org access
LoadBalancerAnyone on the internetPublic access

🧠 Final Summary of Your Content

Kubernetes Services provide:

βœ”οΈ Load Balancing

βœ”οΈ Service Discovery (through labels & selectors)

βœ”οΈ External Exposure (depending on type)

Without a Service:

  • Pods get new IPs when recreated

  • Users cannot access the application

  • Auto-healing breaks connectivity

With a Service:

  • Users access via a single stable address

  • Traffic is distributed

  • IP changes of pods don’t matter

More from this blog

Dinesh's Blog

104 posts