kubernets nodeport 无法访问

本文探讨了Kubernetes集群中使用NodePort类型Service时遇到的问题。当Service配置为NodePort类型并尝试从集群外访问时,仅能通过对应Pod所在的节点访问成功,其他节点访问失败。文章分析了问题原因,即Docker更新后iptables FORWARD链默认策略被设置为DROP,导致跨节点转发失败,并提供了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Environment

Os: centos7.1
Kubelet: 1.6.7
Docker: 17.06-ce
Calico: 2.3

K8s Cluster: master, node-1, node-2

Problem

现有 service A, 为了能使外部访问,故将 service type 设为NodePort。端口为 31246
A 所对应的 pod 运行在 node-1 上。

经过测试发现,外部访问 master:31246node-2:31246 时均出现失败,只有通过 node-1:31246 才可正常访问。

Reason

为了安全起见, docker 在 1.13 版本之后,将系统iptables 中 FORWARD 链的默认策略设置为 DROP,并为连接到 docker0 网桥的容器添加了放行规则。这里引用 moby issue#14041 中的描述:

When docker starts, it enables net.ipv4.ip_forward without changing the iptables FORWARD chain default policy to DROP. This means that another machine on the same network as the docker host can add a route to their routing table, and directly address any containers running on that docker host.

For example, if the docker0 subnet is 172.17.0.0/16 (the default subnet), and the docker host’s IP address is 192.168.0.10, from another host on the network run:

   $ ip route add 172.17.0.0/16 via 192.168.0.10
$ nmap 172.17.0.0/16

The above will scan for containers running on the host, and report IP addresses & running services found.

To fix this, docker needs to set the FORWARD policy to DROP when it enables the net.ipv4.ip_forward sysctl parameter.

kubernetes 使用的 cni 插件会因此受影响(cni并不会在 FORWARD 链中生成相应规则),由此导致除 pod 所在 host 以外节点无法转发报文而访问失败。

Solution

如果对安全要求较低,可将 FORWARD 链的默认规则设为 ACCEPT

iptables -P FORWARD ACCEPT

其它方法可参考:

kubernetes issues#40182

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值