Skip to main content
In this hands-on walkthrough, Bob inspects a Kubernetes cluster, verifies application connectivity, and experiments with default-deny NetworkPolicies to lock down a namespace. Objectives:
  • Inspect cluster resources and CNI
  • Verify service connectivity using a pod
  • Create and apply default-deny ingress NetworkPolicies
  • Observe differences between absent ingress and ingress: []
  • Outline how to restore only the minimal required traffic paths

Inspect the cluster

Bob starts by listing pods and services in the default namespace to understand what’s running:
Below the pods there are three services (two serving on port 80 and one on port 3306). He confirms there are no existing NetworkPolicies in default:
To determine which CNI is enforcing network policies, Bob lists pods across all namespaces and inspects kube-system:
He opens the Canal CNI config and sees that Canal delegates policy enforcement to Calico. The active CNI is Canal (with Calico providing policy enforcement). For more detail, see the Canal and Calico project pages:
NetworkPolicy enforcement depends on your CNI plugin. If your cluster uses a CNI that doesn’t support Kubernetes NetworkPolicies, the policies will have no effect—Calico and Canal are common CNIs that support them.

Verify connectivity between services

With no NetworkPolicies in place, Bob verifies connectivity from a frontend pod to the middleware and MySQL services. He lists frontend pods, chooses one, and runs netcat inside it. Netcat is run with -z (scan without sending data), -v (verbose), and -w 3 (3-second timeout) so the command doesn’t hang:
Both services respond, which is expected when no NetworkPolicies restrict traffic.

Apply a default-deny ingress policy

Bob writes his first policy to deny all ingress to pods in the default namespace. The podSelector is empty ({}), selecting all pods, policyTypes contains only Ingress, and no ingress rules are provided:
Because this policy selects all pods and declares it applies to ingress but contains no ingress rules, it prevents any incoming traffic to those selected pods. Bob applies the policy:
From this point, incoming connections to pods in default are denied unless another NetworkPolicy explicitly allows them.

Add a broad allow policy (to demonstrate policy behavior)

Next, Bob adds a second policy, default-allow-ingress, that also selects all pods (podSelector: {}) but contains a single, empty ingress rule (- {}), which matches all sources and allows all incoming traffic:
Now two policies select the same pods: one that allows nothing, and one that allows everything. The cluster diagram below illustrates these policies and the service relationships.
A stylized Kubernetes diagram showing frontend, middleware, and MySQL pods and their services with arrows indicating traffic flows. Locks and labeled policy badges (default-deny-ingress / default-allow-ingress) illustrate network policies controlling access.
Bob re-tests connectivity from a frontend pod to the middleware and MySQL services:
Both connections are still allowed.
NetworkPolicies are additive: when multiple policies select the same pod, traffic is allowed if any matching policy permits it. There is no “priority” where a deny in one policy overrides an allow in another—omitting rules effectively denies, while an explicit allow in any policy permits traffic.

Expressing “no ingress” — absent ingress vs ingress: []

Bob explores two equivalent ways to express “no ingress rules.” He deletes existing policies and creates a policy named default-deny-ingress-null that omits the ingress field entirely:
He creates it:
This denies all ingress to the selected pods. An equivalent variant sets ingress to an empty list (ingress: []). Both forms are functionally identical: they result in no allowed ingress. When the cluster stores or prints these policies, it may show either representation. For example, the cluster might present both policies like:
Either omitting the ingress field or setting it to an empty list ([]) yields the same behavior: no ingress is allowed.

Effect: namespace locked down and application broken

With a default-deny ingress policy in place that selects all pods, the namespace is locked down. Incoming connections required by the application are dropped, so the frontend can no longer reach middleware and middleware can no longer reach MySQL.
A stylized architecture diagram titled "Locked Down and Broken" showing frontend, middleware, and MySQL pods/services inside a Kubernetes "default" namespace with lock icons and a blocked link between frontend and middleware. Service names and ports (frontend-svc: 80, middleware-svc: 80, mysql-svc: 3306) are also displayed.

Next step — restore only the minimal allowed paths

To restore required functionality while keeping the namespace secure, add minimal, specific allow rules that permit only the required service-to-service connections. For example:
  • Allow frontend → middleware on port 80
  • Allow middleware → mysql on port 3306
A practical approach is to create focused NetworkPolicies that select the destination pod(s) and allow ingress only from specific pod selectors and ports.

Quick reference: examples and effects

Note: in the table above, examples with curly braces are shown as inline code (e.g., podSelector: {}) so MDX does not interpret them as object literals.
Commands
  • List pods and services: kubectl get pods,svc
  • List all NetworkPolicies in a namespace: kubectl get networkpolicy -n <namespace>
  • Apply a policy: kubectl apply -f <policy.yaml>
  • Test connectivity from a pod: kubectl exec <pod> -- nc -zv -w 3 <host> <port>
References If you want, I can show example minimal allow NetworkPolicy manifests for the frontend→middleware and middleware→mysql paths.

Watch Video

Practice Lab