Difference between NodePort, HostPort and Cluster IP

zarathustra picture zarathustra · Jun 5, 2018 · Viewed 12.8k times · Source

Rancher 2 provides 4 options in the "Ports" section when deploying a new workload:

  • NodePort
  • HostPort
  • Cluster IP
  • Layer-4 Load Balancer

What are the differences? Especially between NodePort, HostPort and Cluster IP?

Answer

Tinky_ picture Tinky_ · Jun 8, 2018

HostPort (nodes running a pod): Similiar to docker, this will open a port on the node on which the pod is running (this allows you to open port 80 on the host). This is pretty easy to setup an run, however:

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 (On every node): Is restricted to ports between port 30,000 to ~33,000. This usually only makes sense in combination with an external loadbalancer (in case you want to publish a web-application on port 80)

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

Cluster IP (Internal only): As the description says, this will open a port only available for internal applications running in the same cluster. A service using this option is accessbile via the internal cluster-ip.