Tech Interview RoomStudy guides

Kubernetes

How does Kubernetes networking work: Services, Ingress, Gateway API, DNS and NetworkPolicies?

Advanced · 2 min read · Lesson 4 of 7 in Kubernetes

The model

Every pod gets its own IP and can reach every other pod without NAT; the CNI plugin (Calico, Cilium, AWS VPC CNI, GKE's dataplane) implements this.

Services

Pods are ephemeral, so a Service provides a stable virtual IP and DNS name in front of a label-selected set of ready pods (tracked in EndpointSlices).

  • ClusterIP (default): reachable only inside the cluster.
  • NodePort: also opens a port (30000–32767) on every node.
  • LoadBalancer: provisions a cloud load balancer (layer 4) pointing at the nodes or directly at pods.
  • Headless (clusterIP: None): no virtual IP; DNS returns the pod IPs directly, used for StatefulSets and client-side load balancing.
  • ExternalName: a DNS CNAME to an external host. kube-proxy programs iptables or IPVS rules (or Cilium uses eBPF) so traffic to the Service IP is load-balanced to a pod IP.

DNS

CoreDNS resolves service.namespace.svc.cluster.local (and short names within a namespace).

Ingress and Gateway API (layer 7, north-south)

  • Ingress: host and path routing plus TLS termination to Services, implemented by a controller (NGINX Ingress, cloud load balancer controllers).
  • Gateway API: the successor; role-oriented resources (GatewayClass, Gateway, HTTPRoute, GRPCRoute) with traffic splitting, header matching and cross-namespace routing built into the standard rather than vendor annotations.

NetworkPolicies

By default all pods can talk to all pods. NetworkPolicies (enforced by the CNI) whitelist ingress and egress by pod labels, namespaces and ports. Start with default-deny per namespace and open only required paths.

Internet
Users
↓HTTPS
Edge
Cloud load balancerIngress or Gateway controller
↓host and path routes
Services
⚙️ Service orders (ClusterIP)⚙️ Service payments (ClusterIP)
↓kube-proxy or eBPF load balancing
Pods
orders pod 1orders pod 2payments pod 1

Example

yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: payments-allow-orders, namespace: shop }
spec:
  podSelector: { matchLabels: { app: payments } }
  policyTypes: [Ingress]
  ingress:
    - from: [{ podSelector: { matchLabels: { app: orders } } }]
      ports: [{ protocol: TCP, port: 8080 }]

Follow-up questions interviewers ask

Try answering each one before you open it.

Why do long-lived gRPC connections cause uneven load with a ClusterIP Service?

kube-proxy balances connections, not requests. gRPC multiplexes all requests over one long-lived HTTP/2 connection, so each client sticks to one pod and new pods receive no traffic. Fixes: a layer-7 proxy or service mesh (Envoy, Linkerd) that balances per request, client-side load balancing over a headless Service, or periodically recycling connections (max-connection-age on the server).