确实是理解有误,很多人分不清 port,targetPort 和 nodePort 。参考这篇文章的解释就很容易理解了。
https://matthewpalmer.net/kubernetes-app-developer/articles/kubernetes-ports-targetport-nodeport-service.htmlnodePort
This setting makes the service visible outside the Kubernetes cluster by the node’s IP address and the port number declared in this property. The service also has to be of type NodePort (if this field isn’t specified, Kubernetes will allocate a node port automatically).
port
Expose the service on the specified port internally within the cluster. That is, the service becomes visible on this port, and will send requests made to this port to the pods selected by the service.
targetPort
This is the port on the pod that the request gets sent to. Your application needs to be listening for network requests on this port for the service to work.
Learn Kube Faster.
Get the #1 guide.
Get my book on Kubernetes for software developers, used by engineers at Google, Microsoft, and IBM.
简单来说 targetPort 是指 pod 里服务在 podIP 上监听的端口,必须指定; port 是 expose 出来在 svc 上提供对外服务的端口,也必须指定;而 nodePort 是指定了 nodePort 方式后在 node IP 上提供服务的端口,这个不指定则随机分配。