How to start container with kubectl and get exit code back? without kubectl exec How to start container with kubectl and get exit code back? without kubectl exec kubernetes kubernetes

How to start container with kubectl and get exit code back? without kubectl exec


To get the exit code from a Pod (container) you can get the pod details with the command:

kubectl get pod termination-demo --output=yaml

Output:

apiVersion: v1kind: Pod...    lastState:      terminated:        containerID: ...        exitCode: 0        finishedAt: ...        message: |          Sleep expired        ...

To know more, you can check the documentation.

To make it easier as you wish you can run:

kubectl get pod busybox-term -ojson | jq .status.containerStatuses[].lastState.terminated.exitCode

Or if you don't want to install jq, you can run:

kubectl get pod busybox-term --output="jsonpath={.status.containerStatuses[].lastState.terminated.exitCode}"


This way was mentioned by mWatney previously, so I've just put some additional details here:

This way you can return exit code 0-255 (after 255 it starts over, 256==0) from a Pod.

-it and --restart=Never are required, --rm is optional, but useful to remove failed pods.
--restart=Never tells the generator to create a Pod object instead of Deployment.

$ kubectl run -it --rm exitcode --image=nginx --restart=Never -- bash -c "exit 0"pod "exitcode" deleted$ echo $?0$ kubectl run -it --rm exitcode --image=nginx --restart=Never -- bash -c "exit 1"pod "exitcode" deletedpod default/exitcode terminated (Error)$ echo $?1$ kubectl run -it --rm exitcode --image=nginx --restart=Never -- bash -c "exit 8"pod "exitcode" deletedpod default/exitcode terminated (Error)$ echo $?8$ kubectl run -it --rm exitcode --image=nginx --restart=Never -- bash -c "exit 250"pod "exitcode" deletedpod default/exitcode terminated (Error)$ echo $?250$ kubectl run -it --rm exitcode --image=nginx --restart=Never -- bash -c "exit 255"pod "exitcode" deletedpod default/exitcode terminated (Error)$ echo $?255$ kubectl run -it --rm exitcode --image=nginx --restart=Never -- bash -c "exit 256"pod "exitcode" deleted$ echo $?0# exit code can also be assigned to a variable$ kubectl run -it --rm exitcode --image=nginx --restart=Never -- bash -c "exit 255" ; a=$? && echo $apod "exitcode" deletedpod default/exitcode terminated (Error)255

Update for LoganMzz:

$ kubectl run -it --rm --image=busybox --restart=Never foobar -- ash -c 'exit 10'; echo rc=$?pod "foobar" deletedpod default/foobar terminated (Error)rc=10$ kubectl versionClient Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.5", GitCommit:"e6503f8d8f769ace2f338794c914a96fc335df0f", GitTreeState:"clean", BuildDate:"2020-06-26T03:47:41Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}Server Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.5", GitCommit:"e6503f8d8f769ace2f338794c914a96fc335df0f", GitTreeState:"clean", BuildDate:"2020-06-26T03:39:24Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}$ kubectl get nodes -o wideNAME         STATUS   ROLES    AGE    VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME10.10.0.21   Ready    master   2d1h   v1.18.5   10.10.0.21    <none>        Ubuntu 16.04.6 LTS   4.4.0-184-generic   docker://18.9.710.10.0.22   Ready    <none>   2d1h   v1.18.5   10.10.0.22    <none>        Ubuntu 16.04.6 LTS   4.4.0-184-generic   docker://18.9.7


kubectl get po pod_name -ojson | jq .status.containerStatuses[].state.terminated.exitCode