Kubernetes 中容器端口和目标端口的区别? [英] Difference between Container port and targetport in Kubernetes?
问题描述
container port
与 targetports
在 Kubernetes 的容器中有何不同?它们是否可以互换使用,如果可以,为什么?
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.那么,这个 containerPort
和 targetport
有什么不同?
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.
推荐答案
简而言之: targetPort
和 containerPort
基本上是指同一个端口(因此,如果两者都使用,则预期它们具有相同的值)但它们用于两种不同的上下文并且具有完全不同的目的.
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 资源/对象规范的一部分:分别是 Service
和 Pod
.虽然 containerPort
的目的可以被视为纯粹的信息,targetPort
是暴露一组 Pods
的 Service
所需要的代码>.
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屋!