Kubernetes REST API - Execute a command in container with proper example (java or groovy)

3/30/2021

I have searched in many places but unable to find one proper way of implementing exec via REST apis(with headers). Can anyone provide an example ?

Example Usecase:

I have deployed a mySQL container in kubernetes. I need to create a table in mySQL container using kubernetes REST api. Tried the following,

URL url = new URL("https://x.x.x.x:6443/api/v1/namespaces/namespace/pods/podname/exec?container=containername&command=commandtoexecute&stdin=true&stderr=true&stdout=true&tty=true");
URLConnection con = url.openConnection();
HttpsURLConnection conn= (HttpsURLConnection) con;
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Accept","*/*");
conn.setRequestProperty("Connection", "upgrade");
conn.setRequestProperty("Upgrade", "websocket");
conn.setRequestProperty("Authorization", "Bearer "+token);

Getting the following error

Exception: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

-- user8226431
kubernetes
kubernetes-pod

0 Answers