75

Is there a way to get some details about Kubernetes pod that was deleted (stopped, replaced by new version).

I am investigating bug. I have logs with my pod name. That pod does not exist anymore, it was replaced by another one (with different configuration). New pod resides in same namespace, replication controller and service as old one.

Commands like

kubectl  get pods
kubectl  get pod <pod-name> 

work only with current pods (live or stopped).

How I could get more details about old pods? I would like to see

  1. when they were created
  2. which environment variables they had when created
  3. why and when they were stopped
Bartosz Bilicki
  • 12,599
  • 13
  • 71
  • 113

8 Answers8

67

As of today, kubectl get pods -a is deprecated, and as a result you cannot get deleted pods.

What you can do though, is to get a list of recently deleted pod names - up to 1 hour in the past unless you changed the ttl for kubernetes events - by running:

kubectl get event -o custom-columns=NAME:.metadata.name | cut -d "." -f1

You can then investigate further issues within your logging pipeline if you have one in place.

iomv
  • 2,409
  • 18
  • 28
  • 1
    I'm not sure why there are so many unhelpful answers here, the real answer is the last sentence: "You can then investigate further issues within your logging pipeline if you have one in place." – user64204 Jan 31 '22 at 12:05
5

As far as I know you cannot get the Pod details once the Pod is deleted. Can I know what is the usecase?

Example:

  1. if a Pod is created using kubectl run busybox-test-pod-status --image=busybox --restart=Never -- /bin/false you will have a Pod with status terminated:error
  2. if a Pod is created using kubectl run busybox-test-pod-status --image=busybox --restart=Never -- /bin/true you will have a Pod with status terminated:Completed
  3. if a container in a Pod restarts: the Pod will be alive and you can get the logs of previous container (only the previous container) using kubectl logs --container <container name> --previous=true <pod name>
  4. if you doing an upgrade of you app and you are creating Pods using Deployments. If the update deployment "say a new image", the Pod will be terminated and new Pod will be created. You can get the Pod details from the Deployment's YAML. if you want to get details of previous Pod you have see "spec" section of previous Deployment's YAML
Daniel Serodio
  • 4,229
  • 5
  • 37
  • 33
0

You can try kubectl logs --previous to list the logs of a previously stopped pod

http://kubernetes.io/docs/user-guide/kubectl/kubectl_logs/

You may also want to check out these debugging tips http://kubernetes.io/docs/user-guide/debugging-pods-and-replication-controllers/

Matt Rickard
  • 1,998
  • 1
  • 12
  • 9
  • 2
    This command no longer seems to work, and a pod name is requested. – Eric Walker Feb 12 '18 at 20:19
  • 12
    It works, but it is meant to show logs of previous *containers* inside an existing pod. From the doc: "If true, print the logs for the previous instance of the container in a pod if it exists." – dvdgsng Feb 16 '18 at 10:54
  • 6
    This only works if the pod still exists, which is not what the OP asked. – Andrés Mejía Dec 07 '20 at 18:56
0

There is a way to find out why pods were deleted and who deleted them. The only way to find out something is to set the ttl for k8s to be greater than the default 1h and search through the events:

kubectl get event -o custom-columns=NAME:.metadata.name | cut -d "." -f1

41 72 6c
  • 1,600
  • 5
  • 19
  • 30
  • The only robust way of monitoring k8s objects is setting up an audit policy for monitoring k8s events. See official k8s documentation on it: https://kubernetes.io/docs/tasks/debug-application-cluster/audit/ – Paul Chibulcuteanu Sep 26 '19 at 09:06
  • 2
    How is this different from the accepted answer? – iomv Sep 16 '21 at 10:39
-4

If your container has previously crashed, you can access the previous container’s crash log with:

kubectl logs --previous ${POD_NAME} ${CONTAINER_NAME}

Sunil Gajula
  • 1,117
  • 2
  • 14
  • 30
  • 4
    This is not what the OP is asking. `kubectl logs --previous` only works if the pod still exists. Once the pod is deleted (after running `kubectl pod delete `, for example), this command no longer works. – Andrés Mejía Dec 07 '20 at 18:59
-5

There is this flag:

-a, --show-all=false: When printing, show all resources (default hide terminated pods.)

But this may not help in all cases of old pods.

manojlds
  • 290,304
  • 63
  • 469
  • 417
-9
kubectl get pods -a    

you will get the list of running pods and the terminated pods in case you are searching for this

oLen
  • 5,177
  • 1
  • 32
  • 48
-13

If you want to see all the previously deleted pods and you are trying to fetch the previous pods.

Command line: kubectl get pods

in which you will get all the pod details, because every service has one or more pods and they have unique ip address

Here you can check the lifecycle of pods and what phases of pod has. https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle

and you can see the previous pod logs by typing a command: kubectl logs --previous

Hushen
  • 427
  • 1
  • 4
  • 15