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 thedefault namespace with the label i-am=dumbledore:
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.
Charms namespace — Charms and Students
Thecharms-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.
- If no pod currently has the
hogwarts=admittedlabel, ingress tocharmspods is closed until a pod is labeled. - Because policies are additive, other NetworkPolicies can further restrict or relax traffic (see
charms-no-egressnext).
Charms namespace — CharmsNoEgress
Thecharms-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:
- Because
Ingressis listed inpolicyTypesbut noingressrules 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:
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.
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.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=dumbledorein thedefaultnamespace. - Allow egress (as implemented here) to a specific IP block on port 53 for DNS (both TCP and UDP).
ingress and egress fields, Kubernetes infers both Ingress and Egress policy types for this object.
- The single
fromitem for ingress contains both apodSelectorand anamespaceSelectorin the same entry: these are ANDed. The source must be a pod labeledi-am=dumbledorelocated in thedefaultnamespace. - In egress rules,
ipBlockandportswithin the same rule are also ANDed: the destination IP must match the CIDR and the destination port must match the listed ports.
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
Usekubectl 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
fromortoentry 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-studentsallowshogwarts=admittedpods in;charms-no-egressrestricts outbound to thecharmsnamespace.potions-rulesisolates Potions toclass=potions, withpotions-port-ruleadding an egress todarkartson TCP 80.darkarts-no-accessisolates Dark Arts by default;darkarts-magicprovides narrow exceptions for Dumbledore and DNS egress.
- Dumbledore is allowed everywhere in
Links and references
- Kubernetes NetworkPolicy — https://kubernetes.io/docs/concepts/services-networking/network-policies/
- Kubernetes Networking Concepts — https://kubernetes.io/docs/concepts/cluster-administration/networking/