How do I tell when/if/why a container in a kubernetes cluster restarts?
You can view the last restart logs of a container using:
kubectl logs podname -c containername --previous
As described by Sreekanth, kubectl get pods should show you number of restarts, but you can also run
kubectl describe pod podname
And it will show you events sent by the kubelet to the apiserver about the lifecycled events of the pod.
You can also write a final message to /dev/termination-log, and this will show up as described in the docs.
Beside the previous answers another command that helped me to find an error is:
kubectl get event [--namespace=my-namespace]
It lists events from Pods, Jobs, Nodes too
kubectl get pods will actually list any restarts of the container also the describe command can be of help cause it lists any events associated with the pod.
Liveness probes and readiness probes can be configured for better handling check here
https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/
Additionally hooks can be configured to be consumed in the container at specific points in the life cycle of the container check here
https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/