Kubernetes中容器端口和目标端口的区别? [英] Difference between Container port and targetport in Kubernetes?

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

问题描述

container port 与 Kubernetes 中容器中的 targetports 有何不同?它们是否可以互换使用,如果可以,为什么?

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 interchangeably as both are part 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中容器端口和目标端口的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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