Kubernetes中的Container port和targetport之间的区别? [英] Difference between Container port and targetport in Kubernetes?
问题描述
Kubernetes容器中的container port
与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(在最后一行).那么,此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
是Service
所必需的,它公开了一组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屋!