Kubernetes 对于NodePort类型的服务,并且指定了port和targetPort,这意味着什么?

Kubernetes 对于NodePort类型的服务,并且指定了port和targetPort,这意味着什么?,kubernetes,Kubernetes,我一天比一天更熟悉库伯内特斯了,但我仍然处于一个基本的水平。我也不是一个善于交际的人 我正盯着服务定义的以下片段,我无法在脑海中正确描述所声明的内容: spec: type: NodePort ports: - port: 27018 targetPort: 27017 protocol: TCP 参考,部分内容如下: nodePort The port on each node on which this service is exposed when t

我一天比一天更熟悉库伯内特斯了,但我仍然处于一个基本的水平。我也不是一个善于交际的人

我正盯着服务定义的以下片段,我无法在脑海中正确描述所声明的内容:

spec:
  type: NodePort
  ports:
  - port: 27018
    targetPort: 27017
    protocol: TCP
参考,部分内容如下:

nodePort     The port on each node on which this service is exposed when type=NodePort or LoadBalancer. Usually
integer      assigned by the system. If specified, it will be allocated to the service if unused or else creation of the
             service will fail. Default is to auto-allocate a port if the ServiceType of this Service requires one. More info: 
             http://kubernetes.io/docs/user-guide/services#type--nodeport

port         The port that will be exposed by this service.
integer

targetPort   Number or name of the port to access on the pods targeted by the service. Number must be in the range 1
IntOrString  to 65535. Name must be an IANA_SVC_NAME. If this is a string, it will be looked up as a named port in the
             target Pod's container ports. If this is not specified, the value of the 'port' field is used (an identity map).
             This field is ignored for services with clusterIP=None, and should be omitted or set equal to the 'port' field.
             More info: http://kubernetes.io/docs/user-guide/services#defining-a-service
我的理解是,集群外的客户端将“看到”的端口将是动态分配的端口,其范围为定义的
30000
-
32767
。这将使用一些我还不了解的黑魔法,流向给定节点上的
targetPort
27017


那么这里使用的
端口是什么

nodePort
是集群外的客户端将“看到”的端口<代码>节点端口
通过在集群中的每个节点上打开。然后使用iptables magic Kubernetes(k8s)将流量从该端口路由到匹配的服务吊舱(即使该吊舱运行在完全不同的节点上)

端口
是您的服务在集群内侦听的端口。让我们举一个例子:

---
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  ports:
  - port: 8080
    targetPort: 8070
    nodePort: 31222
    protocol: TCP 
  selector:
    component: my-service-app
从我的k8s群集中可以通过
my service.default.svc.cluster.local:8080
(群集中的服务对服务通信)访问此服务,到达该群集中的任何请求都会转发到
targetPort
8070上的运行pod


tagetPort
如果没有另外指定,默认情况下也与
port
相同。

为了更好地解释这个概念,我将服务的节点端口概念可视化

正如他在回答中提到的,NodePort允许将k8s主机端口(aka
NodePort
)公开给外部客户端。客户端可以直接访问
节点端口
,k8s将流量转发到必要的端口

K8s在其所有节点上保留一个
nodePort
。所有运行服务吊舱的节点都打开了此端口


POD不仅可以通过内部群集IP访问,还可以通过节点的IP和保留端口aka
HOST\u IP:node\u port
pair访问。

谢谢。所以
nodePort
不是服务侦听的端口?相反,它是一个在承载POD的节点上打开的端口,服务前端是唯一的?
nodePort
,因此两个不同的服务不能分配相同的
nodePort
。一旦声明,k8s主机将为该服务保留
nodePort
<然后在每个节点(主节点和工作节点)上打开code>nodePort——以及不运行该服务pod的节点——magic负责路由。这样,您就可以从k8s群集外部向
nodePort
上的任何节点发出服务请求,而无需担心pod是否安排在那里。从#kubernetes用户松弛频道:“nodePort路由到服务,然后再路由到pod/如果您直接点击服务,则跳过
nodePort
步骤/由
kube代理处理“神奇路由”。(使用发明的符号):“
nodePort
->
端口
->
目标端口
,不是
targetPort
->
targetPort
&
port
->
targetPort
“@lairdelson我喜欢这个端口链解释。我不完全确定kube代理是否总是先路由到服务
端口
(来自
nodePort
)或者,如果在某些情况下它跳过这一步,直接进入
nodePort
->
targetPort
。感谢您的澄清!在您的示例中:any.host.in.the.cluster:31222=>service.listening:8080=>one.of.the.pods.listening:8070此场景中的更多信息:我相信该图像是误导性的。如果CURL是一个外部调用,则它应该是uld指向云节点端口,请求会跟随到pod。这意味着云部分应该有节点IP:10.0.2.15和节点端口:30230。此外,“pod:CURL”pod应该与直接到“pod:Nginx”pod的请求一起显示。您也可以有“pod:CURL”“pod有一个对节点云部分的请求,但我认为您不想描述它。在这个图中,我认为‘hostPort’是
节点端口
,而‘Node Port’是
端口
。这令人困惑!我猜