Kubernetes Services, Ingress, and Reverse Proxy
Why Services exist: decoupling ephemeral Pods
Because Pods are ephemeral, their IP addresses change whenever they crash, scale, or reschedule onto another node. A Service (svc) acts as a stable reception desk sitting in front of a dynamic set of Pods:
- Selects ready Pods using a
labelSelector(the same label mechanism used byReplicaSet) and load-balances requests across them (e.g., 1/3 each across 3 pods). - Exposes a stable virtual IP (separate from the Pod IP range) and a Cluster DNS name:
serviceNamewithin the same namespace, orservicename.namespace.svc.cluster.localacross namespaces. - Can remap ports (for example, exposing standard
port: 80while forwarding to non-privilegedtargetPort: 3000or8080inside containers).
The five Service types
The five Service types split cleanly into three “nesting-doll” virtual-IP layers (progressing from internal to public) plus two DNS shortcuts that skip proxying:
| Service type | Creates ClusterIP / proxying? | Reachable from Internet? | Mechanism & use case |
|---|---|---|---|
| ClusterIP (default) | Yes (cluster-internal VIP) | No | Internal virtual IP (e.g., 172.16.0.0/16) reachable only inside the cluster; used for backend microservices and caches |
| NodePort | Yes (ClusterIP + node ports) | Yes (<NodeIP>:<NodePort>) | Opens a static port (e.g., 30000) on every Worker Node’s IP and routes to the underlying ClusterIP; poor UX for end users, useful for debugging or beneath LoadBalancer |
| LoadBalancer | Yes (ClusterIP + NodePort + external LB) | Yes (<PublicIP>:<Port>) | Requests an external cloud load balancer (or on-prem component such as MetalLB); consumes one public IP per Service |
| ExternalName | No (DNS CNAME only) | N/A (points outward) | Returns a DNS CNAME pointing to an external hostname (e.g., foo.bar.example.com); no ClusterIP and no proxying |
Headless (clusterIP: None) | No (kube-proxy skips it) | No | Allocates no virtual IP and performs no load balancing; DNS returns the direct Pod IPs so clients (e.g., StatefulSet primary vs. replica) pick the exact Pod |
Clarifying ExternalName traffic direction
A frequent exam trap is misreading ExternalName as exposing Pods to the Internet. It works strictly in the opposite direction—inside cluster → outside cluster:
Pod inside K8s ──(1. DNS lookup: my-db)──► Cluster DNS ──(2. CNAME: prod-db.example.com)
│
└──────────(3. direct outbound TCP connection)──────────► prod-db.example.com (external)External clients cannot call your Pods through ExternalName; it merely gives internal Pods a stable cluster-local alias for an external database or legacy API so you can later swap the backend without changing app code.
Ingress: multiplexing HTTP/HTTPS behind one IP
Each LoadBalancer Service consumes its own public IP, and NodePort forces awkward port numbers (:30000). Ingress (ing) solves both by acting as a Layer 7 HTTP/HTTPS reverse proxy at the cluster entrance:
- Multiplexes many backends behind one IP: Maps hostnames and URL paths to internal
ClusterIPServices (e.g.,www.mysite.com→10.1.1.1,www.othersite.com/weather→10.1.1.3). - Two-part design: The
IngressYAML defines routing rules; a third-party Ingress Controller (such as NGINX or Traefik) implements the actual reverse proxy. - Address translation (D-NAT): Traffic sent from Ingress to a
ClusterIPundergoes Destination NAT bykube-proxyinto the target Pod IP (and an outer D-NAT may occur at the datacenter edge if the Ingress Controller itself sits behind a private LB address).
Forward TCP proxy vs. reverse proxy
| Property | Forward TCP proxy | TCP reverse proxy (Ingress role) |
|---|---|---|
| Placement | Sits in front of internal clients (N clients → many servers) | Sits in front of datacenter servers (many clients → N servers) |
| Main purpose | Regulate outbound access, mask client IPs, enforce client security policies | Protect backend servers, terminate TLS, expose multiple internal services on one public IP |
| Connection model | Two separate TCP connections (client ↔ proxy, proxy ↔ Internet server) | Two separate TCP connections (Internet client ↔ reverse proxy, reverse proxy ↔ backend server) |
Retrieval
- You deploy 10 web microservices (
app1.example.com–app10.example.com) and want external HTTPS access on port 443 without allocating 10 public IPs or exposing:30000ports. How should you wire Services and Ingress?
Expose each microservice internally with aClusterIPService, then deploy an Ingress Controller (e.g., NGINX or Traefik) behind one public entry point with Ingress rules mapping each hostname to its correspondingClusterIPService. - Why does a database
StatefulSetwith one primary and two read replicas often pair with a Headless Service (clusterIP: None) instead of a standardClusterIPService?
A standardClusterIPinstructskube-proxyto load-balance across all matching Pods unpredictably, which would misroute writes to read-only replicas. SettingclusterIP: Nonebypasseskube-proxyso DNS returns direct Pod IPs, letting clients target the exact primary or replica Pod.
Source: Lecture 11 - Kubernetes, PDF pages 28–37 (printed slides 32–43).