K8S iptables 与 pod 内容器的关系

2023-12-20

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

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

and 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 等内容,但看起来并非如此。

所以我的问题是“K8S iptables 和 pod 内的容器之间的关系是什么”以及“为什么我们阻止用户修改容器的 iptables 而无需privileged field"?


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

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

我不确定特权模式是否与现在操纵主机的 iptables 有关。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

K8S iptables 与 pod 内容器的关系 的相关文章

随机推荐