Day 25 - Kubernetes Services
π 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:
Acts as a load balancer
Performs service discovery
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:
Any public website
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 Type | Who Can Access | Purpose |
| ClusterIP | Only inside Kubernetes | Internal services |
| NodePort | Anyone with access to Node IP | Internal org access |
| LoadBalancer | Anyone on the internet | Public 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