Referencing a Service in Kubernetes Using the Local Address
Jeferson Nicolau Cassiano
Fullstack Software Engineer | .Net | C# | Back-end | Azure | GCP | React | Angular
When deploying applications in Kubernetes, ensuring smooth communication between services is essential. One common challenge developers face is understanding how to reference a service within the cluster using its local address.
Understanding Services in Kubernetes
In Kubernetes, a Service is used to expose a set of Pods reliably. Since Pods are dynamic and may restart on different nodes, a Service provides a stable endpoint for communication.
Common types of Services include:
How to Reference a Service Locally
Pods inside the cluster can communicate with a Service using Kubernetes' built-in DNS system. The standard format for referencing a Service is:
<SERVICE_NAME>.<NAMESPACE>.svc.cluster.local
For example, if a Service named my-service exists in the default namespace, it can be accessed within the cluster using:
my-service.default.svc.cluster.local
This allows Pods to resolve the address automatically without requiring manual configuration.
Checking a Service’s Address
To find details about a Service, use:
kubectl get svc my-service -n default
This will return an output similar to:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-service ClusterIP 10.96.1.10 <none> 80/TCP 10d
The Cluster-IP can be used by other Pods in the cluster to reach the Service.
To test connectivity from a running Pod:
kubectl exec -it my-pod -- curl my-service.default.svc.cluster.local:80
Accessing a Service from Outside the Cluster
If you need to access the Service from outside the cluster (e.g., from your local machine), you can expose it using NodePort or forward traffic with kubectl port-forward:
kubectl port-forward svc/my-service 8080:80
Now, the Service will be accessible at https://localhost:8080.
Final Thoughts
Kubernetes makes it easy to expose and connect services using internal DNS. When working inside the cluster, referencing a service via <SERVICE_NAME>.<NAMESPACE>.svc.cluster.local ensures reliable communication. If external access is needed, options like NodePort or port-forward provide simple solutions.
Have you faced challenges working with Kubernetes services? Share your experience in the comments!
Software Engineer | Java | Kotlin | Spring | Microservices | AWS | Azure
1 周Nice tips !
Senior Front-end Developer | React - NextJS - Typescript - NodeJS - AWS
1 周Great Content! Thanks!