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 clusteringPractical 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.
Comments
Comments
Post a Comment