Services -- Networking for Your Pods
Pods are ephemeral. They get created, destroyed, and replaced constantly. Each Pod gets a new IP address when it starts, so you cannot rely on Pod IPs to communicate between components. Services solve this problem by providing a stable network endpoint that routes traffic to the right Pods.
What Does a Service Do?
A Service creates a stable DNS name and IP address that stays the same even as Pods come and go behind it. It uses label selectors to decide which Pods receive traffic.
# List all services
kubectl get services
# Short form
kubectl get svc
Service Types
Kubernetes offers four service types, each serving a different purpose:
ClusterIP (default) -- accessible only from within the cluster. Use this for internal communication between services.
kubectl expose deployment my-app --port=80 --target-port=8080 --type=ClusterIP
NodePort -- exposes the service on a static port on every node. Use this for development and testing.
kubectl expose deployment my-app --port=80 --target-port=8080 --type=NodePort
LoadBalancer -- provisions an external load balancer from your cloud provider. Use this for production traffic in cloud environments.
kubectl expose deployment my-app --port=80 --target-port=8080 --type=LoadBalancer
ExternalName -- maps a service to an external DNS name. Use this to reference external services from within the cluster.
Inspecting a Service
Get detailed information about a service:
# See service details including endpoints
kubectl describe service my-service
# Check which pods back the service
kubectl get endpoints my-service
The Endpoints list shows the actual Pod IPs that the service routes to. If the endpoints list is empty, the label selector is not matching any running Pods.
Service DNS
Every Service gets a DNS entry in the format <service-name>.<namespace>.svc.cluster.local. Pods can reach a service by its short name if they are in the same namespace:
# From within a pod in the same namespace
curl http://my-service:80
# From a pod in a different namespace
curl http://my-service.production.svc.cluster.local:80
Key Takeaways
- Services provide stable networking for ephemeral Pods
- ClusterIP is for internal traffic, NodePort for testing, LoadBalancer for production
- Services use label selectors to find their target Pods
- Every Service gets a DNS name for easy discovery
- Use
kubectl get endpointsto verify which Pods a Service is routing to