Warm tip: This article is reproduced from serverfault.com, please click

其他-Kubernetes服务定义中targetPort和端口之间的区别

(其他 - Difference between targetPort and port in Kubernetes Service definition)

发布于 2018-04-23 12:52:07

Kubernetes在服务定义中Service可以有targetPortport

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

port之间有什么区别targetPort

Questioner
Jacob
Viewed
22
Manikanta P 2020-06-02 20:56:11

服务:这会将流量引导到吊舱。

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 

如果你点击,my-service:8089则将流量路由到8080容器(targetPort)。同样,如果你命中,my-service:8443则将其重定向到8085容器(targetPort)。但这myservice:8089是kubernetes集群的内部功能,可以在一个应用程序想要与另一应用程序通信时使用。因此,要从群集外部访问服务,需要有人公开运行kubernetes的主机上的端口,以便将流量重定向到容器的端口。这是node port(主机上暴露的端口)。在上面的示例中,你可以通过以下方式从群集外部( Postman 或任何其他客户端)访问服务:host_ip:nodePort

假设你的主机机的ip是10.10.20.20可以通过打HTTP,指标,卫生服务10.10.20.20:3047510.10.20.20:3126110.10.20.20:30013

编辑:根据Raedwald注释进行编辑