Kubernetes Network Policies are a critical security primitive that allow you to define how groups of pods are allowed to communicate with each other and with external network endpoints. Operating at Layer 3/4 (IP address and port level), they enable you to enforce the principle of "least privilege" within your cluster's network. Instead of having a flat network where any pod can talk to any other pod by default, Network Policies provide a declarative way to segment your application's network traffic, significantly reducing the attack surface and preventing unauthorized communication paths.
Practically, you define Network Policies as YAML manifest files. Each policy targets a specific set of pods using a podSelector in its spec. Once targeted, you then specify ingress (incoming) and/or egress (outgoing) rules. These rules can restrict traffic based on various criteria: source/destination pods (using podSelector or namespaceSelector), IP CIDR blocks (ipBlock), and specific ports and protocols. It's crucial to understand that Network Policies are opt-in; a pod is not restricted by a policy unless it is selected by that policy. If a pod is selected by a policy with policyTypes: Ingress, then all ingress traffic to that pod is denied by default unless explicitly allowed by a rule. The same applies to Egress.
Implementing Network Policies is fundamental for building secure, resilient Kubernetes deployments. They are invaluable for preventing lateral movement within your cluster in the event of a compromised pod, as an attacker would be confined to only what the compromised pod is explicitly allowed to communicate with. Furthermore, they are often a requirement for regulatory compliance (e.g., PCI DSS). For Network Policies to function, your Kubernetes cluster must be using a Container Network Interface (CNI) plugin that supports them, such as Calico, Cilium, or Weave Net. Without a supporting CNI, these policies will have no effect, highlighting the importance of proper infrastructure setup.
Key Takeaways
- Kubernetes Network Policies enforce Layer 3/4 (IP/port) traffic rules between pods.
- They are declarative YAML objects defining
ingress(incoming) andegress(outgoing) rules. - Network Policies help implement the principle of least privilege for pod communication, reducing attack surface.
- They are opt-in; a pod is unrestricted by default until selected by a Network Policy.
- A CNI plugin (e.g., Calico, Cilium) that supports Network Policies is required for them to be enforced.
Code Example
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-frontend-to-backend
namespace: default
spec:
# Apply this policy to pods labeled 'app: backend'
podSelector:
matchLabels:
app: backend
policyTypes:
- Ingress # This policy only defines ingress rules
ingress:
# Allow incoming traffic only from pods labeled 'app: frontend'
- from:
- podSelector:
matchLabels:
app: frontend
# ...and only on TCP port 8080
ports:
- protocol: TCP
port: 8080How this code works
This Kubernetes NetworkPolicy defines crucial security rules for traffic flowing into a cluster's services. Its primary job is to ensure that only authorized services can communicate with specific backend components. Specifically, this policy dictates that pods labeled app: backend are the target of these rules, identified by the podSelector. The policyTypes: Ingress line explicitly states that this policy only governs incoming traffic to these backend pods, not traffic originating from them.
Within the ingress section, the policy specifies exactly what kind of incoming connections are permitted. The from rule uses a podSelector to allow traffic only from pods labeled app: frontend. This establishes a secure communication channel, ensuring only frontend services can reach the backend. Furthermore, the ports rule restricts this allowed traffic to protocol: TCP on port: 8080, preventing unauthorized access on other ports. A key point for beginners: once an Ingress NetworkPolicy is applied to a pod, all traffic not explicitly allowed by an ingress rule will be automatically blocked. This "default deny" behavior makes Network Policies a powerful security tool.