Skip to main content
This lesson walks through the NetworkPolicy objects protecting the Hogwarts cluster. We examine each namespace in order, explain the intent of each policy, and show what traffic is explicitly allowed or denied.
Note about empty rules: In a NetworkPolicy, a rule that is an empty item (for example ingress: - {}) matches all sources and ports (it effectively allows everything for that direction). By contrast, an empty list (ingress: []) explicitly denies all ingress to the selected pods.

Quick policy summary

Default namespace — Dumbledore

A pod named Dumbledore exists in the default namespace with the label i-am=dumbledore:
A NetworkPolicy named default-allow-all selects that pod and contains a single empty ingress rule and a single empty egress rule. Each rule is an empty YAML object (for example ingress: - {} and egress: - {}) — meaning “allow all” for that direction.
Because both directions explicitly allow any source/destination and any port, the Dumbledore pod is unrestricted by NetworkPolicy.

Charms namespace — Charms and Students

The charms-and-students NetworkPolicy selects all pods in the charms namespace and allows ingress only from pods labeled hogwarts=admitted. The policy omits policyTypes; Kubernetes infers the types from the presence of ingress or egress.
Behavioral notes:
  • If no pod currently has the hogwarts=admitted label, ingress to charms pods is closed until a pod is labeled.
  • Because policies are additive, other NetworkPolicies can further restrict or relax traffic (see charms-no-egress next).

Charms namespace — CharmsNoEgress

The charms-no-egress policy selects all pods in charms, explicitly lists both Ingress and Egress in policyTypes, and provides an egress rule that permits traffic only to the charms namespace:
Important semantics:
  • Because Ingress is listed in policyTypes but no ingress rules are supplied, this policy denies all ingress to the selected pods by default.
  • However, another policy (such as charms-and-students) can still grant ingress if its rules permit the traffic; NetworkPolicy evaluation is additive — traffic is allowed if any applicable policy permits it.

Potions namespace — Potions rules

potions-rules selects all pods in potions and explicitly restricts both ingress and egress to pods with the label class=potions:
Result: Potions pods may only initiate connections to other pods labeled class=potions, and may only receive connections from those same pods.

Potions namespace — PotionsPort rule

potions-port-rule augments potions-rules by allowing an additional egress path: Potions pods can reach the darkarts namespace, but only on TCP port 80. Note that both the namespace match and the port match must be satisfied for the rule to allow traffic.
This enforces a precise allowance: destination namespace darkarts AND destination TCP port 80. Other ports or destinations are not permitted by this rule.

Dark Arts namespace — Full default deny

darkarts-no-access is a strict isolation policy that selects all pods in darkarts and explicitly denies both ingress and egress by declaring empty lists for both directions. It also lists both Ingress and Egress in policyTypes.
Warning: ingress: [] and egress: [] are explicit deny lists. When a policy lists a policyTypes entry but provides an empty list for that direction, nothing is allowed for that direction unless another NetworkPolicy explicitly permits it.
Because the lists are empty, the selected pods are isolated for both ingress and egress (no traffic allowed) unless additional policies allow specific traffic.

Dark Arts namespace — Darkarts magic (narrow exceptions)

darkarts-magic layers narrow exceptions on top of the full deny:
  • Allow ingress from the single pod labeled i-am=dumbledore in the default namespace.
  • Allow egress (as implemented here) to a specific IP block on port 53 for DNS (both TCP and UDP).
Because the NetworkPolicy contains both ingress and egress fields, Kubernetes infers both Ingress and Egress policy types for this object.
Semantics to remember:
  • The single from item for ingress contains both a podSelector and a namespaceSelector in the same entry: these are ANDed. The source must be a pod labeled i-am=dumbledore located in the default namespace.
  • In egress rules, ipBlock and ports within the same rule are also ANDed: the destination IP must match the CIDR and the destination port must match the listed ports.
Combined, darkarts-no-access plus darkarts-magic isolate Dark Arts pods by default and permit only the small, well-audited exceptions above.

Inspecting policies with kubectl

Use kubectl describe netpol to inspect a NetworkPolicy and confirm how the controller interprets it. Example outputs from this cluster:

Key takeaways

  • NetworkPolicy objects are additive: multiple policies can apply to the same pod. Traffic is allowed if any matching policy permits it.
  • An empty rule item (e.g. ingress: - {}) allows all for that direction; an empty list (ingress: []) denies all for that direction.
  • podSelector: {} selects all pods in the NetworkPolicy’s namespace.
  • Selectors within the same from or to entry are ANDed; multiple list items are ORed.
  • The Hogwarts cluster now has explicit, auditable rules mapping allowed network paths to policy files:
    • Dumbledore is allowed everywhere in default (default-allow-all).
    • charms-and-students allows hogwarts=admitted pods in; charms-no-egress restricts outbound to the charms namespace.
    • potions-rules isolates Potions to class=potions, with potions-port-rule adding an egress to darkarts on TCP 80.
    • darkarts-no-access isolates Dark Arts by default; darkarts-magic provides narrow exceptions for Dumbledore and DNS egress.
With these policies in place, each network path through Hogwarts is explicitly allowed or denied and has a clear policy document you can review and audit.

Watch Video