从另一台机器通过 kube-proxy 访问 nodeport

Access nodeport via kube-proxy from another machine

我有 kubernetes 集群 (node01-03)。 有一个带有 nodeport 的服务可以访问 pod(nodeport 31000)。 pod 在 node03 上 运行。 即使在端口 31000 的 node01 上有一个侦听器(kube-proxy),我也可以使用 http://node03:31000 from any host. On every node I can access the service like http://[name_of_the_node]:31000. But I cannot access the service the following way: http://node01:31000 访问该服务。iptables 规则对我来说没问题。这就是它的工作方式吗?如果没有,我该如何进一步排除故障?

如果在 Kubernetes 集群中访问 pods,则不需要使用节点端口。改为推断 Kubernetes 服务目标端口。假设 podA 需要通过名为 serviceB 的服务访问 podB。假设 http 是 http://serviceB:targetPort/

NodePort 暴露在集群中的 每个 节点上。 https://kubernetes.io/docs/concepts/services-networking/service/#type-nodeport 清楚地说:

each Node will proxy that port (the same port number on every Node) into your Service

因此,无论是在集群内部还是外部,都可以在集群中的 任何 节点上使用 NodeIP:NodePort 访问该服务,并且 kube-proxy 将使用 iptables 进行路由到具有后端 pod 的正确节点。

但是,如果从集群外部使用 NodeIP:NodePort 访问服务,我们需要首先确保从我们访问 NodeIP:NodePort 的地方可以访问 NodeIP

如果在不是运行后端pod的节点上无法访问NodeIP:NodePort,可能是默认的DROP FORWARD 链上的规则(出于安全原因,这又是由 Docker 1.13 引起的)。 Here is more info about it. Also see step 8 here。一个解决方案是在节点上添加以下规则:

iptables -A FORWARD -j ACCEPT

k8s 的问题是 here and the fix is here(修复应该在 k8s 1.9 中)。

启用对服务的外部访问的其他三个选项是:

  1. ExternalIPs: https://kubernetes.io/docs/concepts/services-networking/service/#external-ips
  2. LoadBalancer 使用外部云提供商的负载均衡器:https://kubernetes.io/docs/concepts/services-networking/service/#type-loadbalancer
  3. Ingress: https://kubernetes.io/docs/concepts/services-networking/ingress/