DNS in Kubernetes

What is DNS in Kubernetes?

DNS (Domain Name System) in Kubernetes is used to enable service discovery. Instead of using IP addresses, services can communicate using DNS names.

How Does Kubernetes DNS Work?

  • Each service in Kubernetes gets a DNS name automatically.
  • The built-in CoreDNS runs inside the cluster to resolve service names.
  • Pods can resolve service names like my-service.default.svc.cluster.local.

Example: Querying a Service Using DNS

You can test Kubernetes DNS with a simple command inside a pod:

nslookup my-service.default.svc.cluster.local

Creating a Service with a DNS Name

When you create a service, Kubernetes assigns it a DNS name automatically.

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080

Once deployed, other pods can access this service using the name my-service.

DNS Resolution in Different Namespaces

If a service exists in another namespace, use the full DNS name:

nslookup my-service.my-namespace.svc.cluster.local

Conclusion

Kubernetes DNS simplifies communication between services by resolving names instead of relying on IP addresses.