Kubernetes Services
Learn how to expose your applications to the network and enable communication between microservices.Why Services?
Pods are ephemeral. They are created and destroyed, and their IP addresses change. A Service provides a stable IP address and DNS name for a set of Pods.Stable IP
Service IP never changes
Load Balancing
Distributes traffic across matching Pods
Service Discovery
DNS names (e.g.,
my-service.default.svc.cluster.local)Decoupling
Frontend talks to Backend Service, not individual Pods
Service Types
1. ClusterIP (Default)
Exposes the Service on an internal IP in the cluster.- Only reachable from within the cluster.
- Use case: Internal microservice communication (e.g., API talking to DB).
2. NodePort
Exposes the Service on each Node’s IP at a static port (30000-32767).- Reachable from outside the cluster via
<NodeIP>:<NodePort>. - Use case: Development, or when you don’t have a Load Balancer.
3. LoadBalancer
Exposes the Service externally using a cloud provider’s Load Balancer (AWS ELB, Google Cloud LB).- Use case: Production public-facing services.
4. ExternalName
Maps the Service to a DNS name (e.g.,foo.bar.example.com).
- Use case: Accessing external services (like RDS) as if they were local services.
Ingress
A Service (NodePort/LoadBalancer) exposes a single service. Ingress exposes multiple services under a single IP address, using routing rules (path-based or host-based). Requires an Ingress Controller (e.g., Nginx, Traefik) to be running in the cluster.Ingress Resource Example
Service Discovery
Kubernetes has a built-in DNS server (CoreDNS). Services get a DNS record in the format:my-service.my-namespace.svc.cluster.local
Example
If you have a pod in thedefault namespace, it can access the database service in the prod namespace via:
database.prod
Headless Services
A Headless Service has no ClusterIP (clusterIP: None). Instead of load balancing, DNS returns the IPs of individual pods.
Use Cases:
- StatefulSets (clients need to connect to specific pods)
- Service discovery without load balancing
- Custom load balancing logic
mysql-headless.default.svc.cluster.local→ Returns all pod IPsmysql-0.mysql-headless.default.svc.cluster.local→ Pod-specific
Network Policies (Critical for Security!)
By default, all pods can communicate with all other pods. NetworkPolicy restricts traffic.Default Deny All Ingress
Allow Specific Traffic
Endpoints & EndpointSlices
Endpoints are automatically created when you create a Service. They track the IP addresses of pods matching the Service selector.External Services (No Selector)
You can create a Service without a selector and manually define endpoints to route to external services:Service Mesh Overview
For complex microservice architectures, a Service Mesh provides:- mTLS: Automatic encryption between services
- Traffic Management: Canary deployments, traffic splitting
- Observability: Distributed tracing, metrics
- Resilience: Retries, circuit breakers, timeouts
Interview Questions & Answers
What is the difference between ClusterIP, NodePort, and LoadBalancer?
What is the difference between ClusterIP, NodePort, and LoadBalancer?
| Type | Accessible From | IP Address |
|---|---|---|
| ClusterIP | Inside cluster only | Internal cluster IP |
| NodePort | External via <NodeIP>:<30000-32767> | Node IPs |
| LoadBalancer | External via cloud LB | Cloud provider assigns |
How does kube-proxy work?
How does kube-proxy work?
kube-proxy runs on every node and implements Services using:
- iptables mode (default): Creates iptables rules for routing
- IPVS mode: Uses Linux IPVS for better performance at scale
- userspace mode (legacy): Proxies in userspace (slow)
What is a Headless Service and when would you use it?
What is a Headless Service and when would you use it?
A Headless Service (
clusterIP: None) doesn’t load balance. Instead:- DNS returns individual pod IPs
- Used with StatefulSets where clients need specific pods
- Example: Kafka brokers, database replicas
How do you expose a service externally without a cloud load balancer?
How do you expose a service externally without a cloud load balancer?
Options:
- NodePort: Expose on node IPs (ports 30000-32767)
- Ingress with NodePort: Use Ingress controller on NodePort
- MetalLB: Bare-metal load balancer for on-prem clusters
- ExternalIPs: Assign external IPs to Services (requires routing setup)
What is an Ingress Controller?
What is an Ingress Controller?
An Ingress Controller is a pod that:
- Watches for Ingress resources via API server
- Configures a reverse proxy (nginx, HAProxy, Envoy)
- Implements routing rules defined in Ingress resources
How do you implement rate limiting in Kubernetes?
How do you implement rate limiting in Kubernetes?
At the Ingress level using annotations:Or with a Service Mesh (Istio):
Common Pitfalls
Key Takeaways
- Use ClusterIP for internal traffic.
- Use LoadBalancer (or Ingress) for public traffic.
- Ingress is a smart router for HTTP/HTTPS.
- Services provide stable networking for ephemeral pods.
- NetworkPolicies are essential for security in production.
- Headless Services are needed for StatefulSets.
Next: Kubernetes Configuration →