I have a question, when we provide static Endpoints
for the Service
, is targetPort
ignored ?
As we can see below, endpoint listens on port 10235 and in Service
targetPort
I specified port 80. It works as expected, so I guess in this case targetPort
is totally ignored. Is it right?
$ 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>
1
TargetPort
is the port that the service will send requests to, so this would be the port that your pod is listening on. Endpoint
is the location (IP address) of the pod itself.
So when kubernetes manages the endpoints
that is a collection of IP:port
, but sometimes when you manually create a endpoints
you have to specify the ports, usually the targetPort
field in the Service specifies the port number
for routing traffic to the Service’s pods, while the port field directs incoming traffic to the specified port on the Service itself, sometimes it is possible that the targetPort
defaults to the same value as the port. So in your case Kubernetes will simply forward traffic from the Service to the port specified in the Endpoint.
For more information follow this Medium blog by Daniele Polencic