> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Securing Hogwarts The Problem

> Explains Hogwarts Kubernetes cluster security gaps and specifies seven exact NetworkPolicy rules to isolate namespaces, restrict ingress and egress, control ports, and allow specific IP ranges

Bob is the new network security engineer for Hogwarts. The cluster he inherits runs several magical departments, each in its own Kubernetes namespace — but the cluster is wide open. There are no NetworkPolicies, so pods can talk to any other pod across all namespaces.

This page explains the situation Bob finds, why it’s a problem, and the exact NetworkPolicy requirements Hogwarts gave him. Use this as a clear checklist when you write the seven policies: it covers names, labels, ports, namespace selectors, IPBlocks, and whether rules are ingress or egress.

What Bob finds in the cluster

* Four namespaces exist: `default`, `charms`, `potions`, and `dark-arts`.
* `default` contains one pod named Dumbledore with label `app=Dumbledore`.
* `charms`, `dark-arts`, and `potions` each contain three pods (names like Expecto Patronum, Imperio, Polyjuice) that have no labels.
* No NetworkPolicies are present, so all pods can reach every other pod by default.

Cluster summary

| Namespace | Pods (examples) | Important labels |
| -: | - | - |
| `default` | Dumbledore (1) | `app=Dumbledore` |
| `charms` | Expecto Patronum, ... (3) | *no pod labels* |
| `potions` | Polyjuice, ... (3) | *no pod labels* |
| `dark-arts` | Imperio, ... (3) | *no pod labels* |

Why this is risky

* Any pod in `charms` or `potions` can communicate directly with `dark-arts` pods — and vice versa.
* A compromise in one department can spread laterally across the cluster unnoticed.
* Hogwarts requires department-level isolation so each namespace can only access what it needs.

Hogwarts’ exact requirements
Hogwarts gave Bob a diagram and seven strict policies to implement. Every name, label, and port must match exactly — one typo can open or close a path incorrectly. We'll walk through requirements namespace by namespace.

| Namespace | Policy name | Requirement |
| -: | - | - |
| `default` | `default allow all` | Allow Dumbledore unlimited ingress and egress (no traffic restrictions). |
| `charms` | `Charms and students` | Allow ingress from pods labeled `hogwarts-admitted`. |
| `charms` | `Charms no egress` | Deny egress to all namespaces except `charms` (pods can talk only within `charms`). |
| `potions` | `Potions rules` | Limit both ingress and egress to pods labeled `class=potions`. |
| `potions` | `Potions port rule` | Allow egress to the `dark-arts` namespace only on TCP port `80`. |
| `dark-arts` | `Dark Arts no access` | Block all ingress and all egress by default. |
| `dark-arts` | (two small openings) | Allow ingress only from the Dumbledore pod in `default` namespace; allow egress only on port `53` (TCP and UDP) to IP range `10.0.0.0/24`. |

Key NetworkPolicy features you'll use

* Default-deny rules (deny-all ingress and/or egress).
* Explicit allow rules for specific pods and namespaces.
* Pod selectors, namespace selectors, or a combination (podSelector + namespaceSelector).
* Port restrictions (TCP/UDP and specific port numbers).
* IPBlock for egress to an IP range (e.g., `10.0.0.0/24`).
* Policy types: `Ingress`, `Egress`, or both.

![An illustrated diagram titled "Everything Bob Has Learned" showing blue smiling pod icons connected by dashed arrows to represent network/Kubernetes policies. Labels include terms like charms, potions, hogwarts=admitted, class=potions, port numbers and the 10.0.0.0/24 subnet.](https://mintcdn.com/kodekloud-c4ac6d9a/UCHQITKCSrYPqxtk/images/Learn-By-Doing-Kubernetes-Network-Policies/Securing-Hogwarts/Securing-Hogwarts-The-Problem/bob-k8s-pods-policies-hogwarts-subnet.jpg?fit=max\&auto=format\&n=UCHQITKCSrYPqxtk\&q=85\&s=d810abeacb46cdc656eedd2bde9a80da)

Two hints from Hogwarts: use the labels that already exist, and remember that every namespace has a built-in label containing its name.

Before you read the solution, try it yourself: seven NetworkPolicy manifests across four namespaces. If you implement them, verify with `kubectl get networkpolicies --all-namespaces` and test connectivity using `kubectl exec` or a network test pod.

Links and references

* [Kubernetes NetworkPolicy documentation](https://kubernetes.io/docs/concepts/services-networking/network-policies/)
* [How NetworkPolicy selects pods](https://kubernetes.io/docs/concepts/services-networking/network-policies/#networkpolicy-objects)
* [IPBlock usage in NetworkPolicy](https://kubernetes.io/docs/concepts/services-networking/network-policies/#ipblock)

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/learn-by-doing-kubernetes-network-policies/module/11c1a4c9-e4a0-47d5-a73e-0ac35acf6e8e/lesson/56128339-6e8a-4f09-9bd0-740a3252a5b5)

> [card] [Practice Lab](https://learn.kodekloud.com/user/courses/learn-by-doing-kubernetes-network-policies/module/11c1a4c9-e4a0-47d5-a73e-0ac35acf6e8e/lesson/5a13175a-11e0-4533-95b2-88d3aadebbd1)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.