Service Types

Difficulty: Intermediate

Question

What are the different types of Services in Kubernetes?

Answer

Services provide a stable IP/DNS name to reach a group of Pods.

1. **ClusterIP (Default)**: Exposes the service on an internal IP in the cluster. It's only reachable from within the cluster. 2. **NodePort**: Exposes the service on each Node's IP at a static port (30000-32767). Reachable from outside the cluster at `<NodeIP>:<NodePort>`. 3. **LoadBalancer**: Exposes the service externally using a cloud provider's load balancer (AWS ELB, GCP LB). It automatically creates a NodePort and ClusterIP. 4. **ExternalName**: Maps a service to a DNS name (e.g., `my-db.example.com`). It doesn't use selectors.

Code examples

Service YAML

apiVersion: v1
kind: Service
metadata:
  name: web-service
spec:
  type: LoadBalancer
  selector:
    app: web
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000

External traffic hits port 80 and is routed to pod port 3000.

Key points

Concepts covered

ClusterIP, NodePort, LoadBalancer, ExternalName