Documentation Index
Fetch the complete documentation index at: https://resources.devweekends.com/llms.txt
Use this file to discover all available pages before exploring further.
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. Imagine trying to call a friend who changes phone numbers every few hours — you would need a directory service that always knows the current number. That is exactly what a Kubernetes Service does: it provides a stable “phone number” (IP address and DNS name) for a set of Pods that may be constantly coming and going behind the scenes.Stable IP
Load Balancing
Service Discovery
my-service.default.svc.cluster.local)Decoupling
Service Types
1. ClusterIP (Default)
Exposes the Service on an internal IP in the cluster. This IP is virtual — it only exists in iptables/IPVS rules on each node, not as a network interface. Think of it as an internal phone extension that only works within the office building.- 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). Every node in the cluster opens this port, even nodes that are not running the target pods. Traffic that arrives on any node is forwarded to a pod matching the selector, regardless of which node the pod is on.- 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. If you have 20 microservices, that means 20 LoadBalancers, 20 public IPs, and 20 cloud provider bills. Ingress solves this by exposing multiple services under a single IP address, using routing rules (path-based or host-based). Think of it as the receptionist at the front desk who directs visitors to the right office based on who they ask for. Requires an Ingress Controller (e.g., Nginx, Traefik) to be running in the cluster. The Ingress resource itself is just a routing configuration — without a controller to implement it, nothing happens.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. A regular Service is like calling a company switchboard — you get connected to whoever is available. A headless Service is like having the direct phone number for every employee — you choose who to call.
Use Cases:
- StatefulSets (clients need to connect to specific pods, e.g., “write to the primary, read from the replica”)
- Service discovery without load balancing (your application handles routing)
- Peer-to-peer communication (e.g., Cassandra or Kafka nodes that need to discover each other)
mysql-headless.default.svc.cluster.local— Returns all pod IPs (A records)mysql-0.mysql-headless.default.svc.cluster.local— Pod-specific (only works with StatefulSets)
Network Policies (Critical for Security!)
By default, all pods can communicate with all other pods — zero network isolation. This is like having every room in an office building connected to every other room with no doors or locks. In production, you almost certainly want to restrict which pods can talk to which. NetworkPolicy is how you add those doors and locks.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?
- 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?
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?
- 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?
- 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?
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.
Interview Deep-Dive
You have a microservices architecture where service A calls service B. Latency between them suddenly spikes from 5ms to 500ms. Using your knowledge of Kubernetes networking internals, how would you diagnose this?
You have a microservices architecture where service A calls service B. Latency between them suddenly spikes from 5ms to 500ms. Using your knowledge of Kubernetes networking internals, how would you diagnose this?
- First, I would determine whether the issue is at the pod level or the Service/networking level. I would exec into a pod of service A and curl service B’s ClusterIP directly, timing the response:
time curl http://service-b.default.svc.cluster.local:8080/health. If this is fast, the problem is upstream (load balancer, ingress). If this is slow, the issue is in the cluster network. - Next, I would curl service B using a pod IP directly (bypassing the Service abstraction):
time curl http://10.244.1.15:8080/health. If the pod IP is fast but the ClusterIP is slow, the issue is in kube-proxy’s iptables/IPVS rules or DNS resolution. - For DNS:
time nslookup service-b.default.svc.cluster.local. CoreDNS can be a bottleneck — I have seen cases where CoreDNS pods are overloaded, causing 5-second DNS lookup delays (the default timeout before the resolver retries). - For iptables: on the node, I would check the number of iptables rules. In large clusters, the iptables chain can grow to thousands of rules, and traversal becomes O(n). Switching to IPVS mode (O(1) lookup) is the fix.
- For the pod itself: check if service B’s pods are CPU-throttled or overwhelmed.
kubectl top pods -l app=service-bshows current usage. If pods are approaching limits, responses slow down.
Explain Headless Services. When exactly would you use one, and what happens at the DNS level compared to a regular ClusterIP Service?
Explain Headless Services. When exactly would you use one, and what happens at the DNS level compared to a regular ClusterIP Service?
- A regular ClusterIP Service gives you a single virtual IP that load-balances across all matching pods. DNS returns the ClusterIP, and kube-proxy handles the load balancing.
- A Headless Service (clusterIP: None) has no virtual IP. Instead, DNS returns the IP addresses of all individual pods as multiple A records. The client gets a list and decides which pod to connect to.
- Headless Services are essential for StatefulSets because each pod needs a stable, addressable identity. With a Headless Service named
mysqland a StatefulSet, you get DNS records likemysql-0.mysql.default.svc.cluster.local. Clients can connect to specific replicas — the primary for writes, a replica for reads. - Other use cases: Kafka brokers (clients need to discover all brokers), Elasticsearch nodes (cluster formation protocol needs specific node addresses), and gRPC services (which need all endpoints for client-side load balancing since gRPC multiplexes over a single HTTP/2 connection).
networkaddress.cache.ttl=0. Clients using Headless Services need short DNS TTLs and connection retry logic.Your company wants to expose a Kubernetes application to the internet with TLS. Compare LoadBalancer Service versus Ingress. What are the cost, operational, and security trade-offs?
Your company wants to expose a Kubernetes application to the internet with TLS. Compare LoadBalancer Service versus Ingress. What are the cost, operational, and security trade-offs?
- LoadBalancer Service: Each Service gets its own cloud load balancer. Simple to set up, but each one costs money. If you have 20 Services, you pay for 20 load balancers. At AWS, that is roughly $16/month per NLB plus data processing charges.
- Ingress controller: One load balancer fronts one Ingress controller (nginx, Traefik). All HTTP/HTTPS traffic routes through this single entry point using host-based and path-based rules. TLS is terminated at the Ingress controller using cert-manager with Let’s Encrypt for automatic certificate provisioning.
- Cost: Ingress is dramatically cheaper at scale. One load balancer instead of 20. The trade-off is the Ingress controller becomes a shared bottleneck — size it appropriately and run multiple replicas.
- Operational: Ingress adds complexity. Configuration via annotations varies by controller. LoadBalancer Services are simpler but less flexible.
- Security: Ingress controllers can enforce centralized policies (WAF rules, rate limiting, IP whitelisting) at the edge.
Next: Kubernetes Configuration →