Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 603
tekno45 Avatar asked Apr 21 '20 08:04

tekno45


People also ask

How do I find my container exit code?

You can even get the exit code without any extra cruft by running docker inspect 61c6 --format='{{. State. ExitCode}}' . That's perfect for scripts because you can check the code easily.

How do I get out of kubectl exec?

It is possible to force termination of kubectl exec by sending -9 signal using kill command.

How do I get out of Kubernetes cluster?

Stopping the Kubernetes clusterStop all worker nodes, simultaneously or individually. After all the worker nodes are shut down, shut down the Kubernetes master node. Note: If the NFS server is on a different host than the Kubernetes master, you can shut down the Kubernetes master when you shut down the worker nodes.


2 Answers

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}"
like image 176
Mark Watney Avatar answered Sep 26 '22 23:09

Mark Watney


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

Update for LoganMzz:

$ kubectl run -it --rm --image=busybox --restart=Never foobar -- ash -c 'exit 10'; echo rc=$?
pod "foobar" deleted
pod default/foobar terminated (Error)
rc=10

$ kubectl version
Client 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 wide
NAME         STATUS   ROLES    AGE    VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
10.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.7
10.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
like image 22
4 revs Avatar answered Sep 26 '22 23:09

4 revs