考虑一个非常简单的 service.yaml 文件:
kind: Service
apiVersion: v1
metadata:
name: gateway-service
spec:
type: NodePort
selector:
app: gateway-app
ports:
- name: gateway-service
protocol: TCP
port: 80
targetPort: 8080
nodePort: 30080
我们知道服务会将所有请求路由到带有此标签的 Pod
app=gateway-app
在端口 8080
(又名 targetPort
)。还有一个port
服务定义中的字段,即 80
在这种情况下。这是什么port
用于?我们应该什么时候使用它?从文档中,还有这一行:
By default the
targetPort
will be set to the same value as the port field.Reference: https://kubernetes.io/docs/concepts/services-networking/service/
换句话说,我们什么时候应该保留
targetPort
和 port
一样,什么时候不一样?
最佳答案
在 nodePort
服务可以定义 3 种类型的端口:
目标端口 :
正如您在问题中提到的,这是您的 pod 的相应端口,本质上是 containerPort
s 您已在副本 list 中定义。
端口(服务端口) :
这定义了其他本地资源可以引用的端口。引用 Kubernetes 文档:
this Service will be visible [locally] as .spec.clusterIP:spec.ports[*].port
这意味着,这不能公开访问,但是您可以使用此端口通过其他资源(集群内)引用您的服务端口。例如,当您为此服务创建入口时。在您的入口中,您将需要在
servicePort
中显示此端口 field : ...
backend:
serviceName: test
servicePort: 80
节点端口 :
这是您节点上公开公开您的服务的端口。再次引用文档:
this Service will be visible [publicly] as [NodeIP]:spec.ports[*].nodePort
关于kubernetes - 用于 Kubernetes 服务的 "port"是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55797312/