default namespace there is a simple three‑tier app split into:
- Frontend: a Deployment named
frontend-deploywith two pods, both labeledapp=frontend. This is what users talk to. - Middleware: a single pod labeled
app=middleware. It accepts requests from the frontend and reads/writes data. - Database: a MySQL pod labeled
app=mysql.
Right now there are no NetworkPolicies in the namespace. That means:
- A frontend pod can reach the middleware (intended).
- A frontend pod can also reach MySQL on port
3306(not intended).

A default deny intentionally blocks all ingress traffic to selected pods. Expect the app to stop working until you add explicit allow rules for the paths it needs.
Bob’s workflow (practical steps)
- Inspect the namespace: list pods, services, and existing NetworkPolicy objects.
- Example commands:
kubectl get pods -n default -o widekubectl get svc -n defaultkubectl get networkpolicy -n default
- Example commands:
- Confirm the cluster CNI supports NetworkPolicy enforcement. Canal (Calico + Flannel) supports NetworkPolicy via Calico components.
- Reference: Kubernetes NetworkPolicy docs
- Test current connectivity from a frontend pod:
- Exec into a frontend pod and curl the middleware and MySQL service to observe reachable paths:
kubectl exec -it <frontend-pod> -n default -- curl -sS http://middleware:80kubectl exec -it <frontend-pod> -n default -- nc -vz mysql 3306
- Exec into a frontend pod and curl the middleware and MySQL service to observe reachable paths:
- Write and apply a default deny NetworkPolicy named
default-deny-ingressthat selects every pod and declaresIngressas a policy type, but provides no ingress rules — this blocks incoming connections to all pods in the namespace. - With the deny present, add a second policy
default-allow-ingressthat explicitly allows everything. This creates two policies selecting the same pods but with opposite effects; in NetworkPolicy semantics the union of allowed traffic applies. - Experiment with deleting and re-applying policies to learn how different policy shapes express “no traffic” vs specific allows.
Default deny examples
A minimal default deny NetworkPolicy (selects all pods and denies all ingress):How NetworkPolicy rules combine
- NetworkPolicy semantics are additive. If one policy selects a pod and denies ingress (no ingress rules) and another policy selects the same pod and allows ingress (for example
ingress: - {}), the union of the two policies results in the allowed traffic taking effect — effectively permitting connections that the allow policy describes. - Practically: a
default-denybaseline plus targeted allow policies is the recommended pattern. The deny provides a safe starting point; allow policies open only required paths.
Tip: Use a strict default deny as a baseline. Then write minimal, intent-driven allow policies (e.g., frontend -> middleware on port 80, middleware -> mysql on 3306). Test each allow rule incrementally to restore only the required connectivity.
Quick checklist and references
-
Checklist:
- Confirm CNI supports NetworkPolicy (Calico/Canal do).
- Apply
default-deny-ingressfirst. - Add specific allow policies for required paths:
- Frontend → Middleware (
port: 80, sourceapp=frontend→ destapp=middleware) - Middleware → MySQL (
port: 3306, sourceapp=middleware→ destapp=mysql)
- Frontend → Middleware (
- Verify connectivity after each change using
kubectl execand tools likecurlornc.
- Useful links: