Search code examples
kubernetescontinuous-deploymentkubectl

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.


Solution

  • 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}"