How to restrict egress from AI agent pods with NetworkPolicy

A pod that no NetworkPolicy selects for egress can open any outbound connection. To restrict an AI agent pod, select it with a NetworkPolicy that lists Egress in policyTypes, then allow only the destinations the agent needs. The policy has no effect unless your network plugin enforces NetworkPolicy.
Checked against the Kubernetes NetworkPolicy docs and the Declare Network Policy walkthrough, as of 2026-11-17.
How egress isolation decides what an agent can reach
Egress isolation is set per pod, and by default a pod is not isolated. The NetworkPolicy docs say a pod is non-isolated for egress, so all outbound connections are allowed, unless a NetworkPolicy both selects the pod and has Egress in its policyTypes. Once a pod is isolated, the only connections it can make are those allowed by the egress rules of a policy that applies to it. Those rules combine additively, so adding a policy only adds destinations, and the order you apply them in does not matter.
Reply traffic for an allowed connection is implicitly allowed, so an agent that calls an API does not need a separate rule for the response.
Egress is only half the check. For a connection from one pod to another to succeed, the source pod’s egress policy and the destination pod’s ingress policy both have to allow it. A destination that no ingress policy isolates accepts inbound connections by default.
Traffic to the node is always allowed
The docs carve out one exception that matters for agent hosts. Traffic to and from the node where a pod runs is always allowed, regardless of the pod or node IP address. A policy cannot block an agent from reaching its own node, so do not rely on egress rules to limit node-local access.
Check that your network plugin enforces NetworkPolicy
NetworkPolicy is a declaration, and a network plugin has to act on it. The NetworkPolicy docs say that creating a NetworkPolicy resource without a controller that implements it has no effect. Confirm the plugin before you write any policy for agents.
- Check the cluster version. The walkthrough requires a server at v1.8 or later, and says to run
kubectl versionto check it.
kubectl version
-
Check that your network provider supports NetworkPolicy. The walkthrough lists Antrea, Calico, Cilium and Kube-router as providers that do. If your cluster uses another CNI plugin, confirm its support in that plugin’s own documentation.
-
Check that a plugin is installed on every node. The network plugins page says a CNI plugin is required to implement the Kubernetes network model, and that the container runtime must load it.
Start with a default deny egress policy
A default deny policy is the baseline for an agent namespace. It selects every pod in the namespace and lists Egress with no egress rules. After it applies, every pod in the namespace is isolated for egress, so an outbound connection from any of them is allowed only if another policy’s egress rule permits it. Check the existing egress policies in the namespace before you rely on default deny, because each allow rule still adds its destinations.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-egress
namespace: agents
spec:
podSelector: {}
policyTypes:
- Egress
Apply it with kubectl apply -f, as the walkthrough does for its own policy. The default deny policy does not change ingress behavior for any pod. Keep the namespace name and the policy name as they fit your cluster, since the docs require only that the name be a valid DNS subdomain name.
kubectl apply -f default-deny-egress.yaml
Allow DNS before you test
The docs warn that a default deny egress policy also blocks DNS traffic. Agents that resolve a name before they connect will fail on it. Add a second NetworkPolicy that allows egress to your cluster’s DNS service. The NetworkPolicy docs do not include a manifest for that rule, so take the selector and port from your cluster’s DNS Service and write the rule from them.
Allow each destination with its own rule
Every egress rule has a to list and a ports list, and a connection must match both. The to entries use one of three selectors, and one entry can combine two of them. The table compares them for an agent.
Selector in a to entry |
What it matches | Use it for an agent when |
|---|---|---|
podSelector |
Pods in the policy’s own namespace, by label | The agent calls a tool pod in the same namespace |
namespaceSelector |
All pods in namespaces matched by label | The agent calls a shared service in another namespace |
ipBlock with cidr and optional except |
A CIDR range, meant for cluster-external IPs | The agent calls a service outside the cluster, by IP range |
namespaceSelector and podSelector in one entry |
Specific pods in specific namespaces | You want one service in one namespace, and nothing else there |
Two entries in one to list are a union. One entry that names both selectors is an intersection. The docs warn that the YAML is easy to get wrong, and advise running kubectl describe to see how Kubernetes interpreted a policy.
The example below allows an agent to reach one namespace on one TCP port. It targets the namespace by the standard kubernetes.io/metadata.name label, because the docs say a NetworkPolicy cannot name a namespace directly.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: agent-egress-tools
namespace: agents
spec:
podSelector:
matchLabels:
app: agent
policyTypes:
- Egress
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: tools
ports:
- protocol: TCP
port: 8080
This policy selects pods labeled app: agent in the agents namespace. Those pods are now isolated for egress, so the default deny policy and this rule both apply, and the allowed set is their union. Adding a rule for a second destination means adding another entry to the egress list, not editing this one.
Test allowed and blocked destinations
Test each rule from inside the namespace. The walkthrough runs a busybox pod and calls a service with wget. A request that the policy blocks times out, and an allowed one connects.
- Start a test pod. The walkthrough starts one in the default namespace with the command below. To test an agent namespace, run the same command against that namespace, and give the pod the label your policy selects when you want it to match the agent.
kubectl run busybox --rm -ti --image=busybox -- /bin/sh
- Run the same request against a destination the agent should reach, and one it should not. The walkthrough uses
wget --spider --timeout=1against a service name. To test the TCP 8080 rule above, first start a service on port 8080 in thetoolsnamespace, then request it on that port from the agent namespace:
wget --spider --timeout=1 http://<service-name>.tools:8080
- Read the result. The walkthrough shows the blocked case as
wget: download timed out, and the allowed case asremote file exists. Run the test once with the agent’s label and once without it, so you see both outcomes.
Timing and existing connections
Policies do not take effect at the same moment as a pod starts, and the docs describe a window where a new agent pod can be unprotected. A newly created pod is isolated before its containers start once the plugin has handled the policy. But a pod created before the plugin finishes may start unprotected, and allow rules can land after isolation rules, so the pod may briefly have no connectivity at all.
The docs’ advice is to make pods resilient to this. If an agent must reach a destination before it starts, use an init container that waits until the destination is reachable. Init containers, sidecars and regular containers are all covered by the same pod-level policy.
Changes to a live policy are also implementation defined for existing connections. Whether a new policy closes a connection that was already open depends on the network plugin. The docs also recommend not modifying policies, pods or namespaces in ways that might affect existing connections.
Watch the fine print on protocols and host networking
Several details in the docs change what a rule actually blocks. Write them into your review checklist before you rely on a policy for an agent.
- Omitted
policyTypesalso sets Ingress. Ingress is always set whenpolicyTypesis missing, and Egress is set only if the policy has egress rules. An egress-only policy that omits the field would also isolate the pod for ingress. WritepolicyTypesout in every policy. - Only TCP, UDP and SCTP are guaranteed. A deny-all policy is only guaranteed to block those three protocols. The behavior for ICMP and other protocols is undefined, and it varies by plugin.
- Service IP rewrites may or may not match. When traffic to a Service IP is rewritten to an external IP, it may or may not be subject to ipBlock-based policies.
endPortneeds plugin support. A port range usesendPort, which is stable since Kubernetes v1.25. If the plugin does not support it, the policy applies only to theportfield.- Host-network pods are undefined. Behavior for
hostNetworkpods is undefined. According to the docs, the most common implementation treats their traffic as node traffic. Confirm how your plugin handles them before you rely on a policy for an agent that runs with host networking.
What NetworkPolicy cannot do for agents
The docs list what the NetworkPolicy API does not do. For agent egress, the gaps that matter most are explicit deny rules, logging of blocked connections, TLS-related rules, and targeting Services by name. The page says the API is deny-by-default with only allow rules, so an agent cannot be blocked from one destination while a broader allow rule covers it.
The same list says the API cannot prevent traffic to localhost or to the node. For the gaps, the docs point to workarounds such as a service mesh, an ingress controller, an admission controller, or Layer 7 technologies. Plan for those controls before you call the agent’s egress locked down.
Checklist before you enforce egress for an agent
Use this list before you call an agent namespace egress-restricted. Each step maps to a section above.
- Confirm the cluster is at v1.8 or later, and the network plugin enforces NetworkPolicy.
- Apply the default deny egress policy in the agent namespace.
- Apply a DNS policy so name resolution still works.
- Add one egress rule per destination the agent needs, using the selector that fits the table.
- Check the ingress policy on each destination the agent calls.
- Test one allowed and one blocked destination, with and without the agent’s label.
- Confirm the agent pods do not run with host networking.
Where NetworkPolicy stops
NetworkPolicy controls layer 3 and 4 connections between pods, namespaces and CIDR ranges. It does not decide which tools an agent calls, what it sends, or which hostname it reaches. Those decisions need other controls, such as RBAC for the service account and pod security settings for the container.
For the pod settings, read the Pod Security Admission post. For the threat model that decides which destinations to allow, read the agent threat model post. For the sandbox case, which uses the same default deny pattern, read the sandbox post. For the runtime layer, see Runtime OSS.
Sources
- Kubernetes Network Policies: isolation rules, policy fields, selectors, default policies, lifecycle and limits
- Declare Network Policy: version requirement, supported providers, the test procedure
- Network Plugins: the CNI plugin requirement
Frequently asked questions
Does a default deny egress policy also block DNS?
Yes. The NetworkPolicy docs say a default deny-all egress policy also blocks DNS traffic. Pods that need name resolution then fail until you add a separate policy that allows egress to your cluster's DNS service. Add that policy before you test the agent, or the test will fail for a reason that has nothing to do with the destination.
Do I need a policy on the destination pod as well?
Only if the destination is isolated for ingress. A connection needs the source pod's egress policy and the destination pod's ingress policy to both allow it. A destination that no ingress policy selects accepts inbound connections by default, so the agent's egress rule is the only check. For a service you own, add an ingress rule that matches the agent's labels.
Can a NetworkPolicy allow a hostname such as an API domain?
Not directly. NetworkPolicy works at the IP address and port level, OSI layers 3 and 4, and its ipBlock rule takes CIDR ranges. The docs list TLS-related requirements and targeting services by name as out of scope. They point to service meshes, ingress controllers and other Layer 7 technologies for those cases.
Does a new egress policy cut off connections that are already open?
That is implementation defined. Whether a change applies to an existing connection depends on the network plugin. The docs recommend not modifying policies, pods or namespaces in ways that might affect existing connections, and they give a case where a new policy denies a connection that was previously allowed.