为Service配置静态Endpoints时,targetPort是否会被忽略?
为Service配置静态Endpoints时,targetPort字段是否会被忽略?
我有一个疑问:当我们为Service配置静态Endpoints时,Service中的targetPort字段是否会被忽略?如下所示,Endpoints监听的端口为10235,而我在Service的targetPort中指定了端口80,但服务仍能正常工作,因此我猜测这种情况下targetPort被完全忽略了,请问这个判断是否正确?
$ kubectl describe endpoints endpoint-1 Name: endpoint-1 Namespace: main Labels: <none> Annotations: <none> Subsets: Addresses: A.B.C.D NotReadyAddresses: <none> Ports: Name Port Protocol ---- ---- -------- <unset> 10235 TCP Events: <none> $ kubectl describe svc endpoint-1 Name: endpoint-1 Namespace: main Labels: <none> Annotations: <none> Selector: <none> Type: ClusterIP IP Family Policy: SingleStack IP Families: IPv4 IP: 172.10.67.160 IPs: 172.10.67.160 Port: <unset> 80/TCP TargetPort: 80/TCP Endpoints: A.B.C.D:10235 Session Affinity: None Events: <none>
你的判断是正确的。当手动配置静态Endpoints时,Service的targetPort字段确实会被忽略。
原因在于:Service的targetPort原本作用是将Service的端口映射到后端Pod的指定端口,但当你手动指定了静态Endpoints后,Endpoints资源已经明确给出了后端服务的具体地址和端口(也就是示例中的A.B.C.D:10235),此时Service会直接使用Endpoints里配置的端口进行流量转发,不再依赖targetPort去解析Pod的端口。
从你的示例也能验证这一点:Service的targetPort设为80,但实际流量被转发到了Endpoints指定的10235端口,服务依然正常工作,说明targetPort并未生效。
内容的提问来源于stack exchange,提问作者Xavier123
相关产品推荐
相关产品推荐

