Kubernetes how to get error messages/logs

Hi guys, I have created an api service for my website that pulls the image from docker hub. That is all working fine, but the container isn’t running properly and I wanted to take a look inside the container for some kind of error message. Is there a way of doing that? What is the proper way to inspect logs for each pod/service/container?

Also, what do I use to create environment variables? Is this done via Config Maps?

Show comments

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.

Accepted Answer

Hi, You can use kubectl logs to retrieve logs from a previous instantiation of a container with --previous flag, in case the container has crashed. If your pod has multiple containers, you should specify which container’s logs you want to access by appending a container name to the command.

@christianstrang If you’re wanting to look inside the container and view its logs, you can do one of the following:

Using Docker

shell into container

docker run --rm -it <image-name> bash

view container’s logs

docker logs <container-name>

Note: If you’re using Alpine based image, you will need to use sh instead of bash.

Using Kubernetes

shell into container

kubectl exec -i -t <pod-name> --container <container-name> -- /bin/bash

Note: If you’re using Alpine based image, you will need to use sh instead of bash.

view container’s logs

kubectl logs <pod-name> -c <container-name>  

Note: A Kubernetes pod can consist of 1 or more running containers. Also, I would recommend taking a look at a tool called stern for viewing logs within K8s cluster.

Next, you can use K8s ConfigMaps for your environment variables and K8s Secrets for your sensitive credentials. Finally, I would highly recommend testing your Docker container(s) prior to using them within your K8s cluster.

Well, I wish that the above information was helpful and I wish you all the best.

Think different and code well,


Try DigitalOcean for free

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

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

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

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel