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

# Controlling Egress The Problem

> Explains Kubernetes NetworkPolicy egress controls, showing syntax, examples, DNS allowances, deny-all patterns, namespace rules and common YAML pitfalls for restricting outbound pod traffic

So far Bob has written rules that answer one question: who may come in? Those were ingress rules, protecting pods from incoming connections. But that’s only half the story.

What stops a pod from sending traffic wherever it wants? Right now, nothing.

If an attacker compromises a pod — for example, the web pod from earlier — they can open outbound connections to internet servers and exfiltrate data. Ingress rules won’t stop that because ingress only controls traffic coming to Bob’s pods. The outbound direction has its own name: egress. Egress is simply traffic leaving a pod — the opposite of ingress.

![A diagram titled "Egress – Traffic Leaving a Pod" showing a pod (labeled pod-b in the default namespace) sending outbound traffic to an Internet server. The graphic also shows a user "Bob" and an in/out policy checkbox inside the pod boundary.](https://mintcdn.com/kodekloud-c4ac6d9a/UCHQITKCSrYPqxtk/images/Learn-By-Doing-Kubernetes-Network-Policies/Egress/Controlling-Egress-The-Problem/egress-pod-b-outbound-internet-policy.jpg?fit=max\&auto=format\&n=UCHQITKCSrYPqxtk\&q=85\&s=c5ffc33b6168e64a6d3e57f49559f52b)

An egress policy decides where a pod may send traffic. Think of it as a second fence that prevents data from leaking out accidentally.

Summary: how egress policies compare to ingress policies

| Aspect | Ingress | Egress |
| - | - | - |
| Direction controlled | Incoming traffic to a pod | Outgoing traffic from a pod |
| Spec field for rules | `ingress` | `egress` |
| Policy type to declare | `Ingress` in `policyTypes` | `Egress` in `policyTypes` |
| Same selectors | Yes — `podSelector`, `namespaceSelector` work the same | Yes — `podSelector`, `namespaceSelector` work the same |

The syntax is almost identical to the ingress policies Bob already knows:

* In `policyTypes` list `Egress` (instead of `Ingress`).
* Put rules under an `egress` array with `to:` destinations (instead of `from:`).
* Use the same selectors (`podSelector`, `namespaceSelector`) inside the rules.
* Only the direction changes.

Example: an ingress rule that lets frontend pods accept connections from middleware pods

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

The equivalent egress rule that allows frontend pods to send traffic to middleware pods

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: frontend-to-middleware
spec:
  podSelector:
    matchLabels:
      app: frontend
  policyTypes:
  - Egress
  egress:
  - to:
    - podSelector:
        matchLabels:
          app: middleware
```

## A real example Bob finds in default namespace

Bob opens a policy file called `deny-egress-to-ns`. Its intent is to restrict egress to the `gamma` namespace.

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

Which namespace does this allow traffic to? The `namespaceSelector` matches `kubernetes.io/metadata.name: gamma`, so it allows destinations in the `gamma` namespace.

When Bob applies the file, Kubernetes warns him there might be errors. One common YAML mistake to watch for is capitalization of field names.

YAML and Kubernetes field names are case-sensitive. The `egress` key must be lowercase. Using `Egress:` (capitalized) under `spec` is invalid and will cause errors.

This policy is also too broad: it allows egress to every pod in `gamma`. To restrict it, add a `podSelector` inside the same `to` element to target only pods labeled `role: access-admin` in `gamma`:

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: deny-egress-to-ns-narrow
spec:
  podSelector: {}
  policyTypes:
  - Egress
  egress:
  - to:
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: gamma
      podSelector:
        matchLabels:
          role: access-admin
```

## DNS: a common egress requirement

Pods discover services using DNS, which typically answers on port 53 (UDP and TCP). If you restrict egress, you often still want to allow DNS queries.

An example policy that explicitly permits egress to port 53 (both UDP and TCP) to any destination:

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: egress-on-53
spec:
  podSelector: {}
  policyTypes:
  - Egress
  egress:
  - to:
    - ipBlock:
        cidr: 0.0.0.0/0
    ports:
    - port: 53
      protocol: UDP
    - port: 53
      protocol: TCP
```

One more important behavior to remember: with no NetworkPolicies selecting a pod, that pod’s traffic is unaffected (default-allow). Only when a NetworkPolicy selects a pod and declares restrictions does the pod become subject to those rules.

## Konoha Corp: multiple namespaces and desired egress behaviors

A new customer, Konoha Corp, runs microservices across multiple namespaces:

* Ramen runs in `genin`
* Chakra runs in `jonin`
* Hokage (admin) runs in `default`

Shikamaru (the admin) needs three egress policies:

* `jonin-to-genin`: let `jonin` namespace send traffic only to Ramen pods in `genin`.
* `genin-no-egress`: block all egress from `genin`.
* `hokage-allow`: let `default` (Hokage) reach both Ramen and Chakra.

![A diagram titled "Two More Policies" showing three dotted boxes labeled genin, jonin, and default, each containing a small smiling blue pod (ramen, chakra, hokage). A dashed arrow shows traffic between the genin and jonin pods, with policy badges ("jonin-to-genin" and "genin-no-egress") and user icons (Bob, Shikamaru) beneath.](https://mintcdn.com/kodekloud-c4ac6d9a/UCHQITKCSrYPqxtk/images/Learn-By-Doing-Kubernetes-Network-Policies/Egress/Controlling-Egress-The-Problem/genin-jonin-policy-diagram.jpg?fit=max\&auto=format\&n=UCHQITKCSrYPqxtk\&q=85\&s=5f61ac2bfa2fab33fa5fd8502bf59511)

One final nuance: `genin-no-egress` needs one exception — allow DNS on port 53 (UDP and TCP). How do you express that?

* To block all egress from a namespace: create a NetworkPolicy that selects the namespace’s pods and declares `policyTypes: [Egress]` with an empty `egress` list. A policy that selects pods and declares `Egress` but provides no egress rules denies all egress for those pods.
* To allow DNS as an exception: create a separate policy in `genin` that permits egress to port 53 (both protocols). NetworkPolicy behavior is additive — the union of allowed traffic from all matching policies is permitted.

Example deny-all egress in `genin` (silence everything):

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: genin-no-egress
spec:
  podSelector: {}
  policyTypes:
  - Egress
  egress: []
```

Example allow-DNS exception in `genin`:

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: genin-allow-dns
spec:
  podSelector: {}
  policyTypes:
  - Egress
  egress:
  - to:
    - ipBlock:
        cidr: 0.0.0.0/0
    ports:
    - port: 53
      protocol: UDP
    - port: 53
      protocol: TCP
```

Where was the original mistake? The original file used `Egress:` (capitalized) instead of the correct lowercase `egress`. And how do you let a namespace go silent except for DNS? Combine a deny-all egress policy (`egress: []`) with an explicit allow policy for DNS — the allowed traffic will be the union of both policies.

Quick best-practices:

* Start by denying broad egress for sensitive namespaces, then add specific allow rules (least privilege).
* Keep DNS and essential control-plane access explicit so pods can resolve and reach cluster services.
* Validate YAML keys and capitalization (`egress` vs `Egress`) before applying manifests.

## Useful references

* Kubernetes NetworkPolicy docs: [https://kubernetes.io/docs/concepts/services-networking/network-policies/](https://kubernetes.io/docs/concepts/services-networking/network-policies/)
* Examples and common patterns for egress control: search for "Kubernetes egress NetworkPolicy examples"

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/learn-by-doing-kubernetes-network-policies/module/66346eea-5872-4503-9fbc-8ba7e0b7ac5b/lesson/3370868e-2807-4e19-b294-a30481241411)


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