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

# NetworkPolicy YAML Walkthrough Hands On

> Guide to reading and reasoning about Kubernetes NetworkPolicy YAML, explaining key fields, examples, and variations for securing pod to pod traffic in a namespace

Learn how to read and reason about Kubernetes NetworkPolicy YAML manifests. This guide walks through a representative NetworkPolicy, explains the key fields, and shows practical variations you’ll encounter when securing pod-to-pod traffic in a namespace.

Reference: [Kubernetes NetworkPolicy docs](https://kubernetes.io/docs/concepts/services-networking/network-policies/)

## Example: basic allow-frontend-to-db policy

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-frontend-to-db
spec:
  podSelector:
    matchLabels:
      role: db
  policyTypes:
    - Ingress
  ingress:
    - from:
      - podSelector:
          matchLabels:
            role: frontend
```

This minimal policy demonstrates the most common fields you’ll see in NetworkPolicy manifests.

## Key fields explained

Pod selector

* `podSelector` determines which pods the policy selects by matching pod labels.
* In the example above, `matchLabels: role: db` means the policy targets pods labeled `role: db` in the same namespace (labels are case-sensitive).

policyTypes

* `policyTypes` declares which traffic directions the policy controls:
  * `Ingress` — controls incoming traffic to the selected pods.
  * `Egress` — controls outgoing traffic from the selected pods.
* The example lists only `Ingress`, so it restricts only incoming traffic to the `role: db` pods; egress remains unaffected.

ingress (rules)

* `ingress` is a list of rules that permit incoming connections to the selected pods. Each rule contains a `from` list of allowed sources.
* Allowed `from` entries include:
  * `podSelector` — select pods by label,
  * `namespaceSelector` — select all pods in namespaces matching the label(s),
  * `ipBlock` — permit traffic from specified CIDR ranges (optionally with `except` ranges).

Interpreting the sample

* The sample reads: "Pods labeled `role: frontend` may reach pods labeled `role: db`." No other sources are permitted by this policy for ingress.

## Empty mappings vs empty lists — critical difference

Use backticks for literal forms: `` `{}` `` (empty mapping) and `` `[]` `` (empty list).

Semantics:

* An empty `podSelector` (`` `{}` ``) selects all pods in the namespace.
* An empty `namespaceSelector` (`` `{}` ``) matches all namespaces.
* An explicitly empty rule list such as `ingress: []` or `egress: []` means "no allowed sources/rules" for that direction — effectively denying that direction for the selected pods (if the policy type includes that direction).

Quick reference table

| Field | Meaning | Example |
| - | - | - |
| Pod selector selects all pods | Empty mapping selects every pod in the namespace | `` `podSelector: {}` `` |
| Allow all namespaces as source | Empty mapping matches all namespaces | `` `namespaceSelector: {}` `` |
| Deny all incoming (Ingress) | Explicit empty list — no allowed ingress sources | `` `ingress: []` `` |
| Deny all outgoing (Egress) | Explicit empty list — no allowed egress targets | `` `egress: []` `` |

## Examples and variations

1. Ingress-only policy that targets `role: db`

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

* Ingress-only: selects `role: db` pods and permits traffic exclusively from pods labeled `role: frontend`.

2. Checking policies on the cluster

* List NetworkPolicy resources:

```bash theme={null}
kubectl get netpol
```

* Inspect a NetworkPolicy in human-readable form:

```bash theme={null}
kubectl describe netpol allow-frontend-to-db
```

* The `kubectl describe` output summarizes the pod selector, policy types, and allowed sources in plain language.

3. Namespace selector trap

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

* `namespaceSelector` matches namespaces by label. This rule permits traffic from any pod in namespaces labeled `project: frontend`, regardless of the pod labels in those namespaces.

4. IP block

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

* `cidr: 172.17.0.0/16` allows any IP address in 172.17.0.0–172.17.255.255.

5. Policy that selects all pods in a namespace

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

* `podSelector: {}` selects every pod in the current namespace. Ingress is then restricted to traffic from pods with `role: frontend`.

6. Explicit deny all ingress to selected pods

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

* `ingress: []` is an explicit empty list: no incoming sources are allowed. Since `Ingress` is included in `policyTypes`, all incoming connections to the selected pods are denied.

7. Namespace `from` matching all namespaces

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

* `namespaceSelector: {}` matches every namespace; this rule permits traffic from all pods in all namespaces to the selected pods.

Comparing subtle differences

* Small syntactic differences can change security posture completely:
  * `ingress: []` — denies all incoming traffic (no allowed sources).
  * `ingress:` with `from: - namespaceSelector: {}` — explicitly allows all namespaces as sources (i.e., effectively allows all incoming traffic).

Deny-all example (both directions)

```yaml theme={null}
spec:
  podSelector: {}
  policyTypes:
    - Ingress
    - Egress
  ingress: []
  egress: []
```

* `podSelector: {}` selects all pods in the namespace.
* Including both `Ingress` and `Egress` in `policyTypes` plus empty lists for each direction results in a true deny-all policy for that namespace: nothing is allowed in or out.

When a pod is selected by at least one NetworkPolicy that covers a specific direction (Ingress or Egress), that direction becomes isolated: only traffic explicitly allowed by the matching rules is permitted. If there are no rules for that direction (for example, `ingress: []`), no traffic for that direction is allowed.

Final notes and best practices

* Always read every field in a NetworkPolicy before applying it. Small differences (an empty selector `` `{}` `` vs a missing selector, or an empty list `[]`) can change behavior from "allow some traffic" to "deny everything".
* When testing, use `kubectl get netpol` and `kubectl describe netpol <name>` to confirm the policy you applied matches your intent.
* Prefer incremental changes in production: apply a restrictive policy in a staging namespace first and test connectivity with representative clients.

Links and references

* [Kubernetes NetworkPolicy documentation](https://kubernetes.io/docs/concepts/services-networking/network-policies/)
* [kubectl reference](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands)

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/learn-by-doing-kubernetes-network-policies/module/7b3f394a-e8c3-469c-8029-07a50b0ba6f5/lesson/bc2050b8-50c4-4142-99c4-39b4f9b13208)

> [card] [Practice Lab](https://learn.kodekloud.com/user/courses/learn-by-doing-kubernetes-network-policies/module/7b3f394a-e8c3-469c-8029-07a50b0ba6f5/lesson/989695e4-e5ad-4dc0-a460-e4287cb017d5)


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