Kubernetes Service
A Kubernetes Service provides a stable DNS name and IP address for accessing a dynamic set of Pods, abstracting away Pod IP churn with label-based routing.
Pod IP addresses in Kubernetes are ephemeral — every time a Pod is replaced, it gets a new IP. A Service solves this by providing a stable virtual IP (ClusterIP) and DNS name that always routes to the current set of healthy Pods matching a label selector. When a Pod is replaced, the Endpoints object is updated automatically and traffic continues without any application-level change.
Service Types
| Type | Access Scope | How It Works |
|---|---|---|
ClusterIP (default) |
Inside the cluster only | Assigns a cluster-internal IP; DNS resolves <svc>.<ns>.svc.cluster.local to it |
NodePort |
External, via any node's IP | Opens a port (30000-32767) on every node and forwards to the ClusterIP |
LoadBalancer |
External, via cloud load balancer | Provisions a cloud LB (AWS ELB, GCP LB) and forwards to NodePort/ClusterIP |
ExternalName |
DNS alias only | Returns a CNAME record to an external DNS name; no proxying |
ClusterIP Service Manifest
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
namespace: default
spec:
selector:
app: nginx # routes traffic to Pods with this label
ports:
- name: http
protocol: TCP
port: 80 # port the Service listens on
targetPort: 80 # port on the Pod to forward to
type: ClusterIP
NodePort Service Manifest
apiVersion: v1
kind: Service
metadata:
name: nginx-nodeport
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
nodePort: 30080 # optional; if omitted, a port is auto-assigned
type: NodePort
LoadBalancer Service (cloud)
apiVersion: v1
kind: Service
metadata:
name: nginx-lb
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
type: LoadBalancer
After applying on a cloud cluster:
kubectl get service nginx-lb
# NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
# nginx-lb LoadBalancer 10.96.100.45 34.102.123.45 80:31234/TCP 2m
How Services Find Pods
Services do not directly route to Pods. The kube-apiserver maintains an Endpoints (or EndpointSlice) object for each Service. The endpoints-controller watches for Pods matching the Service's spec.selector and updates the Endpoints object with the IP:port of each ready Pod. kube-proxy on every node then programs iptables/IPVS rules using the Endpoints data.
# Inspect a Service's current endpoints
kubectl get endpoints nginx-svc
# NAME ENDPOINTS AGE
# nginx-svc 10.244.1.5:80,10.244.2.3:80,10.244.3.7:80 5m
# If ENDPOINTS shows <none>, the selector doesn't match any ready Pod
DNS Resolution Inside the Cluster
CoreDNS provides in-cluster DNS. A Pod can reach nginx-svc in the same namespace as:
nginx-svc(short name)nginx-svc.default(namespace-qualified)nginx-svc.default.svc.cluster.local(fully qualified)
# From inside a Pod
curl http://nginx-svc/
curl http://nginx-svc.default.svc.cluster.local/
Common Mistakes
- Selector/label mismatch. If
spec.selectordoes not match the labels on any running, ready Pod, theEndpointsobject will be empty and all connections will fail. Check withkubectl get endpoints <svc-name>. This is one of the most frequent networking issues in Kubernetes. - Using
NodePortin production without a load balancer in front. DirectNodePortaccess routes to one specific node, which becomes a single point of failure. Always frontNodePortServices with a load balancer or useLoadBalancertype. - Confusing
port,targetPort, andnodePort.portis what clients inside the cluster connect to;targetPortis the port the container listens on;nodePortis the port opened on cluster nodes for external access. - Forgetting that
LoadBalancerincurs cloud costs. EachLoadBalancerService typically provisions a separate cloud load balancer. For HTTP/HTTPS, use anIngresswith one load balancer routing to many Services. - Expecting a Service to perform health checking. A Service routes only to Pods that pass their
readinessProbe. Without a readiness probe, a Service will route to Pods that are not yet ready.
Best Practices
- Use
ClusterIPfor internal microservice communication; expose externally only what needs to be external. - Prefer
Ingressover multipleLoadBalancerServices for HTTP/HTTPS traffic to save on cloud costs. - Always set a
readinessProbeon Pods so unhealthy Pods are automatically removed from Service endpoints. - Use named ports (
name: http) in both the Service and the container spec; this lets you change port numbers without updating all references. - Label your Pods clearly and consistently; Service selectors depend entirely on labels.
Interview Questions
1. What is a Kubernetes Service and why is it needed?
A Service provides a stable virtual IP and DNS name that always routes to the current set of healthy Pods matching a label selector. Without a Service, clients would need to track individual Pod IPs, which change every time a Pod is replaced. Services abstract away Pod IP churn.
2. How does a Service find its backend Pods?
A Service uses a spec.selector (a map of key-value labels). The Endpoints controller watches for Pods in the same namespace that match the selector and have passed their readiness probe, then populates the Endpoints object with their IP:port pairs. kube-proxy reads the Endpoints object to program iptables/IPVS rules on every node.
3. What is the difference between ClusterIP, NodePort, and LoadBalancer?
ClusterIP is only reachable from inside the cluster. NodePort opens a port on every cluster node and forwards traffic to the ClusterIP, enabling external access via <node-ip>:<nodePort>. LoadBalancer extends NodePort by also provisioning a cloud load balancer that forwards to the NodePort, providing a production-ready external IP.
4. Why does a Service have no endpoints?
The most common cause is a selector/label mismatch: the spec.selector on the Service does not match the labels on any running, ready Pod. Verify with kubectl get endpoints <svc-name> and compare the selector to kubectl get pods --show-labels. Another cause is that the Pods exist but are failing their readiness probe.
5. What is an ExternalName Service?
An ExternalName Service maps a Kubernetes Service name to an external DNS name. It creates a CNAME record in CoreDNS. No proxying occurs — it is purely a DNS alias. It is useful for referencing an external database or API endpoint via a Kubernetes-internal DNS name, making it easy to change the external target without modifying application configuration.
6. How does DNS work for Services inside the cluster?
CoreDNS is the cluster DNS server. It creates an A record for each ClusterIP Service in the form <service-name>.<namespace>.svc.cluster.local. Pods are configured (via /etc/resolv.conf) to search <namespace>.svc.cluster.local and svc.cluster.local, so a Pod can reach a Service in the same namespace by its short name.
7. What is the difference between a Service and an Ingress?
A Service of type LoadBalancer operates at Layer 4 (TCP/UDP) and gives each Service its own cloud load balancer and external IP. An Ingress operates at Layer 7 (HTTP/HTTPS) and allows a single load balancer to route to many Services based on hostname and URL path rules. Ingress is more cost-efficient for HTTP traffic.
Summary
- A
Serviceprovides a stable ClusterIP and DNS name that abstracts the dynamic IP addresses of Pods. - Services find Pods via a
spec.selectorlabel match; checkkubectl get endpointswhen traffic is not flowing. - The four types (
ClusterIP,NodePort,LoadBalancer,ExternalName) cover internal, node-level, cloud, and DNS-alias access patterns. - For HTTP/HTTPS external access, prefer
Ingressover multipleLoadBalancerServices. - A readiness probe on Pods ensures the Service only routes to healthy instances.
Continue Learning
Previous: Deployment | Next: ConfigMap
Was this guide helpful?
CodeWithVenu is free to read, with no paywall. If this guide saved you time or helped you prepare, a small voluntary contribution helps me write more practical tutorials, architecture guides, and interview-prep content.