0
0
Kubernetesdevops~5 mins

Service discovery via DNS in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run multiple parts of an application in Kubernetes, they need to find and talk to each other. Service discovery via DNS lets these parts find each other by simple names instead of complicated IP addresses.
When you want your app components to communicate without hardcoding IP addresses.
When you deploy a database and want your app to find it by a simple name.
When you scale your app and new copies need to be reachable automatically.
When you want to connect microservices inside the same Kubernetes cluster.
When you want to avoid manual updates of service addresses after restarts.
Config File - service.yaml
service.yaml
apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: ClusterIP
---
apiVersion: v1
kind: Pod
metadata:
  name: my-app-pod
  labels:
    app: my-app
spec:
  containers:
  - name: my-app-container
    image: nginx
    ports:
    - containerPort: 8080
  dnsPolicy: ClusterFirst

This file creates a Service named my-app-service that selects pods with label app: my-app. It exposes port 80 and forwards traffic to port 8080 on the pods.

The Pod runs an nginx container labeled app: my-app so the service can find it.

The dnsPolicy: ClusterFirst ensures the pod uses Kubernetes DNS for service discovery.

Commands
This command creates the service and pod defined in the service.yaml file so they can start running and be discoverable.
Terminal
kubectl apply -f service.yaml
Expected OutputExpected
service/my-app-service created pod/my-app-pod created
Check that the pod is running and ready to receive traffic.
Terminal
kubectl get pods
Expected OutputExpected
NAME READY STATUS RESTARTS AGE my-app-pod 1/1 Running 0 10s
Inside the pod, this command looks up the DNS name of the service to verify that service discovery via DNS works.
Terminal
kubectl exec my-app-pod -- nslookup my-app-service
Expected OutputExpected
Server: 10.96.0.10 Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local Name: my-app-service.default.svc.cluster.local Address 1: 10.96.123.45
Get details about the service including its cluster IP address which pods use to connect.
Terminal
kubectl get svc my-app-service
Expected OutputExpected
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE my-app-service ClusterIP 10.96.123.45 <none> 80/TCP 1m
Key Concept

If you remember nothing else from this pattern, remember: Kubernetes automatically creates DNS names for services so pods can find each other by simple names.

Common Mistakes
Not labeling pods correctly to match the service selector
The service cannot find any pods to send traffic to, so DNS lookup works but connections fail.
Make sure pod labels exactly match the service selector labels.
Using dnsPolicy other than ClusterFirst in pods
Pods may not use Kubernetes DNS and fail to resolve service names.
Set dnsPolicy to ClusterFirst (default) to enable service DNS resolution.
Trying to access service DNS from outside the cluster without extra setup
Service DNS names only resolve inside the cluster by default.
Use port forwarding or expose the service externally if needed.
Summary
Create a Service with a selector to group pods and expose ports.
Deploy pods with matching labels and dnsPolicy ClusterFirst for DNS resolution.
Use kubectl exec with nslookup inside pods to verify service DNS names resolve.
Check service details with kubectl get svc to find cluster IP addresses.