kubernetes服务定义中targetPort和port之间的区别 [英] Difference between targetPort and port in kubernetes Service definition

查看:159
本文介绍了kubernetes服务定义中targetPort和port之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

A Kubernetes Service can have a targetPort and port in the service definition:

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

porttargetPort有什么区别?

推荐答案

服务:这会将流量定向到pod.

Service: This directs the traffic to a pod.

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

TargetPort: This is the actual port on which your application is running inside the container.

端口:有时,容器内的应用程序会在不同的端口上提供不同的服务.例如:-实际应用程序可以运行8080,并且此应用程序的运行状况检查可以在容器的8089端口上运行. 因此,如果您在没有端口的情况下访问该服务,它将不知道应将请求重定向到容器的哪个端口.服务需要有一个映射,以便它可以访问容器的特定端口.

Port: Some times your application inside container serves different services on a different port. Ex:- the actual application can run 8080 and health checks for this application can run on 8089 port of the container. So if you hit the service without port it doesn't know to which port of the container it should redirect the request. Service needs to have a mapping so that it can hit the specific port of the container.

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,则流量将路由到容器(targetPort)的8080.同样,如果您点击my-service:8443,它将被重定向到容器(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).

但是此myservice:8089是kubernetes集群的内部组件,可以在一个应用程序想要与另一个应用程序通信时使用.因此,要从集群外部访问服务,需要有人公开运行kubernetes的主机上的端口 以便将流量重定向到容器的端口.在那可以使用nodePort.

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. In that can use nodePort.

在上面的示例中,您可以通过host_ip:Nodeport从集群外部(邮递员或任何restclient)访问该服务

From the above example, you can hit the service from outside the cluster(Postman or any restclient) 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,指标,健康服务

Say your host machine ip is 10.10.20.20 you can hit the http,metrics,health services by 10.10.20.20:30475,10.10.20.20:31261,10.10.20.20:30013

根据 Raedwald 注释进行编辑.

这篇关于kubernetes服务定义中targetPort和port之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆