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

4/21/2020

My CI tool uses lifecycles so if Dev deployments works, it goes to QA.

I have an end to end test container that i want to run in kubernetes, but how do i get the exit code from the container?

Can i somehow run the container and get back the exit code in one command?

kubectl run -it doesn't seem to get the exit code and has some extra things to say after the container is done.

-- tekno45
continuous-deployment
kubectl
kubernetes

4 Answers

4/29/2020

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" deleted
pod default/exitcode terminated (Error)
$ echo $?
1

$ kubectl run -it --rm exitcode --image=nginx --restart=Never -- bash -c "exit 8"
pod "exitcode" deleted
pod default/exitcode terminated (Error)
$ echo $?
8

$ kubectl run -it --rm exitcode --image=nginx --restart=Never -- bash -c "exit 250"
pod "exitcode" deleted
pod default/exitcode terminated (Error)
$ echo $?
250

$ kubectl run -it --rm exitcode --image=nginx --restart=Never -- bash -c "exit 255"
pod "exitcode" deleted
pod 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 $a
pod "exitcode" deleted
pod default/exitcode terminated (Error)
255
-- VAS
Source: StackOverflow

4/21/2020
kubectl get po pod_name -ojson | jq .status.containerStatuses[].state.terminated.exitCode
-- FL3SH
Source: StackOverflow

4/21/2020

You can pipe the output of kubectl get to jq which can parse the json and print the exit code, you may skip the -c container_name if single container is present.

kubectl get pod pod_name -c container_name-n namespace -ojson | jq .status.containerStatuses[].state.terminated.exitCode
-- prashant
Source: StackOverflow

4/21/2020

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: v1
kind: 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}"
-- mWatney
Source: StackOverflow