如今,我对Kubernetes越来越熟悉,但仍处于基本水平。我也不是网络专家。

我盯着下面的Service定义片段,无法确定所声明的内容:

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

引用ServicePort documentation,部分读取:
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

我的理解是,群集外部的客户端将“看到”的端口将是in the documentation定义的30000-32767范围内的动态分配端口。这将使用我尚不了解的一些黑魔法流到给定节点上的targetPort(在这种情况下为27017)。

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

最佳答案

nodePort是集群外部的客户端将“看到”的端口。通过kube-proxy在集群中的每个节点上打开nodePort。然后使用iptables magic Kubernetes(k8s)将流量从该端口路由到匹配的服务Pod(即使该Pod运行在完全不同的节点上)。
port是您的服务在集群内部监听的端口。让我们来看这个例子:

---
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相同,如果未另行指定。

关于kubernetes - Service的类型为NodePort,并且同时指定了port和targetPort是什么意思?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41963433/

10-10 04:37