net/go.book
All Parts Marketing

Container and Kubernetes Network Security

Containers are like apartments sharing one building's plumbing and electrical system: each unit feels self-contained, but a serious enough problem in the shared infrastructure below can affect every tenant at once. Understanding exactly where that shared infrastructure boundary sits is the whole game in container network security.


What Container Isolation Actually Provides

Containers are not lightweight virtual machines - they are ordinary processes on a shared host kernel, isolated from each other using Linux namespaces (separate views of the network stack, process tree, and filesystem) and resource limits enforced by cgroups. This isolation is real and useful, but it is weaker than the hardware-enforced boundary a hypervisor provides between virtual machines: a sufficiently severe kernel vulnerability, or a container running with excessive privileges, can potentially affect the host or sibling containers in ways a properly configured VM boundary would prevent. Understanding this distinction shapes a lot of container security practice: run containers with the least privilege the workload actually needs, avoid running as root inside the container where avoidable, and treat the shared kernel as the actual trust boundary rather than the container abstraction.


Container Networking Basics

By default, container runtimes typically create a private bridge network where each container gets an internal IP address and can reach other containers on the same host, with explicit port mapping required to expose a service beyond the host. Multi-host container networking layers an overlay network on top, tunneling traffic between hosts so containers can reach each other transparently regardless of which physical machine they're scheduled on - this is the same fundamental problem the Container Network Interface (CNI) specification standardizes for Kubernetes, letting different networking plugins (each with different performance, encryption, and policy capabilities) implement the same pod-networking contract.


The Kubernetes Networking Model

Kubernetes imposes a specific and important networking rule: every pod gets its own IP address, and by default, every pod can reach every other pod across the entire cluster without network address translation - a flat network model that dramatically simplifies application development but means, absent additional controls, a compromised pod in one namespace can freely reach services in a completely unrelated namespace.

Services provide a stable virtual IP and DNS name in front of a shifting set of pod replicas, so other workloads don't need to track individual pod IPs as pods are created and destroyed. Ingress resources expose HTTP/HTTPS routes from outside the cluster to internal services, typically through a controller that also handles TLS termination, similar in spirit to the reverse proxy patterns covered in Part 2 of this book.


NetworkPolicies: Kubernetes' Segmentation Tool

Because the default pod network is flat and permissive, NetworkPolicy resources are how Kubernetes implements the microsegmentation principles introduced earlier in this part, at the pod level. A NetworkPolicy selects a set of pods (by label) and defines exactly which other pods, namespaces, or IP ranges may communicate with them, and on which ports.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-frontend-to-api
  namespace: production
spec:
  podSelector:
    matchLabels:
      app: api
  policyTypes:
    - Ingress
  ingress:
    - from:
        - podSelector:
            matchLabels:
              app: frontend
      ports:
        - protocol: TCP
          port: 8080

This policy allows only pods labeled app: frontend to reach the api pods on port 8080, and implicitly denies every other inbound connection to those pods once any NetworkPolicy selects them - a default-deny model that mirrors the "explicit-allow" posture recommended for cloud resources in the previous chapter.

No NetworkPolicy Means No Restriction
A cluster with zero NetworkPolicy resources defined has no pod-to-pod network segmentation at all, regardless of namespace boundaries. Namespaces organize resources logically; they do not restrict network traffic by themselves. Treating namespace separation as network isolation is one of the more common and dangerous Kubernetes security misconceptions.


Service Mesh and mTLS

A service mesh (Istio, Linkerd, and similar projects) injects a lightweight proxy sidecar alongside each application container, transparently intercepting all network traffic to and from it. This makes it possible to enforce mutual TLS between every service in the cluster automatically - encrypting and authenticating pod-to-pod traffic without any change to application code - along with fine-grained traffic policies, retries, and observability that would otherwise need to be built into every application individually.


Supply Chain and Runtime Security

Container security extends past the network layer into the images themselves: scanning images for known-vulnerable dependencies before deployment, signing images cryptographically so a cluster can verify what it's running actually came from a trusted build pipeline (a practice explored further in the emerging-threats chapter), and runtime security tools (Falco is a widely used open-source example) that monitor for anomalous process behavior or unexpected network connections from a running container, applying the same behavioral detection philosophy from the IDS/IPS chapter directly to container workloads.


Frequently Asked Questions

If containers aren't really lightweight VMs, does that mean container isolation is basically fake? No, but it is worth respecting for what it actually is. Namespaces and cgroups genuinely separate what a process can see and how much of the host it can consume, and that boundary holds up in the overwhelming majority of day-to-day operation. The honest caveat is that it is a software boundary enforced by a shared kernel rather than a hardware boundary enforced by a hypervisor, which is exactly why running as non-root and dropping unnecessary privileges inside the container matters so much more here than it would for a VM.

We have namespaces separating our teams in Kubernetes - doesn't that mean their pods can't reach each other? This is the single most common misconception this chapter tackles, and it is worth repeating plainly: namespaces are an organizational and access-control boundary, not a network boundary. Unless a NetworkPolicy explicitly restricts traffic, a pod in the billing namespace can open a connection to a pod in payments just as freely as it can reach its own neighbor. If segmentation is the goal, a NetworkPolicy has to do that work.

Is it legal or ethical to run a vulnerability scan against my own company's container images or cluster? Scanning images and clusters you own or are authorized to test - as part of your own CI/CD pipeline, a sanctioned internal security review, or a bug bounty program that explicitly covers your infrastructure - is exactly the kind of defensive practice this chapter encourages. The line to never cross is scanning or probing containers, registries, or clusters that belong to someone else without their written permission; everything in this part of the book assumes authorized, ethical use only.

I applied a NetworkPolicy and my pods still seem to talk to everything - what did I miss? Check three things in order: whether your CNI plugin actually enforces NetworkPolicy (some do not by default, since the resource is just an API object until a compatible plugin implements it), whether the podSelector labels on the policy actually match the labels on your pods, and whether you have a default-deny policy in place at all - remember that a NetworkPolicy is purely additive, so one policy allowing frontend-to-api traffic does nothing to restrict a completely unrelated pod pair that no policy mentions.

How does a service mesh's mTLS relate to the TLS chapter earlier in this part? It is the same cryptographic idea applied at a different layer of the stack. Where an application developer might terminate TLS inside their own code using the patterns from the TLS/PKI chapter, a service mesh sidecar handles the handshake, certificate rotation, and encryption transparently outside the application entirely - which is powerful for consistency across a cluster, but it also means the mesh's control plane becomes a trust-critical component worth securing just as carefully as any certificate authority.


Summary

  • Container isolation relies on a shared host kernel, which is a weaker boundary than hypervisor-based VM isolation - least privilege inside containers matters accordingly.
  • Kubernetes' default pod network is flat and permissive; NetworkPolicy resources are required to enforce any meaningful pod-to-pod segmentation.
  • Namespace boundaries organize resources but do not, by themselves, restrict network traffic.
  • Service meshes automate mutual TLS and traffic policy across a cluster; image scanning, signing, and runtime monitoring extend security beyond the network layer into the supply chain.