What is a Kubernetes Service and what are the different service types?
Learn what a Kubernetes Service is and how ClusterIP, NodePort, LoadBalancer, and ExternalName differ, with YAML examples and interview-ready answers.
Expected Interview Answer
A Kubernetes Service is a stable network abstraction that groups a set of Pods (selected by labels) behind a single, unchanging virtual IP and DNS name, load-balancing traffic across them even as Pods are created, killed, or rescheduled.
Because Pods are ephemeral and get new IPs on every restart, clients cannot rely on Pod IPs directly. A Service provides a durable endpoint; kube-proxy programs iptables/IPVS rules to forward traffic to healthy backing Pods tracked via an Endpoints/EndpointSlice object. The main types are ClusterIP (internal-only virtual IP, the default), NodePort (exposes the Service on a static port on every node), LoadBalancer (provisions an external cloud load balancer), and ExternalName (maps the Service to an external DNS name via a CNAME, with no proxying).
- Stable IP and DNS name that outlives individual Pods
- Automatic load balancing across healthy Pods
- Decouples clients from Pod lifecycle and IP churn
- Label-based selection so scaling adds/removes backends automatically
- Multiple exposure options from internal-only to external cloud LB
AI Mentor Explanation
A Service is like the fixed role of 'the bowler at the pavilion end' rather than a named player. Batters face whoever is bowling from that end, and the captain rotates fresh bowlers in as others tire, yet the striker always knows which end the ball comes from. The role stays constant while the individual bowlers behind it keep changing over after over.
Step-by-Step Explanation
Step 1
Label your Pods
Deployments stamp Pods with labels like app=web that a Service can target.
Step 2
Define a selector
The Service's spec.selector matches those labels to build the backend set.
Step 3
Kubernetes tracks endpoints
An EndpointSlice is maintained automatically listing the IPs of ready matching Pods.
Step 4
Pick a type
Choose ClusterIP, NodePort, LoadBalancer, or ExternalName based on who needs to reach it.
Step 5
kube-proxy programs routing
iptables or IPVS rules forward the Service IP/port to healthy Pod endpoints.
Step 6
Clients use the DNS name
Consumers resolve svc-name.namespace.svc.cluster.local instead of Pod IPs.
What Interviewer Expects
- Why Pod IPs are unreliable and a stable abstraction is needed
- Correct definition and role of a Service and its selector
- Accurate description of ClusterIP, NodePort, LoadBalancer, ExternalName
- Awareness of kube-proxy and EndpointSlices
- When to choose each service type
Common Mistakes
- Saying a Service runs the Pods rather than routing to them
- Confusing NodePort with LoadBalancer
- Forgetting ClusterIP is the default and internal-only
- Thinking ExternalName proxies traffic instead of returning a CNAME
- Believing the Service load-balances at L7 by default (it is L4)
Best Answer (HR Friendly)
“A Kubernetes Service gives an app a permanent address so other parts of the system can always reach it, even though the underlying app copies are constantly being replaced. There are a few types depending on whether the app needs to be reachable only inside the cluster or from the outside internet.”
Code Example
apiVersion: v1
kind: Service
metadata:
name: web
spec:
type: ClusterIP
selector:
app: web
ports:
- protocol: TCP
port: 80
targetPort: 8080apiVersion: v1
kind: Service
metadata:
name: web-lb
spec:
type: LoadBalancer
selector:
app: web
ports:
- port: 80
targetPort: 8080Follow-up Questions
- How does a headless Service differ from a normal ClusterIP Service?
- What is the difference between a Service and an Ingress?
- How does kube-proxy implement Service routing?
- What are EndpointSlices and why did they replace Endpoints?
- How does DNS-based service discovery work in Kubernetes?
MCQ Practice
1. What is the default Service type in Kubernetes?
ClusterIP is the default, exposing the Service only on an internal virtual IP reachable from within the cluster.
2. Which Service type provisions an external cloud load balancer?
LoadBalancer asks the cloud provider to create an external load balancer that forwards to the Service.
3. How does a Service decide which Pods to route to?
A Service uses spec.selector to match Pod labels; matching ready Pods become its endpoints.
Flash Cards
What problem does a Service solve? — Pods are ephemeral with changing IPs; a Service gives a stable IP/DNS name and load-balances across them.
Default Service type? — ClusterIP — internal-only virtual IP.
What does ExternalName do? — Maps the Service to an external DNS name via a CNAME; it does not proxy traffic.
What component programs Service routing? — kube-proxy, using iptables or IPVS rules based on EndpointSlices.