NodePort、HostPort 和集群 IP 的区别

Difference between NodePort, HostPort and Cluster IP

Rancher 2 在部署新工作负载时在 "Ports" 部分提供了 4 个选项:

有什么区别?特别是在 NodePort、HostPort 和 Cluster IP 之间?

HostPort (nodes 运行ning a pod): 类似于docker,这将在pod所在的节点上打开一个端口运行ning(这允许您在主机上打开端口 80)。这很容易设置 运行,但是:

Don’t specify a hostPort for a Pod unless it is absolutely necessary. When you bind a Pod to a hostPort, it limits the number of places the Pod can be scheduled, because each combination must be unique. If you don’t specify the hostIP and protocol explicitly, Kubernetes will use 0.0.0.0 as the default hostIP and TCP as the default protocol. kubernetes.io

NodePort(在每个节点上): 仅限于端口 30,000 到 ~33,000 之间的端口。这通常只与外部负载均衡器结合使用才有意义(如果您想在端口 80 上发布 Web 应用程序)

If you explicitly need to expose a Pod’s port on the node, consider using a NodePort Service before resorting to hostPort. kubernetes.io

集群 IP(仅限内部使用): 正如描述中所说,这将打开一个仅供内部应用程序使用的端口 运行ning 在同一个 集群。使用此选项的服务是通过内部 cluster-ip 访问文件。

Host Port Node Port Cluster IP
When a pod is using a hostPort, a connection to the node’s port is forwarded directly to the pod running on that node With a NodePort service, a connection to the node’s port is forwarded to a randomly selected pod (possibly on another node) Exposes the Service on an internal IP in the cluster. This type makes the Service only reachable from within the cluster.
pods using a hostPort, the node’s port is only bound on nodes that run such pods NodePort services bind the port on all nodes, even on those that don’t run such a pod NA
The hostPort feature is primarily used for exposing system services, which are deployed to every node using DaemonSets NA NA

一般提问

问: 当许多 pods 运行 在同一个节点上使用 NodePort 时会发生什么?

A: 对于NodePort,不管你有一个还是多个节点,端口在每个节点上都可用。