From point A to point B
Point A: you know how to control who talks to whom and on which port, but not what gets requested inside that connection or which domain gets called. Point B: by the end of this chapter you'll write HTTP policies by method and path, filter DNS queries, allow egress by domain name (FQDN) and understand when you need to intercept TLS and what it costs.
HTTP policies
The policies in chapter 12 decide by IP, port and identity. Sometimes that isn't enough: you want a client to only be able to read from an API, or nobody to reach /admin. For that, a layer 4 rule accepts a rules block with HTTP rules.
Start from the chapter 12 API and a policy that only lets the frontend in on port 80. Add HTTP rules to that port:
# api-l7.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
rules:
http:
- method: GET
path: "/"
- method: GET
path: "/api/.*" # regular expression: everything under /api/
kubectl apply -f api-l7.yaml
kubectl exec web -- curl -s -o /dev/null -w "%{http_code}\n" http://api/ # 200
kubectl exec web -- curl -s -o /dev/null -w "%{http_code}\n" http://api/admin # 403
kubectl exec web -- curl -s -o /dev/null -w "%{http_code}\n" -X POST http://api/ # 403
kubectl exec web -- curl -sD - -o /dev/null http://api/admin | grep -i server # server: envoy
path: "/api" only allows exactly /api. For a whole subtree, use /api/.*. Careful with /api.*: it would also open /api-beta or /apikeys.
You can also filter by host and by headers. Notice the response: at layer 3/4 the packet is dropped and the client waits; at layer 7 the TCP connection is accepted and Envoy answers with a 403.
Envoy in the path, and its cost
Denied requests die in Envoy; allowed ones take one extra hop. Diagram: The House of Routing.
- When a port has layer 7 rules, eBPF hands the connection to the node's Envoy (a DaemonSet, not a sidecar). Envoy completes the TCP handshake, reads the request and decides.
- Denied requests never reach the application: they don't show up in its logs.
- The application sees an internal Cilium IP of the node as the source, not the client's. If you need it, read it from the headers Envoy adds or from Hubble.
Envoy is very fast, but never as fast as eBPF. Measure it in your environment before turning on layer 7 everywhere:
# fortio ships with netshoot: 2000 requests, 25 connections at a time
# Run it with the layer 4 policy (api-from-frontend.yaml) and then with the layer 7 one
kubectl exec web -- fortio load -quiet -qps 0 -c 25 -n 2000 http://api/ 2>&1 | grep "All done"
DNS policies and their limit
Cilium understands DNS too. With a dns rule on port 53, the agent's DNS proxy only lets allowed queries through:
# test-dns.yaml
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: test-egress
spec:
endpointSelector:
matchLabels:
run: test
egress:
- toEndpoints:
- matchLabels:
k8s-app: kube-dns
io.kubernetes.pod.namespace: kube-system
toPorts:
- ports:
- port: "53"
protocol: ANY # UDP and TCP
rules:
dns:
- matchPattern: "*.github.com"
- toEntities: [world]
toPorts:
- ports:
- port: "443"
protocol: TCP
kubectl apply -f test-dns.yaml
kubectl exec test -- curl -sI -m 5 https://api.github.com | head -n1 # answers
kubectl exec test -- curl -sI -m 5 https://example.com | head -n1 # Could not resolve host
But this only blocks resolution. Since port 443 is open to the whole world, the pod can resolve over DNS over HTTPS with a public resolver and connect to any IP. Many content filters only block DNS; don't mistake that for egress control.
FQDN policies
The solution is to combine DNS with identity. With toFQDNs, Cilium learns which IPs DNS returns for a name and only allows connections to those IPs:
# test-fqdn.yaml
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: test-egress
spec:
endpointSelector:
matchLabels:
run: test
egress:
- toFQDNs:
- matchName: api.github.com
- matchPattern: "*.githubusercontent.com"
toPorts:
- ports:
- port: "443"
protocol: TCP
- toEndpoints:
- matchLabels:
k8s-app: kube-dns
io.kubernetes.pod.namespace: kube-system
toPorts:
- ports:
- port: "53"
protocol: ANY
rules:
dns:
- matchPattern: "*" # the proxy has to see every response
kubectl apply -f test-fqdn.yaml
kubectl exec test -- curl -sI -m 5 https://api.github.com | head -n1 # answers
kubectl exec test -- curl -sI -m 5 https://example.com | head -n1 # timeout
kubectl exec test -- curl -sI -m 5 https://1.1.1.1 | head -n1 # timeout: the IP has no FQDN identity
# The cache of learned names
kubectl -n kube-system exec ds/cilium -- cilium-dbg fqdn cache list | head
First DNS reveals the IP; then the connection is allowed by identity. Diagram: The House of Routing.
It's like a CIDR policy that updates itself with every DNS response. And it works with any protocol, not just HTTP, because the final decision is made at layer 3/4.
api.github.com.) in your tests if you see strange lookups: without the dot, the pod tries the cluster's search domains first.The CDN problem
Many sites live behind a CDN, and a single IP can serve hundreds of domains. If you allow site-a.example and it resolves to a CDN IP, that IP can also serve site-b.example. A client can force the connection to that IP while asking for another name:
# Illustrative: same CDN IP, a different name in the Host header / SNI
curl --resolve site-b.example:443:$(dig +short site-a.example | head -n1) https://site-b.example
The FQDN policy allows it, because it decides by IP. To close that gap you have to look at the name that travels inside the connection (the TLS SNI or the Host header), and that's layer 7.
Intercepting TLS
With HTTPS, the path and the headers travel encrypted: Envoy can't apply layer 7 rules without decrypting. TLS interception does exactly that: Envoy terminates the pod's connection with a certificate signed by your internal CA, inspects the request and opens a new TLS connection to the real destination.
You need three pieces:
- An internal CA that the client pod trusts.
- A certificate for the domain, signed by that CA, which Envoy presents to the pod (
terminatingTLS). - A bundle of public CAs so Envoy can verify the real destination (
originatingTLS).
# 1 · Internal lab CA
openssl req -x509 -newkey rsa:2048 -nodes -days 30 \
-keyout lab-ca.key -out lab-ca.crt -subj "/CN=THoR Lab CA"
# 2 · Certificate for api.github.com signed by that CA
openssl req -newkey rsa:2048 -nodes -keyout gh.key -out gh.csr -subj "/CN=api.github.com"
printf "subjectAltName=DNS:api.github.com" > san.ext
openssl x509 -req -in gh.csr -CA lab-ca.crt -CAkey lab-ca.key -CAcreateserial \
-days 30 -out gh.crt -extfile san.ext
kubectl -n kube-system create secret tls gh-intercept --cert=gh.crt --key=gh.key
# 3 · Public CAs to validate the destination (the system ones)
kubectl -n kube-system create secret generic public-cas \
--from-file=ca.crt=/etc/ssl/certs/ca-certificates.crt
# Egress rule in the test pod's policy
- toFQDNs:
- matchName: api.github.com
toPorts:
- ports:
- port: "443"
protocol: TCP
terminatingTLS:
secret:
namespace: kube-system
name: gh-intercept
originatingTLS:
secret:
namespace: kube-system
name: public-cas
rules:
http:
- method: GET
path: "/repos/cilium/.*"
kubectl cp lab-ca.crt test:/tmp/lab-ca.crt
kubectl exec test -- curl -s -o /dev/null -w "%{http_code}\n" \
--cacert /tmp/lab-ca.crt https://api.github.com/repos/cilium/cilium # 200
kubectl exec test -- curl -s -o /dev/null -w "%{http_code}\n" \
--cacert /tmp/lab-ca.crt https://api.github.com/user # 403
By default, Cilium syncs these secrets to the cilium-secrets namespace and delivers them to Envoy over SDS. That way the agents don't need permission to read every secret in the cluster, which would be a huge risk.
- Each domain needs its own certificate, and there's no SNI-based selection between several certificates in the same rule.
- Pods have to trust your internal CA; in production, distribute it with tools like cert-manager and trust-manager.
- You're decrypting third-party traffic: check with your legal and security teams what can be inspected.
Challenge
Answer before you open the solution.
1. Does path: "/api" allow /api/v1/users?
No. The path is an exact regular expression. For a subtree, use /api/.*.
2. Why doesn't a DNS-only policy really control egress?
Because it only blocks resolution. If the port is open, the pod can get the IP some other way and connect.
3. In an FQDN policy, why does the DNS rule use matchPattern: "*"?
So the DNS proxy sees every response and can map the IPs to the allowed names.
4. What does Envoy need to apply HTTP rules over HTTPS?
TLS interception: a certificate from your internal CA for the pod and a bundle of public CAs for the destination.
What you take away
- HTTP rules filter by method, path, host and headers; paths are regular expressions.
- At layer 7 the client gets a 403 from Envoy; at layer 3/4, silence.
- A DNS policy alone doesn't stop egress; FQDN does, because it turns names into identities.
- CDNs share IPs: FQDN can't tell apart domains that share an IP.
- Filtering inside HTTPS requires TLS interception, with its operational cost.
In the next chapter: transparent encryption between nodes with WireGuard.
Official resources
- Policy language: L7 and DNSdocs.cilium.io
- Locking down external access with DNS-based policiesdocs.cilium.io
- Inspecting TLS Encrypted Connectionsdocs.cilium.io
- Envoy documentationenvoyproxy.io
- trust-managercert-manager.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.