kubernetes服务-可以在3个不同的变量值(端口,目标端口,节点端口)上使用相同的端口值(即10002)吗?

时间:2019-05-10 09:19:50

标签: docker kubernetes kubernetes-ingress

kubernetes服务-可以在3个不同的变量值(端口,目标端口,节点端口)上使用相同的端口值(即10002)吗?

spec:
  type: NodePort
  ports:
  - port: 10002
    name: webapi
    targetPort: 10002
    nodePort: 10002

2 个答案:

答案 0 :(得分:2)

在k8s服务规格中

TargetPort: Pod的端口

端口:服务的端口

NodePort: Nodepool的端口

因此,端口和目标端口必须相同,并且节点端口的值是动态变化的。 (默认值:30000-32767)

您可以使用以下命令kubectl explain service.spec.ports

获取详细信息
FIELDS:
   name <string>
     The name of this port within the service. This must be a DNS_LABEL. All
     ports within a ServiceSpec must have unique names. This maps to the 'Name'
     field in EndpointPort objects. Optional if only one ServicePort is defined
     on this service.

   nodePort     <integer>
     The port on each node on which this service is exposed when type=NodePort
     or LoadBalancer. Usually 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:
     https://kubernetes.io/docs/concepts/services-networking/service/#type-nodeport

   port <integer> -required-
     The port that will be exposed by this service.

   protocol     <string>
     The IP protocol for this port. Supports "TCP" and "UDP". Default is TCP.

   targetPort   <string>
     Number or name of the port to access on the pods targeted by the service.
     Number must be in the range 1 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:
     https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service

  

如果您需要特定的端口号,可以在nodePort字段中指定一个值,系统将为您分配该端口,否则API事务将失败(即,您需要自己注意可能发生的端口冲突)。您指定的值必须在节点端口的配置范围内。   nodeport

答案 1 :(得分:1)

服务ip和端口是虚拟的。它们未连接到网络接口。您可以将任何端口用作服务端口。它不必与Pod目标端口相同。

kubernetes中的默认nodeport范围是30000-32767。您还可以定义自定义范围。

因此,如果该端口已打开以进行连接,则Pod / server / nodeport可能具有相同的端口