Skip to main content
In this lab we practice Kubernetes NetworkPolicies. We’ll inspect the cluster, identify existing NetworkPolicies, interpret their effect, run connectivity checks, and then create a policy that restricts egress from the internal pod so it can only reach the payroll and mysql pods. Note: the examples below use the k alias for kubectl. If you don’t have that alias, either set it (alias k=kubectl) or replace k with kubectl in the commands.
By default, pods allow all ingress and egress traffic. When a NetworkPolicy selects a pod for a particular policy type (Ingress or Egress), traffic in that direction becomes restricted: only the explicitly allowed traffic is permitted.

1) Inspect the cluster: pods and services

Start by listing pods in the default namespace:
List services and their exposed ports:
Quick reference (services of interest):

2) Identify NetworkPolicies in the environment

List NetworkPolicies in the namespace:
Describe the payroll-policy to see its rules:
Interpretation:
  • The policy selects the payroll pod (name=payroll).
  • It allows ingress only to TCP port 8080 and only from pods labeled name=internal.
  • There are no egress rules defined for payroll, so egress from the payroll pod is unrestricted.
  • Because Policy Types includes only Ingress, ingress to the payroll pod is restricted to the defined sources; other ingress is denied.

3) What traffic is allowed / blocked by payroll-policy?

Summary: So, for example:
  • internal -> payroll:8080 is allowed.
  • external -> payroll:8080 is blocked.
  • payroll -> anywhere is allowed.

4) Connectivity verification (UI / application tests)

Using the apps’ web UI or built-in connectivity tests:
  • From the internal-facing application: connecting to payroll:8080 succeeds (allowed).
  • From the external-facing application: connecting to payroll:8080 times out / fails (blocked).
Other interactions:
  • internal -> external:8080 succeeds (no policy prevents that).
  • internal -> database (mysql:3306) currently succeeds (no egress policy yet).
These observed behaviors match the payroll-policy.
Caution: when you add policyTypes: ["Ingress"] or ["Egress"] without specifying the corresponding rules, you will restrict that traffic direction. For example, adding Ingress without any ingress rules will deny all ingress to the selected pods.

5) Task: Create a policy to restrict egress from internal

Goal: create a NetworkPolicy named internal-policy that selects the internal pod and allows egress only to:
  • payroll pod on TCP port 8080
  • mysql pod on TCP port 3306
All other egress traffic should be denied. Create a file named internal-policy.yaml with the following manifest:
Manifest notes:
  • podSelector.matchLabels.name: internal targets the internal pod(s).
  • policyTypes: ["Egress"] restricts only egress; ingress behavior remains unchanged.
  • The egress rules permit only the two specified destinations. When a pod is selected by a NetworkPolicy that defines Egress, all other egress traffic is denied unless another policy allows it.

6) Apply the policy and verify

Apply the manifest:
Describe the new policy to confirm it was created correctly:
Verification checklist:
  • The internal pod should now only be able to reach:
    • payroll:8080 (TCP)
    • mysql:3306 (TCP)
  • Attempts from internal to reach any other service or external host should be blocked unless another NetworkPolicy explicitly allows them.
  • The existing payroll-policy still restricts ingress to payroll (only from internal), so payroll behavior remains the same.
That’s it — internal-policy is created and restricts egress from the internal pod to only the payroll and DB services.

Watch Video