Fetch logs for killed docker container in kubernetes

2/21/2019

A simplified version of the code

trap 'rm /tmp/not_done; echo "trap command got executed"; sleep 10' SIGTERM

wait_sigterm() {

  touch /tmp/not_done
  while [ -f /tmp/not_done ]
  do
      sleep 60 &
      wait $!  
  done
}

wait_sigterm

How can I make sure trap command got executed?

One way would be if I can get access to the logs of the killed container and check for my echo message.

I tried getting the logs (kubectl -n namespace-name logs pod-name container-name) in a while loop, so that I would be able to pick the last written logs till the container is alive.

But the echo message was not present. My guess is logs are not written once we kill the container or maybe something else. Is there any way I can get the logs of a killed container or test the above scenario?

-- user7131571
docker
kubernetes

1 Answer

2/21/2019

Approach 1

If you want to check out the logs of the previously terminated container from pod POD_NAME. You can use the following command with -p flag

kubectl logs POD_NAME -c CONTAINER_NAME -p

You can get further information such as options and flags with the following command

kubectl logs --help

Approach 2

another approach to determine the termination message is in the Status field of Pod object.

kubectl get pod $POD_NAME -o yaml

check the field lastState of containerStatuses:

status:
  conditions:

  containerStatuses:
  - containerID: 
    image: registry.access.redhat.com/rhel7:latest
    imageID:
    lastState: {}

    state:
      running:

Here is more detail reading-a-termination-message

-- Suresh Vishnoi
Source: StackOverflow