服务类型为 NodePort 并且同时指定了端口和目标端口是什么意思? [英] What does it mean for a Service to be of type NodePort, and have both port and targetPort specified?

查看:26
本文介绍了服务类型为 NodePort 并且同时指定了端口和目标端口是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我日益熟悉 Kubernetes,但仍处于基本水平.我也不是网络人.

I am becoming more familiar with Kubernetes by the day, but am still at a basic level. I am also not a networking guy.

我正在盯着服务定义的以下片段,但我无法在脑海中对所声明的内容形成正确的画面:

I am staring at the following snippet of a Service definition, and I can't form the right picture in my mind of what is being declared:

spec:
  type: NodePort
  ports:
  - port: 27018
    targetPort: 27017
    protocol: TCP

参考 ServicePort 文档,部分阅读:

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).

My understanding is that the port that a client outside of the cluster will "see" will be the dynamically assigned one in the range of 30000-32767, as defined in the documentation. This will, using some black magic that I do not yet understand, flow to the targetPort on a given node (27017 in this case).

那么这里使用的 port 是什么?

So what is the port used for here?

推荐答案

nodePort 是集群外的客户端将看到"的端口.nodePort 通过 kube 在集群中的每个节点上打开-代理.使用 iptables magic Kubernetes (k8s) 然后将流量从该端口路由到匹配的服务 pod(即使该 pod 运行在完全不同的节点上).

nodePort is the port that a client outside of the cluster will "see". nodePort is opened on every node in your cluster via kube-proxy. With iptables magic Kubernetes (k8s) then routes traffic from that port to a matching service pod (even if that pod is running on a completely different node).

port 是您的服务在集群内侦听的端口.我们以这个例子为例:

port is the port your service listens on inside the cluster. Let's take this example:

---
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.

From inside my k8s cluster this service will be reachable via my-service.default.svc.cluster.local:8080 (service to service communication inside your cluster) and any request reaching there is forwarded to a running pod on targetPort 8070.

tagetPort 也默认与 port 相同的值,如果没有另外指定.

tagetPort is also by default the same value as port if not specified otherwise.

这篇关于服务类型为 NodePort 并且同时指定了端口和目标端口是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆