Skip to content

Kubernetes Services

Pods have ephemeral IP addresses. A Service provides a stable network endpoint in front of a set of pods, load balancing traffic across them.

Services use label selectors to find pods. Any pod with matching labels receives traffic:

selector:
app: my-app # routes to pods with this label

Kubernetes maintains an Endpoints object that tracks the IPs of matching pods automatically as pods are created and destroyed.

Accessible only within the cluster. Used for internal service-to-service communication.

apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
type: ClusterIP
selector:
app: my-app
ports:
- port: 80 # port the service listens on
targetPort: 8080 # port on the pod
Terminal window
# Access from inside the cluster using DNS
curl http://my-app-service.default.svc.cluster.local
# Or within the same namespace:
curl http://my-app-service

Exposes the service on a static port on every node. Accessible from outside the cluster at <NodeIP>:<NodePort>.

spec:
type: NodePort
selector:
app: my-app
ports:
- port: 80
targetPort: 8080
nodePort: 30080 # must be 30000-32767

Useful for development and testing. Not recommended for production โ€” prefer LoadBalancer or Ingress.

Provisions a cloud load balancer (AWS ELB, GCP LB, Azure LB) that routes traffic to the NodePort:

spec:
type: LoadBalancer
selector:
app: my-app
ports:
- port: 80
targetPort: 8080

The cloud provider assigns an external IP automatically. Each service gets its own load balancer, which can be costly โ€” use Ingress to share one LB across multiple services.

Maps a service name to an external DNS name. No proxying โ€” just DNS aliasing.

spec:
type: ExternalName
externalName: my.external-database.com

Kubernetes provides automatic DNS for services:

<service-name>.<namespace>.svc.cluster.local

From the same namespace, just <service-name> works.

ports:
- name: http
port: 80
targetPort: 8080
- name: https
port: 443
targetPort: 8443
Terminal window
kubectl get services
kubectl describe service my-app-service
kubectl delete service my-app-service
# Check which pods the service routes to
kubectl get endpoints my-app-service
Service (LoadBalancer)Ingress
CostOne LB per serviceOne LB for all services
RoutingPort-basedPath and host-based HTTP routing
TLS terminationAt LB levelAt Ingress level
Use forNon-HTTP protocols, single serviceHTTP/HTTPS with multiple services