What if your apps could always find each other, no matter how often servers change?
Why Service discovery via DNS in Kubernetes? - Purpose & Use Cases
Imagine you have many apps running on different servers, and each app needs to find and talk to others. You write down each server's address by hand and update your apps every time something changes.
This manual way is slow and confusing. Servers can change IPs or move, and your apps get lost trying to find each other. It's like calling a friend who keeps changing their phone number without telling you.
Service discovery via DNS lets apps ask a simple name to find others automatically. When servers change, DNS updates the addresses behind the scenes, so apps always connect to the right place without manual work.
app.connect('192.168.1.10:8080') # hardcoded IP address
app.connect('my-service.default.svc.cluster.local:8080') # DNS name resolves automatically
It makes your apps smart enough to find each other anytime, even when things move or change, without you lifting a finger.
In Kubernetes, when a new version of a service starts on a different pod, DNS updates so other pods always reach the latest version by name, not by IP.
Manual IP management is slow and error-prone.
DNS-based service discovery automates finding services.
Apps stay connected even when infrastructure changes.
Practice
Solution
Step 1: Understand service discovery basics
Kubernetes provides a way for apps to find each other by name instead of IP, which can change.Step 2: Identify the mechanism Kubernetes uses
Kubernetes automatically creates DNS entries for services, enabling DNS-based discovery.Final Answer:
DNS-based service discovery -> Option BQuick Check:
Service discovery = DNS-based [OK]
- Thinking IPs are fixed and manually configured
- Confusing load balancers with service discovery
- Assuming static routing is used
myapp in namespace prod?Solution
Step 1: Recall Kubernetes DNS naming convention
The format is<service>.<namespace>.svc.cluster.local.Step 2: Match the service and namespace
For servicemyappin namespaceprod, the DNS name ismyapp.prod.svc.cluster.local.Final Answer:
myapp.prod.svc.cluster.local -> Option CQuick Check:
Service.Namespace.svc.cluster.local = B [OK]
- Swapping service and namespace order
- Omitting 'svc' in the DNS name
- Using incorrect domain suffix
dev trying to reach a service named backend, what DNS name should the pod use to connect to the service?Solution
Step 1: Understand the DNS naming pattern
Kubernetes services are accessible viaservice.namespace.svc.cluster.local.Step 2: Apply the pattern for service
The correct DNS name isbackendin namespacedevbackend.dev.svc.cluster.local.Final Answer:
backend.dev.svc.cluster.local -> Option AQuick Check:
Service.Namespace.svc.cluster.local = A [OK]
- Mixing up namespace and service order
- Leaving out 'svc' in the DNS name
- Using incomplete domain suffix
db.prod.svc.cluster.local but fails. Which of these is the most likely cause?Solution
Step 1: Check if the service exists in the namespace
If the servicedbis missing inprod, DNS lookup fails.Step 2: Consider DNS and pod namespace
Using DNS name requires the service to exist; pod namespace or IP usage doesn't cause DNS failure.Final Answer:
The servicedbdoes not exist in namespaceprod-> Option DQuick Check:
Missing service = DNS failure [OK]
- Assuming DNS is always enabled
- Confusing pod namespace with service namespace
- Thinking IP use breaks DNS resolution
api in namespaces dev and prod. How can a pod in dev namespace connect to the api service in prod namespace using DNS?Solution
Step 1: Understand cross-namespace DNS resolution
Within a namespace, service names resolve locally; cross-namespace requires full DNS name.Step 2: Apply full DNS name for cross-namespace access
To reachapiinprodfromdev, useapi.prod.svc.cluster.local.Final Answer:
Use the full DNS nameapi.prod.svc.cluster.local-> Option AQuick Check:
Cross-namespace = full DNS name [OK]
- Assuming service name alone resolves across namespaces
- Using wrong namespace in DNS name
- Relying on IP addresses which can change
Ready to prove it?
Pass the test and this pattern moves unknown
