java icon indicating copy to clipboard operation
java copied to clipboard

How to use Kubernetes Java Client

Open syonchao opened this issue 8 years ago • 2 comments

my kubernetes master and slave in the host :192.168.1.182, use the secure port 6443. my java project in 192.168.1.106,

ApiClient client = Config.fromUrl("https://192.168.1.182:6443");
client.setSslCaCert(new FileInputStream("ca.pem")); // this is a right pem
Configuration.setDefaultApiClient(client);
CoreV1Api api = new CoreV1Api();
V1NamespaceList namespaceList= api.listNamespace(null,null,null,null,null,null, null, null, null);

but it's error

io.kubernetes.client.ApiException: Unauthorized
at io.kubernetes.client.ApiClient.handleResponse(ApiClient.java:882)
at io.kubernetes.client.ApiClient.execute(ApiClient.java:798)
at io.kubernetes.client.apis.CoreV1Api.listNamespaceWithHttpInfo(CoreV1Api.java:15606)
at io.kubernetes.client.apis.CoreV1Api.listNamespace(CoreV1Api.java:15584)

I wannt to ask ,how to use Kubernetes Java Client when the port of apiserver is 6443?

syonchao avatar Oct 31 '18 03:10 syonchao

you need token.

kubectl describe secrets admin-token-xxx --namespace kube-system

you can get token

kai8406 avatar Nov 01 '18 08:11 kai8406

Do you have a valid Kubeconfig file?

If you do (and it is in ${KUBECONFIG} or ${HOME}/.config/kube) then

Config.defaultClient();

Will just load it for you.

If not and you are constructing the client in code, then you need to add a token, or a certificate to the config in order to identify yourself, depending on how authentication is setup for your cluster.

brendandburns avatar Nov 02 '18 05:11 brendandburns