How to exec into a container and view file if container is in CrashLoopBackOff state

4/4/2019

I am running nodejs based application on kubernetes and it's in CrashLoopBackOff state.

kubectl logs api-5db677ff5-p824m

> api@0.0.1 staging /home/node
> NODE_ENV=staging node src/loader.js

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! api@0.0.1 staging: `NODE_ENV=staging node src/loader.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the api@0.0.1 staging script.
npm ERR! This is probably not a problem with npm. There is likely additional 
logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/node/.npm/_logs/2019-04-04T14_24_26_908Z-debug.log

There is not much information in logs and I cannot access the complete log file from the given path in container.

When I try check that file in container:

kubectl exec -it api-5db677ff5-p824m -- /bin/bash

It's giving me this error:

error: unable to upgrade connection: container not found ("api")

-- Ronak Patel
docker
kubernetes
node.js

2 Answers

4/4/2019

If you have access to the k8s node, you can access the logs of (all) the pods at /var/log/pods.

Alternatively, you can try to mount of PVC to your node pod and configure your node application to write to logs there. This will ensure that your crash logs are not destroyed when the container crashes.

Another similar approach is to override your pod container command with sleep 3600 and then exec into the container to run your node application manually. Once the node process crashes and writes the logs, you can then view them (inside the container).

Hope this helps!

-- Frank Yucheng Gu
Source: StackOverflow

4/4/2019

You can call exec only for containers which are in a "running" state.

If a container in a CrashLoopBackOff state, you cannot do it, because you have no running container where K8s can call a command.

The only way to get that file is to save it to some shared volume or host directory and then check it there. Try to check Volumes documentation.

-- Anton Kostenko
Source: StackOverflow