Service Discovery and DNS in Kubernetes
Learn how containers find each other: cluster DNS, Service objects, and the resolution flow that makes microservices addressable by name.
Service Discovery and DNS in Kubernetes is a free Linux Networking & TCP/IP for Developers lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Linux Networking & TCP/IP for Developers learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Discovery Problem
Container IPs are ephemeral — pods restart and get new addresses constantly. Hardcoding IPs is impossible.
Service discovery lets workloads find each other by stable names instead of changing IPs.
The Service Abstraction
A Kubernetes Service provides a stable virtual IP (ClusterIP) and DNS name that load-balances across a set of pods selected by labels.
apiVersion: v1
kind: Service
metadata:
name: orders
spec:
selector:
app: orders
ports:
- port: 80Cluster DNS
Every cluster runs a DNS server (CoreDNS). It automatically creates records for each Service so workloads can resolve them by name.
The DNS Naming Scheme
Services follow a predictable FQDN pattern:
service.namespace.svc.cluster.local
Within the same namespace, just orders works. Across namespaces use orders.payments.
curl http://orders.payments.svc.cluster.localHow a Pod Resolves Names
Each pod's /etc/resolv.conf points at the cluster DNS and includes search domains, so short names expand to full Service FQDNs automatically.
cat /etc/resolv.confClusterIP Load Balancing
A ClusterIP is virtual — kube-proxy programs iptables or IPVS rules so traffic to it is distributed across the healthy backing pods. Clients never see individual pod IPs.
Headless Services
Set clusterIP: None for a headless Service. DNS then returns the individual pod IPs directly, which is ideal for stateful sets and client-side load balancing.
spec:
clusterIP: None
selector:
app: dbEndpoints and EndpointSlices
Behind each Service, Kubernetes maintains EndpointSlices listing the ready pod IPs. As pods come and go, these update so DNS and routing stay current.
kubectl get endpointslicesResolving from Inside a Pod
You can debug discovery by running nslookup from a pod to verify a Service name resolves to its ClusterIP.
kubectl exec -it shell -- nslookup ordersExternal Name Services
An ExternalName Service maps a cluster DNS name to an outside hostname via a CNAME, letting you reference external systems with cluster-internal names.
spec:
type: ExternalName
externalName: api.example.comCommon Discovery Failures
When name resolution fails, check:
- Label selector mismatch (no endpoints)
- Wrong namespace in the FQDN
- CoreDNS pods unhealthy
- NetworkPolicy blocking DNS (port 53)
Quick Check
Test your service discovery knowledge.
Recap
You now understand container service discovery:
- Services give stable ClusterIPs and DNS names
- CoreDNS resolves
service.namespace.svc.cluster.local - EndpointSlices track ready pods
- Headless and ExternalName Services for special cases
- Debug with
nslookupfrom inside pods
This builds on your Docker network modes and Kubernetes networking basics.
Frequently asked questions
Is the “Service Discovery and DNS in Kubernetes” lesson free?
Yes — the full text of “Service Discovery and DNS in Kubernetes” is free to read here on the web, and the Linux Networking & TCP/IP for Developers course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Linux Networking & TCP/IP for Developers course, upgrade to CoddyKit PRO.
What will I learn in “Service Discovery and DNS in Kubernetes”?
Learn how containers find each other: cluster DNS, Service objects, and the resolution flow that makes microservices addressable by name. You practise Linux Networking & TCP/IP for Developers with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Linux Networking & TCP/IP for Developers?
No prior experience is required. Linux Networking & TCP/IP for Developers on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Service Discovery and DNS in Kubernetes” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Linux Networking & TCP/IP for Developers lesson?
Yes. Every Linux Networking & TCP/IP for Developers lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Docker Network Modes
- Kubernetes Networking Basics
- Network Policies in Containers
- Service Discovery and DNS in Kubernetes