Kubernetes中的Container port和targetport之间的区别? [英] Difference between Container port and targetport in Kubernetes?

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

问题描述

Kubernetes容器中的container porttargetports有何不同? 它们可以互换使用吗?如果可以,为什么?

How is container port different from targetports in a container in Kubernetes? Are they used interchangeably, if so why?

我遇到了下面的代码片段,其中containerPort用于表示Kubernetes中Pod上的port.

I came across the below code snippet where containerPort is used to denote the port on a pod in Kubernetes.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deployment
  labels:
    app: demo-voting-app
spec:
  replicas: 1
  selector:
    matchLabels:
      name: postgres-pod
      app: demo-voting-app
  template:
    metadata:
      name: postgres-pod
      labels:
        name: postgres-pod
        app: demo-voting-app

    spec:
      containers:
      - name: postgres
        image: postgres:9.4
        ports:
        - containerPort: 5432
        

在上面的代码片段中,他们为containerPort参数指定了5432(在最后一行).那么,此containerPorttargetport有何不同?

In the above code snippet, they have given 5432 for the containerPort parameter (in the last line). So, how is this containerPort different from targetport?

据我所知,术语port通常是指service(Kubernetes)上的port.如果我不正确,请纠正我.

As far as I know, the term port in general refers to the port on the service (Kubernetes). Correct me if I'm incorrect.

推荐答案

简而言之: targetPortcontainerPort基本上是指相同的端口(因此,如果两者都使用,则应该使用它们具有相同的值),但它们在两种不同的上下文中使用并且用途完全不同.

In a nutshell: targetPort and containerPort basically refer to the same port (so if both are used they are expected to have the same value) but they are used in two different contexts and have entirely different purposes.

它们不能互换使用,因为它们都是两个不同的kubernetes资源/对象的规范的一部分:分别为ServicePod.虽然containerPort的目的可以纯粹用作参考,但targetPortService所必需的,它公开了一组Pods.

They cannot be used interchangebly as both are parts of the specification of two distinct kubernetes resources/objects: Service and Pod respectively. While the purpose of containerPort can be treated as purely informational, targetPort is required by the Service which exposes a set of Pods.

重要的是要理解,通过用Pod/Deployment规范中的特定值声明containerPort,您不能使Pod公开此特定端口,例如.如果您在containerPort字段中声明nginx Pod公开了端口8080而不是默认的80,则仍需要在容器中配置nginx服务器以侦听此端口.

It's important to understand that by declaring containerPort with the specific value in your Pod/Deployment specification you cannot make your Pod to expose this specific port e.g. if you declare in containerPort field that your nginx Pod exposes port 8080 instead of default 80, you still need to configure your nginx server in your container to listen on this port.

Pod规范中声明containerPort是可选的.即使没有它,您的Service也会根据在targetPort中声明的信息知道将请求定向到何处.

Declaring containerPort in Pod specification is optional. Even without it your Service will know where to direct the request based on the info it has declared in its targetPort.

请记住,不需要在Service定义中声明targetPort.如果省略它,则默认为您为port声明的值(这是Service本身的端口).

It's good to remember that it's not required to declare targetPort in the Service definition. If you omit it, it defaults to the value you declared for port (which is the port of the Service itself).

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

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