Skip to main content
This lab walks through a practical NetworkPolicy exercise: inspecting an existing policy that restricts access to a payroll pod, then creating a new NetworkPolicy to restrict egress from an internal pod. You’ll review the cluster state, interpret the active policy, implement a policy that limits egress, and validate the result.

1. Inspect the cluster

Start by listing pods and services to understand which workloads and endpoints exist. Pods:
Services:
Quick summary of key resources:

2. Check for existing NetworkPolicies

List NetworkPolicies to see what’s already enforced:
There is one policy: payroll-policy, which targets pods with the label name=payroll. Describe that policy to see the exact rules:
Interpretation:
  • payroll-policy selects the payroll pod and defines Ingress rules only.
  • Because the policy selects the pod and includes Ingress rules, any ingress not explicitly allowed will be denied. In this case only traffic from pods labeled name=internal to TCP port 8080 is permitted.
  • There are no Egress rules for the payroll pod, so its egress remains unrestricted.
Note: By default, pods allow all ingress and egress. When a NetworkPolicy selects a pod and defines Ingress rules, it denies other ingress that is not explicitly allowed. Likewise, defining Egress rules denies other egress unless explicitly allowed. To restrict only egress (and leave ingress unchanged), set policyTypes to Egress only and omit ingress.
Connectivity (high-level):
  • internal → payroll on TCP 8080: allowed (policy explicitly permits).
  • external → payroll on TCP 8080: blocked/timeout (not allowed by policy).
  • ICMP (ping) is not covered by TCP rules — NetworkPolicies work at L3/L4 (TCP/UDP/SCTP).

3. Task: restrict egress from the internal pod

Goal
  • Create a NetworkPolicy named internal-policy.
  • Target the pod labeled name=internal.
  • Restrict egress so the internal pod can only initiate connections to:
    • the payroll pod on TCP port 8080
    • the mysql pod on TCP port 3306
  • Do not change ingress behavior for the internal pod (leave ingress unaffected).
(Reference screenshot of Kubernetes docs used to get the template: https://kubernetes.io/docs/concepts/services-networking/network-policies/)
A screenshot of the Kubernetes documentation webpage, with a blue header announcing "Dockershim removal set for Kubernetes 1.24" and a left navigation pane. The main content shows three columns titled "Understand Kubernetes," "Try Kubernetes," and "Set up a K8s cluster" with links and action buttons.
Create internal-policy.yaml with the following content:
Apply the policy:
Verify the policy with describe:
Result
  • The internal pod can now only initiate outbound TCP connections to:
    • payroll pods on port 8080
    • mysql pods on port 3306
  • All other egress connections (to other pods or external IPs) are denied by default because the NetworkPolicy selects the pod and defines Egress rules.
  • Ingress to the internal pod remains unchanged by this policy.
Validation tips
  • Exec into the internal pod and test connectivity:
    • Test allowed connections (e.g., curl or nc to payroll:8080 and mysql:3306).
    • Test denied connections (e.g., try connecting to external-service or other cluster IPs).
  • Example validation commands (run from within the internal pod):
    • # curl http://payroll-service:8080 (should succeed)
    • # nc -zv payroll 8080 (should succeed)
    • # nc -zv mysql 3306 (should succeed)
    • # nc -zv external-service 8080 (should fail / timeout)

Watch Video