首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >服务的类型为NodePort,并且同时指定了端口和targetPort,这意味着什么?

服务的类型为NodePort,并且同时指定了端口和targetPort,这意味着什么?
EN

Stack Overflow用户
提问于 2017-02-01 01:08:30
回答 2查看 16.9K关注 0票数 52

我对Kubernetes的熟悉程度与日俱增,但仍处于初级水平。我也不是一个喜欢交际的人。

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

代码语言:javascript
复制
spec:
  type: NodePort
  ports:
  - port: 27018
    targetPort: 27017
    protocol: TCP

引用ServicePort documentation,其部分内容为:

代码语言:javascript
复制
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范围内动态分配的端口,正如in the documentation所定义的。这将使用一些我还不了解的黑魔法,流向给定节点上的targetPort (本例中为27017)。

那么port在这里用来做什么呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-01 01:31:22

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

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

代码语言:javascript
复制
---
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的值相同。

票数 56
EN

Stack Overflow用户

发布于 2018-04-30 01:22:12

为了更好地解释这个概念,我将服务的NodePort概念可视化。

正如@fishi在他的回答中提到的,NodePort允许向外部客户端公开k8s主机端口(也称为nodePort)。客户端可以直接访问nodePort,k8s会将流量转发到必要的端口。

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

Pod不仅可以通过内部集群IP访问,还可以通过节点的IP和预留端口访问,也就是HOST_IP:NODE_PORT对。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41963433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档