Sub topic 8
Controlling outgoing traffic with egress rules
This chapter assumes you know how ingress rules and policyTypes work, how pod isolation works, and how to read a NetworkPolicy manifest. It also assumes you are comfortable with labels, namespaces, and CIDR notation.
What egress rules do
Ingress rules control traffic entering the selected pods. Egress rules control traffic leaving them. A pod becomes isolated for egress when a policy selects it and lists Egress in policyTypes. After that, only traffic matched by an egress rule can leave the pod.
The structure mirrors ingress, with one change. The peer list is called to instead of from.
Anatomy of an egress rule
Each item under egress can have a to list and a ports list. A connection is allowed when it matches at least one to entry and at least one port entry in the same rule item. If you omit to, the rule matches any destination. If you omit ports, it matches any port.
The to list accepts the same peer types as from:
podSelectorfor pods in the policy's own namespace.namespaceSelectorfor all pods in the matching namespaces.namespaceSelectorandpodSelectortogether in one entry for pods with a label in namespaces with a label.ipBlockfor CIDR ranges, usually addresses outside the cluster.
Example: restricting a backend to an external range
This policy isolates app=backend pods for egress. They can reach only 203.0.113.0/24 on TCP 443, except for the upper half of that range.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-external-https
namespace: demo
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- Egress
egress:
- to:
- ipBlock:
cidr: 203.0.113.0/24
except:
- 203.0.113.128/25
ports:
- protocol: TCP
port: 443The except list removes 203.0.113.128/25 from the allowed range. Its entries must fall inside cidr.
This policy does not allow DNS. The backend pods cannot resolve names, so they can only connect to the IP address directly. The next chapters cover how to fix this.
How the decision is made
Common errors
- Forgetting
EgressinpolicyTypes. Without it, theegresssection is not what isolates the pod. List the type explicitly so your intent is clear. - Blocking DNS. Once a pod is isolated for egress, name lookups to the cluster DNS fail unless a rule allows them.
- Using
ipBlockfor pods. Pod IPs change, so select pods with labels instead. - Expecting replies to need a rule. Reply traffic for an allowed connection is permitted automatically. You only write rules for the direction that opens the connection.
- Forgetting the other side. If the destination pod is isolated for ingress, it also needs an ingress rule that allows this pod. Both sides must permit the connection.
Checking the result
Start a temporary pod with the right labels and try the connection:
kubectl run test --rm -it --image=busybox:1.36 --restart=Never --labels=app=frontend -n demo -- wget -qO- -T 3 http://backend:8080A timeout means a policy dropped the traffic or the name did not resolve. Your network plugin must enforce policies for any of this to take effect.
Exercise
A pod labeled app=backend is selected by a policy with policyTypes: [Egress] and one egress rule that allows TCP 443 to 203.0.113.0/24. The pod runs wget https://api.example.com, where api.example.com resolves inside that range, and the request fails. Why, and what is the smallest fix?
Show answer
The pod is isolated for egress, and no rule allows DNS, so it cannot resolve api.example.com. The smallest fix is an additional egress policy (or rule) allowing UDP and TCP port 53 to the cluster DNS pods, for example pods labeled k8s-app=kube-dns in kube-system.