Prompt
How do I handle service discovery between Kubernetes microservices?
Latest observation
In Kubernetes, service discovery is usually handled for you by Kubernetes Services plus DNS. For most microservices, the simplest pattern is:
1) Put each microservice behind a Kubernetes Service
Create a Service object for each app you want to reach.
Example:
apiVersion: v1
kind: Service
metadata:
name: orders
spec:
selector:
app: orders
ports:
- port: 80
targetPort: 8080
Then other pods can reach it by DNS name:
ordersorders.defaultorders.default.svc.cluster.local(full FQDN)
If your client and service are in different namespaces, use:
orders.my-namespace.svc.cluster.local
2) Use Kubernetes DNS, not hardcoded IPs
Pods get DNS resolution through CoreDNS automatically. This means you should reference services by name, not pod IPs.
Good:
http://orders:80http://orders.my-namespace.svc.cluster.local
Avoid:
- storing pod IPs
- hardcoding node addresses
- calling individual pods directly unless you specifically need that
3) Choose the right Service type
For internal microservice-to-microservice communication, use:
- ClusterIP: internal-only, most common
- Headless Service (
clusterIP: None): if you need direct pod DNS records, stateful workloads, or custom client-side load balancing
Example headless service:
apiVersion: v1
kind: Service
metadata:
name: kafka
spec:
clusterIP: None
selector:
app: kafka
ports:
- port: 9092
4) Consider readiness probes
Kubernetes will only send traffic to pods that are Ready. This is important for service discovery because it prevents routing to unhealthy instances.
Add:
readinessProbelivenessProbe
5) Use labels carefully
Services select pods via labels. Make sure your labels are stable and specific enough.
Example:
selector:
app: orders
If labels are wrong, service discovery breaks because the Service won’t find any endpoints.
6) For external access, use Ingress or Gateway API
If another service is outside the cluster or you need HTTP routing from outside Kubernetes:
- use Ingress
- or better, Gateway API if available in your environment
But for internal microservices, stick with Services + DNS.
7) Handle retries and timeouts in clients
Service discovery solves “how do I find the service,” but not reliability. Your client should also:
- set timeouts
- retry safely
- use exponential backoff
- implement circuit breakers if needed
8) If you need advanced discovery, use a service mesh
For traffic management, observability, mTLS, and more advanced discovery patterns, consider:
- Istio
- Linkerd
- Consul service mesh
These are useful when you need more than native Kubernetes DNS/service discovery.
Practical recommendation
For most Kubernetes setups:
- Create a
ClusterIP Servicefor each microservice - Call it by DNS name
- Add readiness probes
- Avoid hardcoded IPs
- Use a service mesh only if you need advanced features
If you want, I can show:
- a full example with two microservices
- cross-namespace discovery
- or headless service vs ClusterIP differences.