1. Inspect the cluster
Start by listing pods and services to understand which workloads and endpoints exist. Pods:2. Check for existing NetworkPolicies
List NetworkPolicies to see what’s already enforced:payroll-policy, which targets pods with the label name=payroll.
Describe that policy to see the exact rules:
payroll-policyselects thepayrollpod 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=internalto TCP port 8080 is permitted. - There are no Egress rules for the
payrollpod, 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.- 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
internalpod 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
internalpod (leave ingress unaffected).

internal-policy.yaml with the following content:
- The
internalpod can now only initiate outbound TCP connections to:payrollpods on port 8080mysqlpods 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
internalpod remains unchanged by this policy.
- Exec into the
internalpod and test connectivity:- Test allowed connections (e.g.,
curlorncto payroll:8080 and mysql:3306). - Test denied connections (e.g., try connecting to
external-serviceor other cluster IPs).
- Test allowed connections (e.g.,
- Example validation commands (run from within the
internalpod):# 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)
Links and references
- Kubernetes NetworkPolicy docs: https://kubernetes.io/docs/concepts/services-networking/network-policies/
- Kubernetes concepts overview: https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/
- CKA / networking resources: https://github.com/cncf/curriculum (useful for exam-style labs)