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

# Reading a NetworkPolicy The Problem

> Explains how to read and interpret Kubernetes NetworkPolicy YAML, focusing on deny-all semantics, selectors, policyTypes, ingress and egress rules, and examples

So what does a `deny-all` NetworkPolicy actually say, line by line?

Bob copied a file, applied it, and watched Canal block traffic from alpha to beta. That confirmed the file worked for a quick test, but it didn’t teach him how to read or write policies. Soon he’ll need to write policies for his apps — and you can’t write what you can’t read.

Here is the `deny-all` policy Bob used:

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: deny-all
  namespace: default
spec:
  podSelector: {}
  policyTypes:
    - Ingress
    - Egress
  ingress: []
  egress: []
```

This is valid Kubernetes YAML. Indentation shows the hierarchy; the three top-level descriptive fields and the behavioral field are worth separating so reading becomes straightforward.

* `apiVersion` — Which API group/version this object belongs to. For NetworkPolicies: `networking.k8s.io/v1`.
* `kind` — The type of object. For every policy you’ll write, this is `NetworkPolicy`.
* `metadata` — Names the policy and sets the `namespace`. Important: NetworkPolicies are namespace-scoped. A policy only affects pods in its own namespace.
* `spec` — Contains the behavior: selectors, policy directions, and the actual rules.

Example header showing those four fields together:

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: simple-network-policy
  namespace: default
spec:
  # rules live here
```

Common fields you’ll see inside `spec`:

* `podSelector` — Chooses which pods the policy applies to. An empty selector selects all pods in the namespace.
* `policyTypes` — Lists which directions the policy covers: `Ingress`, `Egress`, or both.
* `ingress` — A list of rules that allow traffic into selected pods.
* `egress` — A list of rules that allow outbound traffic from selected pods.

Quick reference table

| Field | Purpose | Example |
| - | - | - |
| `podSelector` | Selects which pods the policy applies to | `podSelector: {}` selects all pods in the namespace |
| `policyTypes` | Which directions are controlled | `policyTypes: [ "Ingress", "Egress" ]` |
| `ingress` | Rules permitting incoming traffic | `ingress: []` (no allowed ingress peers) |
| `egress` | Rules permitting outgoing traffic | `egress:` with entries to allow destinations |
| `ipBlock` | Allow traffic from an IP CIDR | `ipBlock: { cidr: 172.17.0.0/16 }` |

Two small symbols matter a lot:

* An empty pair of curly braces: `podSelector: {}` — commonly used to mean “select all pods in the namespace”.
* An empty pair of square brackets: `ingress: []` or `egress: []` — means “there are no rules for this direction”.

Those characters completely change what a policy does. In the `deny-all` example above, the policy selects all pods and sets both `ingress: []` and `egress: []` while `policyTypes` covers both directions. The effect: deny all ingress and egress for the selected pods (all pods in the `default` namespace).

Examples (common patterns)

* Selector with a pod label that allows only certain sources:

```yaml theme={null}
spec:
  podSelector:
    matchLabels:
      role: db
  policyTypes:
    - Ingress
  ingress:
    - from:
      - podSelector:
          matchLabels:
            role: frontend
```

* Permit traffic only from a CIDR range using `ipBlock`:

```yaml theme={null}
spec:
  podSelector:
    matchLabels:
      role: db
  policyTypes:
    - Ingress
  ingress:
    - from:
      - ipBlock:
          cidr: 172.17.0.0/16
```

Reading practice: things to ask for each NetworkPolicy file

* What `policyTypes` are specified? (Ingress, Egress, or both.)
* Which pods are targeted? (Look at `podSelector` and the policy’s `namespace`.)
* For ingress rules: who is allowed to connect? (Pod selectors, namespace selectors, `ipBlock`, etc.)
* When you see `podSelector: {}` or `ingress: []`, what is the effective behavior?

Why two similar-looking files can behave differently

Two files may look similar at the top but have different effects based on their `ingress` or `egress` contents. For example:

* A file targeting pods labeled `role: db` with an `ingress` that contains an `ipBlock` (CIDR) explicitly permits traffic from that IP range.
* A file targeting the same pods but having `ingress: []` and `policyTypes` including `Ingress` defines no allowed ingress peers — effectively denying all incoming traffic to those pods.

Practical rule of thumb

* `ingress: []` together with `policyTypes` that include `Ingress` = explicitly deny all incoming traffic to the selected pods.
* An `ingress` list that contains `from:` clauses = allow only the specified sources.

Inspecting policies on a cluster

You can confirm what’s deployed and see the exact YAML with:

```bash theme={null}
kubectl get networkpolicy -n default
kubectl get networkpolicy deny-all -n default -o yaml
```

Exercises (try these)

* Open the sixth file in Bob’s set that contains `ingress: []`. Who does it allow? (Answer: it allows no incoming peers — it denies all ingress to the selected pods when `Ingress` is included in `policyTypes`.)
* Compare that file to the one that uses `ipBlock`. Which sources are permitted in each?

Further reading

* Kubernetes NetworkPolicy docs: [https://kubernetes.io/docs/concepts/services-networking/network-policies/](https://kubernetes.io/docs/concepts/services-networking/network-policies/)
* Examples and patterns for common NetworkPolicy use cases in the official docs.

Remember: `podSelector: {}` selects all pods in the policy’s namespace. An empty rule list such as `ingress: []` explicitly denies that direction to the selected pods (when `Ingress` is included in `policyTypes`).

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/learn-by-doing-kubernetes-network-policies/module/7b3f394a-e8c3-469c-8029-07a50b0ba6f5/lesson/bcc3e09e-cacd-4f51-a388-443632689651)


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