In this lesson, you’ll learn how Kubernetes implements stable service discovery for your applications. We’ll demonstrate two primary mechanisms:Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
- Environment Variables injected into pods at launch
- Cluster DNS names resolving service endpoints
nginx-service deployed in the default namespace as our example.
Service Overview
First, confirm that thenginx-service exists in the default namespace:
1. Environment Variable–Based Discovery
When a pod starts, the kubelet injects environment variables for each Service in the same namespace. Let’s verify this with a temporary pod:nginx-service:
1.1 Limitations Across Namespaces
Environment variables are only injected into pods in the same namespace as the Service.
kube-system namespace:
nginx-service resides in default.
2. Cluster DNS–Based Discovery
Kubernetes also runs a DNS server (CoreDNS or kube-dns) that resolves Service names cluster-wide. Pods automatically get DNS settings in/etc/resolv.conf:
2.1 Verifying DNS Resolution
Install DNS utilities and lookup the Service FQDN:2.2 Shortened DNS Names
Sincesvc.cluster.local is in your search domains, you can use a shorter name:
10.103.206.194.
Comparison of Discovery Methods
| Method | Scope | Example Usage |
|---|---|---|
| Environment Variables | Same namespace only | echo $NGINX_SERVICE_SERVICE_HOST |
| Cluster DNS | Cluster-wide | curl http://nginx-service.default.svc.cluster.local |
Summary
- Environment Variables
Injected per namespace by the kubelet. Simple but limited to in-namespace communication. - Cluster DNS
Provides cross-namespace, cluster-wide name resolution. Requires CoreDNS or kube-dns running.