From point A to point B
Point A: you wrote a policy in chapter 3, but without fully knowing why it worked. Point B: by the end of this chapter you'll understand how Cilium decides on every packet, you'll write ingress and egress policies by identity, by entity and by CIDR, and you'll avoid the mistakes that open holes without you noticing.
Your first port policy
Network policies replace traditional firewalls inside the cluster, but they speak Kubernetes' language: namespaces, labels and services. To practice, deploy an API with two ports: 80 for normal traffic and 9090 for metrics.
# api.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
spec:
replicas: 2
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
containers:
- name: web
image: nginx:stable # port 80
- name: metrics
image: hashicorp/http-echo
args: ["-listen=:9090", "-text=api_requests_total 42"]
---
apiVersion: v1
kind: Service
metadata:
name: api
spec:
selector:
app: api
ports:
- name: http
port: 80
- name: metrics
port: 9090
kubectl apply -f api.yaml
kubectl run test --image=nicolaka/netshoot -- sleep infinity
kubectl exec test -- curl -s -o /dev/null -w "%{http_code}\n" http://api # 200
kubectl exec test -- curl -s http://api:9090 # api_requests_total 42
This policy only lets TCP in on port 80:
# api-80-only.yaml
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy # namespaced: only selects pods in its namespace
metadata:
name: api
spec:
endpointSelector: # which pods it applies to
matchLabels:
app: api
ingress: # list of ingress rules
- toPorts:
- ports:
- port: "80" # always in quotes; names work too
protocol: TCP
kubectl apply -f api-80-only.yaml
kubectl exec test -- curl -s -o /dev/null -w "%{http_code}\n" http://api # 200
kubectl exec test -- curl -s -m 2 http://api:9090
# timeout: the SYN is dropped in eBPF and nobody answers
Every pod Cilium manages is an endpoint (kubectl get ciliumendpoints). The policy is enforced right as traffic leaves the pod and right before it enters it. That's why it doesn't apply to pods with hostNetwork: true, or between processes in the same pod.
Default deny, and when it doesn't apply
The rule you already know from chapter 3: an endpoint that no policy selects allows everything. As soon as a policy selects it with at least one rule in one direction, that direction switches to default deny. That's why port 9090 stopped working: nobody allowed it.
The opposite direction doesn't change. Your policy only has ingress rules, so the API can still go out wherever it wants:
# An ephemeral container inside the API pod, with network tools
POD=$(kubectl get pod -l app=api -o jsonpath='{.items[0].metadata.name}')
kubectl debug -it $POD --image=nicolaka/netshoot --profile=general -- \
curl -sI https://cilium.io | head -n1 # HTTP/2 200
To block all egress, add an empty egress rule. A rule exists, so default deny kicks in; but that rule matches nothing:
# api-no-egress.yaml
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: api-no-egress
spec:
endpointSelector:
matchLabels:
app: api
egress:
- {}
Now the API can't start connections, but it still answers the ones you allow: replies to accepted traffic always get through. It doesn't matter whether the rules live in one policy or several; the engine combines them.
Sometimes you want a policy to add permissions without blocking everything else. That's what enableDefaultDeny is for, per direction:
spec:
enableDefaultDeny:
ingress: false # this policy doesn't turn on default deny
egress: false
Careful: if any other policy that selects the endpoint does turn it on, it applies.
Identities: the core idea
There are two classic ways to know who's who on a network: by IP (layer 3, fast but fragile when IPs change or get reshuffled) or by credentials inside the protocol (layer 7, such as tokens or mTLS: stronger, but it depends on each application). Cilium takes the best of both: it decides at layer 3, but uses what Kubernetes knows about each pod to translate IPs into identities.
Each unique set of security labels has an identity, a number. All the replicas of a deployment share an identity. Policies never talk about numbers or IPs: they select identities by their labels.
Two lookups per packet: IP to identity, and identity to verdict. Diagram: The House of Routing.
# Identities in the default namespace (the resource isn't namespaced: filter by label)
kubectl get ciliumidentities -l io.kubernetes.pod.namespace=default
# A node's IP → identity map
kubectl -n kube-system exec ds/cilium -- cilium-dbg bpf ipcache list | head
- ipcache: a longest-prefix-match (LPM) map.
10.0.2.17/32beats0.0.0.0/0. The lookup doesn't walk every entry: it depends on the address size, not on the number of rules. - Endpoint policy map: precomputed by the agent. For each identity, direction, protocol and port, it stores the decision: allow, drop or send to a layer 7 proxy.
- Between nodes, the identity travels in the tunnel (chapter 5), so the destination doesn't even need to check the ipcache.
By default almost any pod label becomes part of its identity. If a system puts a unique label on every pod (a job ID, for example), every pod creates a new identity, and every agent in the cluster has to recompute. In large clusters, limit the relevant labels with the Helm value labels (with a leading ! to exclude).
Selecting by labels
Now port 80 only for the frontend. Two statements in the same rule: fromEndpoints and toPorts.
# api-from-frontend.yaml
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: api
spec:
endpointSelector:
matchLabels:
app: api
ingress:
- fromEndpoints:
- matchLabels:
role: frontend
toPorts:
- ports:
- port: "80"
protocol: TCP
- fromEndpoints: # second rule: the metrics collector
- matchLabels:
io.kubernetes.pod.namespace: monitoring
app: prometheus
toPorts:
- ports:
- port: "9090"
protocol: TCP
kubectl apply -f api-from-frontend.yaml
kubectl exec test -- curl -s -m 2 http://api # timeout: test isn't frontend
kubectl run web --labels role=frontend --image=nicolaka/netshoot -- sleep infinity
kubectl exec web -- curl -s -o /dev/null -w "%{http_code}\n" http://api # 200
kubectl exec web -- curl -s -m 2 http://api:9090 # timeout
hubble observe -P --to-pod default/api -t policy-verdict --last 10
io.kubernetes.pod.namespace is a special label Cilium sets with the pod's namespace. You can't fake it by putting it on a pod yourself: Cilium ignores labels that match the special ones.
Rules are OR, statements are AND
This is the most important rule in the chapter, and the one that opens the most holes:
- The rules in a list are combined with OR: matching one is enough.
- The statements inside a rule are combined with AND: all of them must hold.
One extra dash turns a strict rule into two permissive ones. Diagram: The House of Routing.
On the left, only the frontend and only to port 80. On the right, the frontend to any port, and anyone, even from outside the cluster, to port 80. Always check the dashes in your YAML lists.
Another trap: not every statement can be combined in the same rule. For example, toEndpoints and toEntities together aren't valid. Cilium doesn't reject the policy: it marks it as invalid and applies none of its rules.
kubectl get ciliumnetworkpolicies # VALID column
kubectl describe cnp <name> | grep -A3 Conditions
The fix is to split them into two rules. That's also why it sometimes pays to spread rules across several policies: if one is invalid, the others keep working.
Cluster-wide policies and entities
A CiliumNetworkPolicy only selects endpoints in its namespace. For cross-cutting rules, like letting Prometheus read metrics from any pod, use a CiliumClusterwideNetworkPolicy:
apiVersion: cilium.io/v2
kind: CiliumClusterwideNetworkPolicy
metadata:
name: allow-metrics-scraping
spec:
endpointSelector: {} # every endpoint in the cluster
enableDefaultDeny:
ingress: false # essential: otherwise you'd block the whole cluster
ingress:
- fromEndpoints:
- matchLabels:
io.kubernetes.pod.namespace: monitoring
app: prometheus
toPorts:
- ports:
- port: "9090"
protocol: TCP
Cilium also has reserved identities (with reserved: labels) and entities, shortcuts to select them with fromEntities and toEntities:
| Entity | What it selects |
|---|---|
world | Everything outside the cluster (IPv4 and IPv6) |
cluster | Everything in the cluster: pods, nodes, health, ingress… |
host | The local node |
remote-node | The other nodes in the cluster |
kube-apiserver | The Kubernetes API server |
ingress | Traffic arriving through Cilium's Ingress or Gateway API |
all | Wildcard: everything |
With entities you can describe CoreDNS's expected traffic: receive DNS from the whole cluster, talk to the API server and query external DNS servers.
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: coredns
namespace: kube-system
spec:
endpointSelector:
matchLabels:
k8s-app: kube-dns
ingress:
- fromEntities: [cluster]
toPorts:
- ports:
- port: "53"
protocol: ANY
egress:
- toEntities: [kube-apiserver]
toPorts:
- ports:
- port: "6443"
protocol: TCP
- toEntities: [world]
toPorts:
- ports:
- port: "53"
protocol: ANY
Beyond the cluster
For specific external destinations, use toCIDR/fromCIDR or toCIDRSet, which supports exceptions. Each CIDR in a policy creates an identity and is added to the ipcache. A CIDR can't select something that already has an identity, like a pod or a node in the cluster.
# test-egress.yaml: the test pod can only go out to 1.1.1.1
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: test-egress
spec:
endpointSelector:
matchLabels:
run: test
egress:
- toCIDR:
- 1.1.1.1/32
kubectl apply -f test-egress.yaml
kubectl exec test -- curl -sI -m 3 https://1.1.1.1 | head -n1 # answers
kubectl exec test -- curl -sI -m 3 https://1.0.0.1 | head -n1 # timeout
Notice there's no DNS rule: that's why the test uses IPs. When you need to allow domain names instead of IPs, you'll use the FQDN policies from chapter 13.
ingress identity. Pods serving behind a Gateway must allow fromEntities: [ingress].Deny rules
Besides allowing, you can explicitly forbid with ingressDeny and egressDeny. Cilium policies have no order, so the question is what happens if one rule allows and another denies. The answer is simple: deny always wins, wherever it comes from.
That makes them ideal for security baselines no team can bypass. For example, no pod outside kube-system talks to the API server:
apiVersion: cilium.io/v2
kind: CiliumClusterwideNetworkPolicy
metadata:
name: deny-kube-api
spec:
endpointSelector:
matchExpressions:
- key: io.kubernetes.pod.namespace
operator: NotIn
values: [kube-system]
enableDefaultDeny:
egress: false # deny rules also turn on default deny
egressDeny:
- toEntities:
- kube-apiserver
The trade-off: a deny is a blunt instrument. You can't make exceptions without changing the deny policy itself.
Challenge
Answer before you open the solution.
1. A policy only has ingress rules. What happens to the pod's egress?
It's still allowed. Default deny is turned on per direction, only where there are rules.
2. What does the ipcache store, and what does the policy map store?
The ipcache translates IP to identity; the endpoint policy map translates identity, direction, protocol and port into a verdict.
3. fromEndpoints and toPorts in the same rule: are they combined with AND or OR?
With AND. If a dash splits them into two rules, they become combined with OR.
4. One policy allows a flow and another denies it. What happens?
It's denied. In Cilium, deny always takes priority.
What you take away
- With no policies everything passes; one rule in a direction turns on default deny in that direction.
- Cilium decides at layer 3, but by identities derived from labels, not by IPs.
- Rules are OR, statements are AND: one extra dash can open a hole.
- An invalid policy applies none of its rules: check the VALID column.
- Entities, CIDRs and deny rules complete the picture; deny always wins.
In the next chapter you level up: layer 7 and domain name (FQDN) policies.
Official resources
- Network Policydocs.cilium.io
- Policy languagedocs.cilium.io
- Deny Policiesdocs.cilium.io
- Identity-relevant labelsdocs.cilium.io
- Network Policy Editornetworkpolicy.io
- Cilium: Up and Running (book)isovalent.com
Source and credits. This chapter summarizes and paraphrases the official book Cilium: Up and Running by Nico Vibert, Filip Nikolic and James Laverack (O'Reilly Media, 2026). The summary was produced with AI and reviewed, verified and approved by Jesús Espinoza. The diagrams and code examples are original work by The House of Routing and were checked against the official Cilium documentation. Cilium: Up and Running, the cover image, and related trade dress are trademarks of O'Reilly Media, Inc. This content is not affiliated with or endorsed by O'Reilly Media or Isovalent.