> ## 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 Solution

> Walks through Kubernetes NetworkPolicy rules protecting a fictional Hogwarts cluster, explaining per-namespace intent, allowed and denied traffic, and practical examples.

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

| Namespace | Policy | Intent / Effect | Key selectors / notes |
| - | -: | - | - |
| `default` | `default-allow-all` | Allow all ingress and egress for selected pods | selects `i-am=dumbledore` (`podSelector: i-am=dumbledore`) |
| `charms` | `charms-and-students` | Allow ingress only from pods labeled `hogwarts=admitted` | `podSelector: {}` (all pods in `charms`); allows from `podSelector: hogwarts=admitted` |
| `charms` | `charms-no-egress` | Deny inbound (no ingress rules) and restrict egress to `charms` namespace | `policyTypes: [Ingress, Egress]`; `egress` to namespace `kubernetes.io/metadata.name=charms` |
| `potions` | `potions-rules` | Restrict both ingress and egress to pods labeled `class=potions` | `podSelector: {}`; `from/to` `podSelector: class=potions` |
| `potions` | `potions-port-rule` | Allow egress to `darkarts` namespace on TCP port 80 only | `policyTypes: [Egress]`; `to` namespace `darkarts` + `ports: 80/TCP` |
| `darkarts` | `darkarts-no-access` | Default deny both ingress and egress (isolate namespace) | `podSelector: {}`; `ingress: []`, `egress: []` |
| `darkarts` | `darkarts-magic` | Narrow exceptions: allow Dumbledore -> pods, and DNS egress to CIDR on port 53 | `ingress` from pod `i-am=dumbledore` in `default`; `egress` to `ipBlock: 10.0.0.0/24` ports 53 TCP/UDP |

## Default namespace — Dumbledore

A pod named Dumbledore exists in the `default` namespace with the label `i-am=dumbledore`:

```bash theme={null}
$ kubectl get pods -n default --show-labels
NAME         LABELS
dumbledore   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.

```bash theme={null}
$ kubectl describe netpol default-allow-all
Allowing ingress traffic:
  To Port: <any> (traffic allowed to all ports)
  From: <any> (traffic not restricted by source)
Allowing egress traffic:
  To Port: <any> (traffic allowed to all ports)
  To: <any> (traffic not restricted by destination)
```

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`.

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: charms-and-students
  namespace: charms
spec:
  podSelector: {}
  ingress:
  - from:
    - podSelector:
        matchLabels:
          hogwarts: admitted
```

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:

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: charms-no-egress
  namespace: charms
spec:
  podSelector: {}
  policyTypes:
  - Egress
  - Ingress
  egress:
  - to:
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: charms
```

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`:

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: potions-rules
  namespace: potions
spec:
  podSelector: {}
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          class: potions
  egress:
  - to:
    - podSelector:
        matchLabels:
          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.

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: potions-port-rule
  namespace: potions
spec:
  podSelector: {}
  policyTypes:
  - Egress
  egress:
  - to:
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: darkarts
    ports:
    - protocol: TCP
      port: 80
```

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`.

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: darkarts-no-access
  namespace: darkarts
spec:
  podSelector: {} # Applies to all pods in the namespace
  policyTypes:
  - Ingress
  - Egress
  ingress: []
  egress: []
```

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.

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: darkarts-magic
  namespace: darkarts
spec:
  podSelector: {}

  ingress:
  - from:
    - podSelector:
        matchLabels:
          i-am: dumbledore
      namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: default

  egress:
  - to:
    - ipBlock:
        cidr: 10.0.0.0/24
    ports:
    - protocol: TCP
      port: 53
    - protocol: UDP
      port: 53
```

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:

```bash theme={null}
$ kubectl describe netpol default-allow-all
PodSelector: i-am=dumbledore
Allowing ingress traffic:
  From: <any>
Allowing egress traffic:
  To: <any>

$ kubectl describe netpol -n charms charms-and-students
From:
  PodSelector: hogwarts=admitted

$ kubectl describe netpol -n charms charms-no-egress
PolicyTypes: Ingress, Egress
Egress:
  To:
    NamespaceSelector: kubernetes.io/metadata.name=charms

$ kubectl describe netpol -n potions potions-rules
From:
  PodSelector: class=potions
To:
  PodSelector: class=potions

$ kubectl describe netpol -n potions potions-port-rule
PolicyTypes: Egress
Egress:
  To Port: 80/TCP
  To:
    NamespaceSelector: kubernetes.io/metadata.name=darkarts

$ kubectl describe netpol -n darkarts darkarts-no-access
PolicyTypes: Ingress, Egress
Policy enforces isolation: ingress=[], egress=[]

$ kubectl describe netpol -n darkarts darkarts-magic
Ingress:
  From:
    PodSelector: i-am=dumbledore
    NamespaceSelector: kubernetes.io/metadata.name=default
Egress:
  To Port: 53/TCP, 53/UDP
  To:
    IPBlock: 10.0.0.0/24
```

## 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.

## Links and references

* Kubernetes NetworkPolicy — [https://kubernetes.io/docs/concepts/services-networking/network-policies/](https://kubernetes.io/docs/concepts/services-networking/network-policies/)
* Kubernetes Networking Concepts — [https://kubernetes.io/docs/concepts/cluster-administration/networking/](https://kubernetes.io/docs/concepts/cluster-administration/networking/)

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.

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/learn-by-doing-kubernetes-network-policies/module/11c1a4c9-e4a0-47d5-a73e-0ac35acf6e8e/lesson/68d8d26d-4435-45a3-9cea-249a717e6801)


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