K8S iptables 与 pod 内容器之一的关系

relationship between K8S iptables and the one of a container inside a pod

我已经在容器中启用了特权模式并添加了规则,

iptables -N udp2rawDwrW_191630ce_C0
iptables -F udp2rawDwrW_191630ce_C0
iptables -I udp2rawDwrW_191630ce_C0 -j DROP
iptables -I INPUT -p tcp -m tcp --dport 4096 -j udp2rawDwrW_191630ce_C0

kt exec放入容器中使用iptables --table filter -L,可以看到添加的规则。

/ # iptables --table filter -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
udp2rawDwrW_191630ce_C0  tcp  --  anywhere             anywhere             tcp dpt:4096

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain udp2rawDwrW_191630ce_C0 (1 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere

而当我登录到容器所在的节点时,运行 sudo iptalbes --table filter -L,我看不到相同的结果。

我在想默认情况下 previleged 被删除,因为容器可能会利用它来更改节点中的 iptables 之类的东西,但它看起来不是那样。

所以我的问题是 "what is the relationship between K8S iptables and the one of a container inside a pod" 和 "why we stop user from modifying the container's iptables without the privileged field"?

如果你想操纵节点的 iptables 那么你肯定需要将 pod 放在主机的网络上(hostNetwork: true 在 pod 的 spec 中)。之后授予容器 NET_ADMINNET_RAW 能力(在 containers[i].securityContext.capabilities.add 中)就足够了。 示例 json 切片:

  "spec": {
    "hostNetwork": true,
    "containers": [{
      "name": "netadmin",
      "securityContext": {"capabilities": { "add": ["NET_ADMIN", "NET_RAW"] } }

我不确定特权模式是否与这些天操纵主机的 iptables 有任何关系。