Skip to main content
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:
  1. Verify the default connectivity behavior
  2. Apply default-deny rules for egress and ingress
  3. Permit specific egress/ingress to selected Pods
  4. 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 the default namespace) and ping an external endpoint:
You should see successful responses:

1.2 Test Cross-Namespace Connectivity

List Pod IPs in kube-system and pick one (e.g. 192.168.121.187):
From pod1, ping that IP:
You should receive replies, confirming open egress/ingress.
By default, no NetworkPolicy is enforced, so all traffic flows freely.

2. Apply Default-Deny Egress

To block all outbound traffic from Pods in the default namespace, create a default-deny egress policy.
Apply and verify:
You should see policyTypes: [Egress] and no egress rules.

2.1 Validate Egress Blocking

Attempt to ping Google and a cross-namespace Pod—both should time out:
No responses will be received.

3. Apply Default-Deny Ingress

Similarly, deny all inbound traffic to Pods in default:
Apply the policy:
From a Pod in kube-system, try to curl pod2 (NGINX):
You should see a timeout.
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:

4.1 Permit Egress to NGINX Pods

Update deny-egress.yaml:
Apply the updated policy:

4.2 Permit Ingress from Management Pods

Update deny-ingress.yaml:
Apply the updated policy:

5. Verify Selective Connectivity

  1. Allowed: From pod1 → NGINX on port 80
    You should see the NGINX welcome page.
  2. 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.

Watch Video

Practice Lab