In this walkthrough, we’ll explore how to secure Pod-to-Pod and Pod-to-External traffic in Kubernetes using NetworkPolicies. You will learn to:Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
- Verify the default connectivity behavior
- Apply default-deny rules for egress and ingress
- Permit specific egress/ingress to selected Pods
- Validate the resulting network restrictions
1. Verify Default Connectivity
By default, Kubernetes allows all egress and ingress traffic between Pods (even across namespaces) and to the Internet.1.1 Test External Connectivity
Exec into pod1 (in thedefault namespace) and ping an external endpoint:
1.2 Test Cross-Namespace Connectivity
List Pod IPs inkube-system and pick one (e.g. 192.168.121.187):
By default, no NetworkPolicy is enforced, so all traffic flows freely.
2. Apply Default-Deny Egress
To block all outbound traffic from Pods in thedefault namespace, create a default-deny egress policy.
policyTypes: [Egress] and no egress rules.
2.1 Validate Egress Blocking
Attempt to ping Google and a cross-namespace Pod—both should time out:3. Apply Default-Deny Ingress
Similarly, deny all inbound traffic to Pods indefault:
kube-system, try to curl pod2 (NGINX):
Applying default-deny policies without specific allow rules can disrupt critical workloads. Always plan your policies carefully.
4. Allow Specific Egress and Ingress
Once Pods are isolated by default, define exceptions:| Policy Name | Direction | Allowed Peer Pods | Port |
|---|---|---|---|
default-deny-egress | Egress | app=nginx | 80 |
default-deny-ingress | Ingress | app=centos | 80 |
4.1 Permit Egress to NGINX Pods
Update deny-egress.yaml:4.2 Permit Ingress from Management Pods
Update deny-ingress.yaml:5. Verify Selective Connectivity
-
Allowed: From pod1 → NGINX on port 80
You should see the NGINX welcome page.
-
Blocked: From pod1 → NGINX on port 8080
Connections on other ports will time out.
Recap
- Kubernetes defaults to allow all ingress/egress traffic.
- Default-deny policies lock down Pods by default.
- Fine-tune communication by defining egress and ingress rules matching labels, ports, and namespaces.