Retrieve the full name of a service in Kubernetes
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Kubernetes, service discovery depends on DNS names that include both service and namespace context. Many issues that look like network failures are actually naming mismatches. Knowing how to retrieve and construct the full service name correctly makes debugging and cross-namespace communication much easier.
What the Full Service Name Looks Like
A Kubernetes service DNS name normally follows this pattern:
service-name.namespace.svc.cluster.local
Parts:
service-name: the metadata name of the Service object.namespace: namespace where the service lives.svc: fixed DNS subdomain for services.cluster.local: cluster domain default, configurable in some clusters.
Inside the same namespace, many clients can use just service-name. For cross-namespace calls, use at least service-name.namespace.
Retrieve Service Name and Namespace with kubectl
Start by fetching canonical metadata from the API.
Construct full DNS manually:
You can also print both in one command:
Confirm Resolution from a Pod
The best validation is DNS lookup from a running pod in the cluster.
Inside the shell:
If nslookup fails, investigate DNS and namespace assumptions before checking application code.
Reading Full Name Programmatically
In automation scripts, build the FQDN from API fields instead of hardcoding.
Python example:
This keeps scripts correct even when service names or namespaces are environment-specific.
Cluster Domain May Differ
Although many clusters use cluster.local, some environments customize this domain. If you assume the default blindly, DNS calls can fail.
You can inspect CoreDNS configuration:
Look for plugin settings showing the active cluster domain. If domain differs, append that domain in your constructed FQDN.
Short Names Versus Full Names
Use short names when scope is obvious:
- Same namespace:
my-api - Cross namespace:
my-api.payments
Use full name when:
- Debugging ambiguous DNS behavior.
- Communicating across multiple namespaces.
- Writing scripts where explicitness matters more than brevity.
Being explicit avoids accidental namespace coupling.
Discovering Service Context from Inside a Pod
Applications can read their own namespace at runtime and compose service targets dynamically. This is useful in multi-tenant deployments where namespace is not hardcoded.
With this value, app code can construct service-name.$POD_NAMESPACE.svc.cluster.local when same-namespace service routing is required.
It also helps when the same container image is promoted through multiple namespaces without rebuilds.
Debugging Checklist
When full name resolution fails:
- Verify service exists in expected namespace.
- Verify endpoint objects have backing pod IPs.
- Verify pod DNS policy is standard cluster DNS.
- Verify cluster domain assumption.
- Test from in-cluster shell.
Endpoint check command:
No endpoints usually indicates selector or pod readiness issues, not DNS naming problems.
Common Pitfalls
- Assuming service short name works across namespaces. Fix by using
service.namespaceor full FQDN. - Hardcoding
defaultnamespace assumptions. Fix by reading metadata namespace explicitly. - Forgetting custom cluster domain setups. Fix by confirming DNS domain in CoreDNS config.
- Debugging app code before checking endpoints. Fix by validating service and endpoint state first.
- Building names manually with typos in scripts. Fix by generating FQDN from API metadata fields.
Summary
- Full service DNS is usually
service.namespace.svc.cluster.local. - Fetch name and namespace from Kubernetes API to avoid mistakes.
- Validate DNS resolution from within a cluster pod.
- Confirm cluster domain if default assumptions fail.
- Combine service DNS checks with endpoint checks for reliable troubleshooting.

