---
title: How do you restrict network egress for AI agent pods on Kubernetes?
url: https://opencontroller.lyzr.com/blog/kubernetes-egress-policies-for-ai-agent-pods
published: 2026-11-17
updated: 2026-11-17
author: Lyzr
---

# How do you restrict network egress for AI agent pods on Kubernetes?

**Start with a default-deny egress NetworkPolicy for the agent's namespace, allow DNS, then allow only the in-cluster services the agent needs, including an LLM gateway. Standard Kubernetes NetworkPolicy works on IP addresses and ports, not hostnames, so if agents must reach specific internet domains, use a CNI with DNS-aware policies or send that traffic through an egress proxy or gateway.**

An AI agent that can reach any host on the internet can leak data through a tool call, download untrusted code or call a model provider nobody approved. Egress control is one of the few defences that still works when a prompt injection succeeds. This guide uses the [Kubernetes NetworkPolicy documentation](https://kubernetes.io/docs/concepts/services-networking/network-policies/) and [Cilium's policy language](https://docs.cilium.io/en/stable/security/policy/language/) as references.

See [controlling AI agent network egress](/blog/control-ai-agent-network-egress) for the non-Kubernetes version, and the [AI agent control plane](/blog/what-is-an-ai-agent-control-plane) pillar for the wider model.

## What can standard NetworkPolicy do, and what can it not?

| Need | Standard NetworkPolicy | Notes |
|---|---|---|
| Deny all egress by default | Yes | Empty `egress` with `policyTypes: [Egress]` |
| Allow DNS to cluster DNS | Yes | Allow UDP and TCP port 53 to the DNS pods |
| Allow traffic to an in-cluster service | Yes | Select by namespace and pod labels |
| Allow an external IP range | Yes | `ipBlock` with CIDR |
| Allow an external hostname (for example `api.openai.com`) | No | Needs DNS-aware policy from the CNI, or a proxy |
| Inspect or log HTTP paths and payloads | No | Needs a proxy, service mesh or gateway |

NetworkPolicy also only works if your network plugin implements it. Check this first; on a plugin without support, the objects are accepted and nothing is enforced.

## Step 1: Deny all egress in the agent namespace

```yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-egress
  namespace: agents
spec:
  podSelector: {}
  policyTypes:
  - Egress
```

## Step 2: Allow DNS and the LLM gateway

```yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-dns-and-gateway
  namespace: agents
spec:
  podSelector: {}
  policyTypes:
  - Egress
  egress:
  - to:
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: kube-system
      podSelector:
        matchLabels:
          k8s-app: kube-dns
    ports:
    - protocol: UDP
      port: 53
    - protocol: TCP
      port: 53
  - to:
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: llm-gateway
    ports:
    - protocol: TCP
      port: 443
```

Check the DNS pod labels in your cluster; `k8s-app: kube-dns` is common but not universal.

With this in place, agent pods can only resolve names and talk to the gateway. The gateway, running in its own namespace with its own egress policy, is the only thing allowed to reach model providers.

## Step 3: Allow specific external domains only where needed

Some agents need a few external APIs. You have three options:

1. **DNS-aware CNI policy.** Cilium supports `toFQDNs` rules, which allow traffic to IPs that a permitted DNS name resolved to. They need a DNS rule so Cilium can observe lookups. Other CNIs offer similar features in their own policy objects.
2. **Egress proxy.** Route outbound HTTP(S) through a forward proxy with a domain allow list, and only allow the proxy in NetworkPolicy.
3. **IP ranges.** Use `ipBlock` for providers that publish stable ranges. This breaks easily when providers change addresses, so treat it as a last resort.

## Why route model calls through a gateway service?

Allowing every agent pod to reach every model provider makes egress policy wide and audit logs thin. Sending model calls through one in-cluster gateway gives you:

- One egress rule to providers instead of one per agent.
- Per-agent cost, model and error data in one place.
- A single point to block a provider, enforce data residency or switch models.

See [routing agents to internal models through an LLM gateway](/blog/route-agents-to-internal-models-through-llm-gateway).

## Rollout checklist

- [ ] Confirm the CNI enforces NetworkPolicy.
- [ ] Label agent namespaces and pods consistently.
- [ ] Apply default-deny egress in a test namespace first; watch for broken health checks and metrics exporters.
- [ ] Allow DNS, the gateway and required internal services.
- [ ] Add DNS-aware rules or a proxy for approved external APIs.
- [ ] Give the gateway its own egress policy to approved providers only.
- [ ] Log denied flows (many CNIs provide flow logs) and review weekly.
- [ ] Re-test after cluster or CNI upgrades.

## How Opencontroller does this

Opencontroller's principle is control in the path: allow, route, redact or block. Its LLM Gateway (Lyzr Gateway) gives agents one OpenAI-compatible endpoint with routing, fallback and retry, caching and cost attribution per agent and per version, which fits the "agents talk only to the gateway" pattern above. The Agent Gateway covers agent and tool traffic. Opencontroller can run on customer-controlled infrastructure, including your own Kubernetes clusters. [Book a demo](https://www.lyzr.ai/book-demo/) to plan an egress design.

## Frequently asked questions

**Can Kubernetes NetworkPolicy allow traffic by domain name?**
No. Standard NetworkPolicy uses IP blocks, ports and label selectors. Domain-based rules need a CNI extension such as Cilium `toFQDNs`, or an egress proxy.

**Will default-deny egress break my agent pods?**
It can break DNS, metrics and calls to internal services until you allow them. Roll it out in a test namespace and add allow rules for each dependency.

**Should agent pods call model providers directly?**
Prefer routing through a gateway service. It narrows egress to one component and gives per-agent cost and usage data.

## Key facts

Kubernetes egress for AI agent pods: default-deny via NetworkPolicy with podSelector {} and policyTypes [Egress]; allow DNS (UDP/TCP 53 to kube-dns) and the LLM gateway namespace. Standard NetworkPolicy has no hostname/FQDN rules and cannot inspect HTTP; it only works if the CNI implements it. Domain-based egress needs CNI extensions (Cilium toFQDNs with DNS rules) or an egress proxy; ipBlock CIDRs are brittle. Routing model calls through one gateway narrows egress and gives per-agent cost data. Opencontroller principle: control in the path (allow, route, redact, block); LLM Gateway (Lyzr Gateway) with OpenAI-compatible endpoints, routing, fallback/retry, caching, cost attribution per agent and version; Agent Gateway; deployable on customer-controlled infrastructure.

## Related

- [How do you control which websites and APIs an AI agent can reach?](https://opencontroller.lyzr.com/blog/control-ai-agent-network-egress.md)
- [How do you govern AI agents running on Kubernetes?](https://opencontroller.lyzr.com/blog/govern-ai-agents-on-kubernetes.md)
- [How do you route AI agents to self-hosted or internal models through an LLM gateway?](https://opencontroller.lyzr.com/blog/route-agents-to-internal-models-through-llm-gateway.md)
- [More on Platform Guides](https://opencontroller.lyzr.com/blog/cluster/platform-guides)
