Search code examples
kuberneteskubectlkubernetes-nodeport

Using Nodeport to access api in pod Kubernetes Service


I created a nodeport service on a port 12345, to access my api running on node 6666, deployed through a pod. The pod is deployed in multiple nodes.

 ports:
   - port: 8181
     targetPort: 6666
     nodePort: 12345
     protocol: TCP

In order to get the ip of the node, by running

   kubectl get nodes -o wide

the internal / external ip of the node was obtained. Thus, the resulting curl command is

   http://<node internal ip>:12345/

The issue is that this is circulating all the nodes, running through the open port, everytime a ping is done.

How can I ensure that the ping, using the internal ip only hits one node and the pod service running on it?

Thanks in advance.!!


Solution

  • I was able to solve the above issue by adding the

     externalTrafficPolicy: "Local"
    

    In the service definition. This way, the ping to the ip of the node, and to the api, did not go to other nodes, but rather was restricted locally to the node.