What happened:
In some cases, NPC's establishment of iptables rules lags behind Pod traffic occurrence, which results in network isolation failing to take effect. Maybe this is a knotty problem in consideration of K8s event synchronization mechanism.
How to reproduce it:
- Deploy the following
Network Policy in the K8s cluster:
apiVersion: extensions/v1beta1
kind: NetworkPolicy
metadata:
name: test-np
namespace: diagnosis
spec:
egress:
- to:
- ipBlock:
cidr: 14.215.0.0/16
podSelector: {}
policyTypes:
- Egress
- Deploy the following
Deployment in the K8s cluster:
apiVersion: apps/v1
kind: Deployment
metadata:
name: diagnosis
namespace: diagnosis
spec:
replicas: 2
selector:
matchLabels:
app: diagnosis
template:
metadata:
labels:
app: diagnosis
spec:
nodeName: 10.0.1.30
containers:
- name: diagnosis
command: ["/bin/sh"]
args: ["-c","ping 220.181.38.150"]
image: jimmyzh/diagnosis:v1
- Observe the logs of certain Pods:
kubectl logs diagnosis-66f959fd76-2wpln -n diagnosis | head
PING 220.181.38.150 (220.181.38.150) 56(84) bytes of data.
64 bytes from 220.181.38.150: icmp_seq=1 ttl=53 time=3.66 ms
64 bytes from 220.181.38.150: icmp_seq=2 ttl=53 time=3.57 ms
64 bytes from 220.181.38.150: icmp_seq=3 ttl=53 time=3.58 ms
- Observe the iptables rules:
iptables-save -t filter
-A KUBE-POD-FW-3F2W5JGDV6M5LYIN -m comment --comment "rule for stateful firewall for pod" -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A KUBE-POD-FW-3F2W5JGDV6M5LYIN -m comment --comment "run through nw policy test-np" -j KUBE-NWPLCY-ONWOLJXYLLQ2RYI2
-A KUBE-POD-FW-3F2W5JGDV6M5LYIN -m comment --comment "default rule to REJECT traffic destined for POD name:diagnosis-66f959fd76-2wpln namespace: diagnosis" -j REJECT --reject-with icmp-port-unreachable
- Drop the stateful firewall rule manually:
iptables -t filter -D KUBE-POD-FW-3F2W5JGDV6M5LYIN -m comment --comment "rule for stateful firewall for pod" -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
- Observe the logs of the Pod above:
kubectl logs diagnosis-66f959fd76-2wpln -n diagnosis | tail
64 bytes from 220.181.38.150: icmp_seq=19347 ttl=53 time=3.59 ms
64 bytes from 220.181.38.150: icmp_seq=19348 ttl=53 time=3.58 ms
From 172.22.0.65 icmp_seq=19349 Destination Port Unreachable
From 172.22.0.65 icmp_seq=19350 Destination Port Unreachable
From 172.22.0.65 icmp_seq=19351 Destination Port Unreachable
The result indicates that the Pod traffic occurs before the establishment of iptables rules, and the related stateful firewall rule aggravates the ineffectiveness of isolation.
- Add a 10 second sleep before the ping action in the
Deployment above:
containers:
- name: diagnosis
command: ["/bin/sh"]
args: ["-c","sleep 10;ping 220.181.38.150"]
image: jimmyzh/diagnosis:v1
- Observe the logs of the new Pod:
kubectl logs diagnosis-796c7d859f-cpskr -n diagnosis | head
PING 220.181.38.150 (220.181.38.150) 56(84) bytes of data.
From 172.22.0.65 icmp_seq=1 Destination Port Unreachable
From 172.22.0.65 icmp_seq=2 Destination Port Unreachable
From 172.22.0.65 icmp_seq=3 Destination Port Unreachable
The result turns out to be normal after a sleep action.
What happened:
In some cases, NPC's establishment of iptables rules lags behind Pod traffic occurrence, which results in network isolation failing to take effect. Maybe this is a knotty problem in consideration of K8s event synchronization mechanism.
How to reproduce it:
Network Policyin the K8s cluster:Deploymentin the K8s cluster:The result indicates that the Pod traffic occurs before the establishment of iptables rules, and the related stateful firewall rule aggravates the ineffectiveness of isolation.
Deploymentabove:The result turns out to be normal after a sleep action.