【问题标题】:relationship between K8S iptables and the one of a container inside a podK8S iptables 与 pod 内的容器之间的关系
【发布时间】:2020-02-15 19:08:00
【问题描述】:

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

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 之类的东西,但它看起来不像那样。

所以我的问题是“K8S iptables 与 pod 内的容器之间的关系是什么”以及“为什么我们阻止用户在没有 privileged 字段的情况下修改容器的 iptables”?

【问题讨论】:

  • 嗨,我认为这是因为对节点 iptables 所做的修改在其动态修改中丢失了。看看kubernetes documentation中的这部分。
  • 请解释“在容器中启用特权模式”:您使用了什么命令/yaml?也是普通的 docker 容器还是 k8s pod?
  • @morgwai,这里记录了特权模式:kubernetes.io/docs/concepts/policy/pod-security-policy。它是 k8s pod 中的容器。
  • @PiotrMales,我认为 Node 的 iptables 实际上并没有被容器修改。查了节点的iptables规则,感觉有很多规则和流量相关,但没有体现在容器中。

标签: docker kubernetes iptables


【解决方案1】:

如果您想操作节点的 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 有关。

【讨论】:

  • 谢谢,我想如果有启用主机网络的选项,那么 pod 上的默认命名空间应该不同于主机的命名空间。
  • @DamonYuan 你的意思是“iptables 命名空间”对吧? (不确定“命名空间”是否是 iptables 的正确术语)所以这似乎基于您的经验,但老实说我以前不知道这一点:我认为在没有 hostNetwork: true 的情况下操作 iptables 的尝试基本上会导致有一些错误。
  • 不,它不会有任何错误 - 容器的 iptalbes 已更改,而主机保持原样。
猜你喜欢
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-16
相关资源
最近更新 更多