0

Using some simple code examples, I am trying to list all the pods in my namespace and find their IPs e.g.

ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
// now you are safe to construct a CoreV1Api.
CoreV1Api api = new CoreV1Api();
V1PodList list =
    api.listNamespacedPod("myNS", null, null, null, null, null, null, null, 30, false);
for (V1Pod item : list.getItems()) {
  System.out.println(item.getMetadata().getName());
}

Result:

SocketTimeoutException: connect timed out

Guessing it's something to do with the config maybe. I've also tried Config.fromCluster() but got the same result.

I should mention that this code is running in a pod in that namespace

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
orion_kid
  • 405
  • 1
  • 7
  • 20
  • You can list the pods in name space by using this command `kubectl get pod -n `. Refer to this [doc](https://www.studytonight.com/post/how-to-list-all-resources-in-a-kubernetes-namespace) by iamabhishek for more information to list all the resources in the namespace. – Hemanth Kumar Aug 17 '23 at 11:18
  • Compare your API with the code available in this [doc](https://www.baeldung.com/java-kubernetes-namespaces-selectors#using_namespaces), might help you in resolving this issue. – Hemanth Kumar Aug 17 '23 at 11:39
  • Thanks. Yes I am familiar with the kubectl tools, but I need to find the IPs inside a rest service, so that I can call the same endpoint on all the other pods. I tried that Baeldung code verbatim and got the same time out result unfortunately. – orion_kid Aug 17 '23 at 14:46
  • As per this [doc](https://kubernetes.io/docs/tasks/debug/debug-application/debug-service/) , You can get the list of Pod IP addresses using `kubectl get pods -l app=hostnames -o go-template='{{range .items}}{{.status.podIP}}{{"\\n"}}{{end}}'` . You can also refer to this [SO](https://stackoverflow.com/questions/57913132/) – Hemanth Kumar Aug 18 '23 at 07:48
  • Again, thanks I am familiar with kubectl, but this is not going to help me discover the pod IPs within a running Spring Boot app – orion_kid Aug 18 '23 at 12:32

1 Answers1

0

I had to add an egress rule from my app to the kube api server

orion_kid
  • 405
  • 1
  • 7
  • 20
  • If your issue is solved , can you please provide the resolution steps you have followed and provide it as an answer for the greater visibility of the community. One line answer is not a perfect way . – Hemanth Kumar Aug 23 '23 at 11:56