Kubernetes networking
How a request finds a pod: pod IPs, Services, DNS, Ingress and NetworkPolicy
1. Pod-to-pod networking
- Pod IP — Every pod gets its own IP address, shared by all the containers in it.
- No NAT — Pods reach each other directly by pod IP, on the same node or another.
- localhost — Containers in one pod share the network, so they talk over localhost.
- CNI plugin — Calico, Cilium, Flannel and others make the pod network work.
Every pod has its own IP, but pods come and go: never hard-code one.
2. Service types
| Type | What it gives you |
|---|---|
| ClusterIP | The default: a stable virtual IP reachable only inside the cluster. |
| NodePort | The Service on a port (30000–32767 by default) of every node. |
| LoadBalancer | A cloud load balancer in front, with an external IP. |
| ExternalName | A DNS alias (CNAME) to a name outside the cluster; no proxying. |
| Headless | clusterIP: None. DNS returns the pods' own IPs instead of one virtual IP. |
A Service picks its pods by label selector and spreads traffic across the ready ones.
3. Cluster DNS
- CoreDNS — The cluster's DNS server; every pod is set up to ask it first.
my-svc.my-namespace.svc.cluster.local— A Service's full name.- Short names — Inside the same namespace, my-svc on its own is enough.
- Headless Services — Return one record per ready pod, so clients can pick a pod.
Call Services by name, never by IP: names survive a redeploy.
4. Ingress
- Rules — Route HTTP and HTTPS by host name and path to Services.
- Controller — An Ingress does nothing until an ingress controller runs in the cluster.
- TLS — Certificates come from a Secret named in the Ingress.
- ingressClassName — Says which controller should handle this Ingress.
- Gateway API — The newer, more expressive way to route traffic into a cluster.
Ingress is for HTTP(S); for other protocols use a LoadBalancer Service.
5. NetworkPolicy
- Open by default — With no policy, every pod accepts traffic from anywhere.
- Selected pods — Once a policy selects a pod, only the traffic it allows gets in (or out).
- Selectors — Allow by podSelector, namespaceSelector, ipBlock and port.
- Enforcement — Your CNI plugin must support NetworkPolicy, or policies are ignored.
Start with a default-deny policy per namespace, then allow what each app needs.
6. Debugging commands
| Command | What it shows |
|---|---|
kubectl get svc -A | Every Service, its type, cluster IP and ports. |
kubectl get endpointslices -l kubernetes.io/service-name=my-svc | The pod IPs behind a Service. |
kubectl describe ingress my-ingress | Its rules, backends and recent events. |
kubectl port-forward svc/my-svc 8080:80 | Reach a Service from your machine on localhost:8080. |
kubectl get networkpolicy -A | Every NetworkPolicy, by namespace. |
No endpoints means the selector matches no ready pod: check labels and readiness.