What are the Kubernetes Service types and when do you use each?
Quick Answer
ClusterIP (default) exposes a stable internal VIP for in-cluster access; NodePort opens a port on every node; LoadBalancer provisions an external cloud LB; ExternalName maps a Service to an external DNS name. Ingress/Gateway usually front HTTP instead of many LoadBalancers.
Detailed Answer
ClusterIP gives a Service a virtual IP reachable only inside the cluster — the default and what you use for service-to-service traffic. NodePort additionally opens the same high port (30000–32767) on every node, so external traffic hitting any node:port is forwarded to the Service; it's simple but ugly for production (odd ports, no TLS termination). LoadBalancer builds on NodePort and asks the cloud provider to provision an external load balancer with a real IP — one per Service, which gets expensive. ExternalName is different: it's just a CNAME to an external hostname with no proxying, handy for pointing in-cluster names at an external database. For real HTTP exposure you typically run one LoadBalancer for an Ingress controller (or Gateway API) and route many hostnames/paths through it, terminating TLS centrally.
Code Example
apiVersion: v1
kind: Service
metadata: { name: web }
spec:
type: ClusterIP # internal-only VIP
selector: { app: web }
ports: [{ port: 80, targetPort: 8080 }]
# Prefer 1 LoadBalancer -> Ingress -> many Services over N LoadBalancersInterview Tip
Show cost awareness: 'one LoadBalancer per Service gets expensive, so we front HTTP with an Ingress/Gateway.' That's the practical insight beyond just listing the four types.