Kubernetes服务可以在服务定义中有一个targetPort和port:

kind: Service
apiVersion: v1
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9376

端口和targetPort有什么区别?


当前回答

服务是一种抽象,用于将流量重定向到底层pod。 因此:

端口:80——>标识服务正在侦听的端口 targetPort: 8080——>标识部署底层pod的目标端口

对my_service:80的调用将把流量重定向到8080上的底层pod(根据给定的示例)。

其他回答

这个答案是参考Kubernetes的文档以及其他答案:

https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/:

targetPort:容器接收流量的端口,

port:是抽象的服务端口,它可以是其他pod用来访问服务的任何端口

https://kubernetes.io/docs/concepts/services-networking/service/:

Pods中的端口定义有名称,您可以在服务的targetPort属性中引用这些名称。即使服务中有使用单一配置名称的pod的混合物,通过不同的端口号使用相同的网络协议,这种方法也有效。

在简而言之

nodeport:监听nodeip:nodeport上所有工作节点上的外部请求,并将请求转发到port。

ClusterIP:请求来自入口,指向服务名称和端口。

port:容器的内部集群服务端口,监听来自节点端口的传入请求并转发到targetPort。

targetPort:从端口接收请求并转发到它正在侦听的容器吊舱(端口)。即使不指定,默认情况下也会分配与port相同的端口号。

所以流量流入——>服务——>端点(基本上有POD IP)——>POD

如果容器监听端口9376,那么 发送:9376

如果服务在端口80上侦听,则 端口:80

然后服务端口配置如下所示

ports:
 - protocol: TCP
   port: 80
   targetPort: 9376

最后,请求接收到服务的端口,并在pod的targetPort上转发。

服务:这将流量引导到一个豆荚。

TargetPort:这是您的应用程序在容器中运行的实际端口。

端口:有时容器中的应用程序在不同的端口上提供不同的服务。

示例:实际应用程序可以运行8080,该应用程序的健康检查可以在容器的8089端口上运行。 因此,如果你点击没有端口的服务,它不知道应该将请求重定向到容器的哪个端口。服务需要有一个映射,这样它才能到达容器的特定端口。

kind: Service
apiVersion: v1
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
    - name: http
      nodePort: 30475
      port: 8089
      protocol: TCP
      targetPort: 8080
    - name: metrics
      nodePort: 31261
      port: 5555
      protocol: TCP
      targetPort: 5555
    - name: health
      nodePort: 30013
      port: 8443
      protocol: TCP
      targetPort: 8085 

if you hit the my-service:8089 the traffic is routed to 8080 of the container(targetPort). Similarly, if you hit my-service:8443 then it is redirected to 8085 of the container(targetPort). But this myservice:8089 is internal to the kubernetes cluster and can be used when one application wants to communicate with another application. So to hit the service from outside the cluster someone needs to expose the port on the host machine on which kubernetes is running so that the traffic is redirected to a port of the container. This is node port(port exposed on the host machine). From the above example, you can hit the service from outside the cluster(Postman or any rest-client) by host_ip:nodePort

假设您的主机ip为10.10.20.20,您可以通过10.10.20.20:30475、10.10.20.20:31261、10.10.20.20:30013访问http、metrics、运行状况服务。

编辑:根据Raedwald的评论编辑。

目标器端口说明

# pod file
apiVersion: v1
kind: Pod
metadata: 
  name: pod_name
  labels: 
     name: pod_ref
spec: 
  containers:
  - name: docker-container
    image: python:3:11
    ports: 
    - containerPort: 5000 # this is the target port which we need to access (target) from service file

---
apiVersion: v1
kind: Service
metadata: 
   name: service_config
   labels: 
     name: service_config_ref
spec:
   type: NodePort
   selector:
     name: pod_ref  # same as pod metadata match labels 
     ports:
      - targetPort: 5000 # this is where the pod is listening 
        port: 80 # this is where this service (this file) is going to listen. 
                 # If you call the port 80, it will reach the service, then the service will forward the port to port:5000