Kubectl Java client returns exit code 3 when using exec

11/13/2019

I am writing a little backup programm for an application. This will run as a CronJob within my k8s cluster. At one point, it should trigger an mysql dump on the database inside another pod.

My code:

Exec exec = new Exec();
Process process = exec.exec(
    "default",
    "database-pod",
    new String[]{"sh", "-c", ""mysqldump -u {{user}} --p={{password}} schema > dbdump.sql",
    false,
    tty
);
process.waitFor();
process.destroy();
int exitValue = process.exitValue();

process.exitValue() always contains 3 + the mysql dump file is created, but does not contain any sql statements. Does somebody have a clue what I am doing wrong?

The base image of my backup programm is gcr.io/distroless/java:11 if that helps and was built using Jib

-- purzel
java
jib
kubectl
kubernetes

2 Answers

2/20/2020

When kubectl exec (or oc exec command) returns non-zero exit code, you should manually connect to the pod, and execute the same command directly, to inspect what's wrong:

[my-host]$ kubectl exec ${pod-id} -n ${namespace} bash -ti
[root@my-pod]# command...
# print command exit code (should be the same as before)
[root@my-pod]# echo $? 
-- Noam Manos
Source: StackOverflow

11/13/2019

So after a bit of reading I figured out what was going wrong. Instead of writing --p I should have used --password all along

-- purzel
Source: StackOverflow