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 by ReplicaSet) 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: serviceName within the same namespace, or servicename.namespace.svc.cluster.local across namespaces.
  • Can remap ports (for example, exposing standard port: 80 while forwarding to non-privileged targetPort: 3000 or 8080 inside 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 typeCreates ClusterIP / proxying?Reachable from Internet?Mechanism & use case
ClusterIP (default)Yes (cluster-internal VIP)NoInternal virtual IP (e.g., 172.16.0.0/16) reachable only inside the cluster; used for backend microservices and caches
NodePortYes (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
LoadBalancerYes (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
ExternalNameNo (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)NoAllocates 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 ClusterIP Services (e.g., www.mysite.com → 10.1.1.1, www.othersite.com/weather → 10.1.1.3).
  • Two-part design: The Ingress YAML 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 ClusterIP undergoes Destination NAT by kube-proxy into 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

PropertyForward TCP proxyTCP reverse proxy (Ingress role)
PlacementSits in front of internal clients (N clients → many servers)Sits in front of datacenter servers (many clients → N servers)
Main purposeRegulate outbound access, mask client IPs, enforce client security policiesProtect backend servers, terminate TLS, expose multiple internal services on one public IP
Connection modelTwo separate TCP connections (client ↔ proxy, proxy ↔ Internet server)Two separate TCP connections (Internet client ↔ reverse proxy, reverse proxy ↔ backend server)

Retrieval

  1. 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 :30000 ports. How should you wire Services and Ingress?
    Expose each microservice internally with a ClusterIP Service, then deploy an Ingress Controller (e.g., NGINX or Traefik) behind one public entry point with Ingress rules mapping each hostname to its corresponding ClusterIP Service.
  2. Why does a database StatefulSet with one primary and two read replicas often pair with a Headless Service (clusterIP: None) instead of a standard ClusterIP Service?
    A standard ClusterIP instructs kube-proxy to load-balance across all matching Pods unpredictably, which would misroute writes to read-only replicas. Setting clusterIP: None bypasses kube-proxy so 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).