Services & Ingress
Give workloads stable networking and route external HTTP traffic into the cluster.
Pods are ephemeral with changing IPs, so you never target them directly. A Service gives a stable virtual IP and DNS name in front of a set of Pods selected by labels, load-balancing across them.
Service types:
- ClusterIP (default) — reachable only inside the cluster
- NodePort — opens a static port on every node
- LoadBalancer — provisions an external cloud load balancer
For HTTP, an Ingress (with an ingress controller like nginx) routes by host and path to Services — so one external IP serves many apps with TLS termination.
apiVersion: v1
kind: Service
metadata: { name: web }
spec:
selector: { app: web }
ports: [{ port: 80, targetPort: 8080 }]
type: ClusterIP
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata: { name: web }
spec:
rules:
- host: app.example.com
http:
paths:
- path: /
pathType: Prefix
backend: { service: { name: web, port: { number: 80 } } } - Create a ClusterIP Service selecting Pods labeled app=web.
- Explain the difference between ClusterIP, NodePort, and LoadBalancer.
- Add an Ingress routing a hostname to that Service.
- Why can’t you just connect to a Pod’s IP directly?
Cheat Sheet▾
| Type / object | Use |
|---|---|
| ClusterIP | Internal-only stable IP |
| NodePort | Static port on each node |
| LoadBalancer | External cloud LB |
| Ingress | HTTP host/path routing + TLS |
| Selector | Matches Pod labels |
kubectl expose | Quick Service creation |
Common Interview Questions▾
Why do you need a Service in front of Pods?
Pods are ephemeral with changing IPs. A Service provides a stable virtual IP and DNS name plus load balancing, selecting Pods by label so clients have a constant endpoint.
What's the difference between a Service and an Ingress?
A Service provides L4 connectivity and load balancing to Pods. An Ingress is L7 HTTP routing (by host/path) that directs external traffic to Services, often with TLS.
What are the main Service types?
ClusterIP (internal), NodePort (a static port on each node), and LoadBalancer (an external cloud-provisioned load balancer).