Kubernetes Java Client API Exception Kubernetes Java Client API Exception kubernetes kubernetes

Kubernetes Java Client API Exception


I'm facing the same exception too.After several survey to the client lib's source code, I think you need to make sure of two things.

  • first of all, can you access your api-server?
  • secondly, you need to check your ApiClient bootstrap order.

Which way do you use to config your connection

The first thing here may not correlated to your case or the lib.The api client lib supports three ways of configuration, to communicate with K8S apiserver from both inside of pod or out of cluster.

  • read env KUBECONFIG
  • read ${home}/.kube/config
  • read the service account resides under /var/run/secrets/kubernetes.io/serviceaccount/ca.crt

If you are using the lib inside a Pod, normally it will try to using the third way.

How you bootstrap your client.

You must keep in mind to invoke

Configuration.setDefaultApiClient(apiClient);

before you init a CoreV1Api or your CRD api.The reason is quite simply, because under all of the Api class, for example underthe class of io.kubernetes.client.api.CoreV1Api

public class CoreV1Api {    private ApiClient apiClient;    public CoreV1Api() {        this(Configuration.getDefaultApiClient());    }...}

If you haven't set the Configuration's defaultApiClient, it will use all default config, which the basePath will be localhost:443, then you will face the error.

Under the example package, The client has already created lots of examples and use case.The full configuration logic may be as below:

public class Example {  public static void main(String[] args) throws IOException, ApiException {    ApiClient client = Config.defaultClient();    Configuration.setDefaultApiClient(client);    // now you are safe to construct a CoreV1Api.    CoreV1Api api = new CoreV1Api();    V1PodList list =        api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);    for (V1Pod item : list.getItems()) {      System.out.println(item.getMetadata().getName());    }  }}

Just keeps in mind, order is important if you are using default constructor to init a XXXApi.


Localhost inside pod is not the same as localhost on nodes. Inside pods try using this URL: https://kubernetes.default.svc