Question

How to check the logs of running and crashed pods in Kubernetes?

If you have managed any kind of Linux bases servers, you have probably used commands like cat and tail to check your server logs.

Here I will show you how to check the logs of your Kubernetes pods for both running and crashed pods using the kubectl command.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
October 28, 2020
Accepted Answer

Before you get started, you need to have the following things:

Getting the name of your pod

First, you need to get your pod’s name. To do so, you could run the following command:

  1. kubectl get pods

If you want to get the pods from a specific namespace, you need to use the following:

  1. kubectl --namespace='your-namespace' get pods

This will return a list of all of your pods, and you need to note down the name of the pods that you want to check the logs for:

NAME                              READY     STATUS             RESTARTS   AGE
nginx-7d8b49557c-c2lx9            1/1       Running            5          1d
nodejs-59f6cdb678-pkn2g           1/1       Running            0          1d
php-fpm-7dcb9c8dd6-r5jbj          1/1       Running            0          1d

With that, you are ready to check your logs!

Checking the logs of a running pod

Let’s say that we wanted to check the logs of the Nginx pod with the name nginx-7d8b49557c-c2lx9 as there have been 5 restarts. All that you need to do to do that is to run the following command:

  1. kubectl logs nginx-7d8b49557c-c2lx9

Note: you might have to specify your namespace in case that you have one

  1. kubectl --namespace logs nginx-7d8b49557c-c2lx9

This will show you all of the available logs for this specific pod.

Check the logs and tail them in real-time

Just like with the tail command, you can just use the -f flag to stream the logs in real-time.

To do so, you need to add the -f flag to the above commands:

  1. kubectl logs -f nginx-7d8b49557c-c2lx9

This will open a stream of your logs, and you will see the logs on your screen in real-time as they populate.

To stop that, just press CTRL+C.

Checking the logs of a crashed pod

In case that a pod restarts, and you wanted to check the logs of the previous run, what you need to do is to use the --previous flag:

  1. kubectl logs nginx-7d8b49557c-c2lx9 --previous

This will show you the logs of the last run of the pod before it crashed. It is a handy feature in case you want to figure out why the pod crashed in the first place.

Checking the logs of a specific container inside a pod

In some cases, you might have multiple containers running inside a single pod. Of course, it is better to keep things isolated and not stack up multiple containers in a single pod, but there are cases where you need to do that.

In case that there are 2 containers, you would see something like this when running kubectl get pods:

NAME                              READY       STATUS             RESTARTS   AGE
nginx-7d8b49557c-c2lx9            2/2        Running            5          1d

In this case, if you just run kubectl logs nginx-7d8b49557c-c2lx9, it will not work as Kubernetes will not know which container you want to check the logs for. You will see the following error:

error: a container name must be specified for pod nginx-7d8b49557c-c2lx9, 
choose one of: [nginx fpm]

As we can see from the output, Kubernetes wants us to specify one of the two containers we want to check the logs for: nginx or the fpm container.

To do that we just need to use the -c argument:

  1. kubectl logs nginx-7d8b49557c-c2lx9 -c nginx

You can add the other arguments like --previous and --namespace to this command as well.

Conclusion

This is pretty much it! Now you know how to check the logs of your Kubernetes pods! This will enormously help you with any troubleshooting that you need to do.

I hope that this helps!

Thanks, Good answer helpful!!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.