Pods are ephemeral. When a Pod dies, its IP address is gone. A Service provides a single, stable IP address and DNS name for a set of Pods, acting as a load balancer.
1. ClusterIP (Default)
This is the most common type. It makes the Service reachable only inside the cluster.
service.yaml:
apiVersion: v1
kind: Service
metadata:
name: my-web-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIPApply and Verify
Action:
kubectl apply -f service.yaml
kubectl get servicesResult:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-web-service ClusterIP 10.96.123.45 <none> 80/TCP 10s(Now, any other Pod in the cluster can reach the nginx pods at http://my-web-service)
2. NodePort
Exposes the Service on a specific port (30000-32767) on each Node's IP. Use this for simple external access or on-premise clusters.
Action (Manifest snippet):
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 300803. LoadBalancer
On cloud providers (AWS, GCP, Azure), this type creates a real Cloud Load Balancer with a public IP.
Check External IP
Action:
kubectl get svc my-lb-serviceResult:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-lb-service LoadBalancer 10.96.0.1 35.200.12.34 80:31234/TCP 2m(You can now visit http://35.200.12.34 in your browser!)
4. Headless Services
For stateful applications (like databases), you can set clusterIP: None. This returns all Pod IP addresses instead of one load-balanced IP.
Summary
- ClusterIP: Internal only (default).
- NodePort: External via NodeIP:Port.
- LoadBalancer: External via Cloud Provider's IP.
- Selectors: Link the Service to the correct Pods.