Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
K8s Networking Kubernetes Kubernetes: From Zero to Production Module 4 - Services and Networking

DNS and Service Discovery in Kubernetes

Reviewed & accurate
AI Summary

What You Will Learn

Intermediate

  • How K8s DNS works
  • Service DNS names
  • Pod DNS names

Kubernetes has built-in DNS (CoreDNS). Every Service gets a DNS name automatically. Pods can resolve Services by name - no need to know IP addresses.

Terminalbash
# Service DNS name format:
# ..svc.cluster.local

# Example:
# my-app.default.svc.cluster.local
# db.production.svc.cluster.local

# Inside a Pod, you can use short names:
curl http://my-app           # Same namespace
curl http://my-app.default   # Specify namespace
curl http://my-app.default.svc.cluster.local  # Full name
DNS-based service discovery
Pod A needs to call the database. Instead of hardcoding the DB IP (which changes), Pod A calls db.default.svc.cluster.local. DNS resolves to the Service IP, which routes to a healthy DB Pod.

DNS for StatefulSets

Terminalbash
# StatefulSet Pods get individual DNS names
# mysql-0.mysql.default.svc.cluster.local
# mysql-1.mysql.default.svc.cluster.local
# mysql-2.mysql.default.svc.cluster.local

# This is how databases find each other for clustering

Practical Exercise

Create a Service named 'api'
Exec into another Pod: kubectl exec -it busybox -- sh
Inside: nslookup api (should resolve)
Try: wget -qO- http://api (should work)
Try the full name: wget -qO- http://api.default.svc.cluster.local

Key Takeaways

  • K8s has built-in DNS (CoreDNS).
  • Service DNS: name.namespace.svc.cluster.local.
  • Short names work within the same namespace.
  • StatefulSet Pods get individual DNS names.
  • DNS-based discovery = no hardcoded IPs.
Previously: Lesson 21 covered Ingress.
Today: You learned: How services find each other by name.
Next: Lesson 23 covers NetworkPolicies.
Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments