How to Use kubectl logs in Kubernetes

·

4 min read


cicube.io

Introduction

Well, we wanted to share below some useful details about the kubectl logs command, especially when working with our Kubernetes clusters. Quite a handy command that retrieves logs from containers running in pods - this is quite an essential activity during debugging or monitoring.

Long story short:

You can fetch logs with kubectl logs from a single container or several containers running in a pod. If there is only one container running in a pod, you don't need to specify the container name. You may also use options such as -f for following your logs in real time, -p to check logs from a previous instance, or --all-containers to fetch logs from every container in a pod.

Some examples to get you started on this might be:

The following code fetches the latest logs from a single container: kubectl logs <pod_name>.

• For multi-container pods, give the container name whose log you want: kubectl logs <pod_name> -c <container_name>.

If using labels, get logs from pods with a label similar to the following: kubectl logs -l app=nginx --all-containers=true.

  • To tail logs, simply add -f to your command.

It provides options for limiting the output, such as --tail for showing only recent lines, the possibility to get logs from the last hour using --since, and even options to bypass verification of TLS certificates if that is an operational requirement: --insecure-skip-tls-verify-backend.

One thing to keep in mind with kubectl logs is that it retrieves output from the standard output and standard error streams of containers, so make sure those are properly configured in the logging configuration of the container.

Why/When to Use kubectl logs

The beauty of this command is how it makes debugging containerized applications agile and quick. You can easily stream logs, check past logs, and apply filters like container names or time ranges. This will definitely help us narrow down issues much faster when debugging, and it really simplifies the process of getting logs for any pod-would that be running or failed.

I would suggest using an alias, such as alias k=kubectl, as this might save some time for you. Hence, you wouldn't have to type the whole command every time.

Example: Viewing PostgreSQL Pod Logs

Pod name: postgresql-db-789df9f65d-abcde

Commands:

To see the latest logs:

kubectl logs postgresql-db-789df9f65d-abcde

To view the last 50 lines of logs:

kubectl logs --tail=50 postgresql-db-789df9f65d-abcde

To follow the logs in real time:

kubectl logs -f postgresql-db-789df9f65d-abcde

If there are multiple containers, see logs for a specific container:

kubectl logs postgresql-db-789df9f65d-abcde -c postgres

Example Log Output:

2024-10-02 12:45:10 UTC [1]: LOG:  starting PostgreSQL 14.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 10.2.1, 64-bit
2024-10-02 12:45:10 UTC [1]: LOG:  listening on IPv4 address "0.0.0.0", port 5432
2024-10-02 12:45:10 UTC [1]: LOG:  listening on IPv6 address "::", port 5432
2024-10-02 12:45:10 UTC [1]: LOG:  database system is ready to accept connections
2024-10-02 12:45:11 UTC [2]: LOG:  connection received: host=192.168.1.10 port=54832
2024-10-02 12:45:12 UTC [2]: LOG:  connection authorized: user=admin database=postgres
2024-10-02 12:45:20 UTC [3]: LOG:  statement: SELECT * FROM users;
2024-10-02 12:45:21 UTC [3]: LOG:  duration: 2.1 ms  statement: SELECT * FROM users;

Conclusion

The kubectl logs command is an indispensable tool for troubleshooting and monitoring applications in Kubernetes. By understanding its various options and how to use them efficiently, you can ensure smooth operations in your Kubernetes cluster.

Whether you need to debug a failed container, monitor real-time logs, or check logs from a specific time period, kubectl logs provides you with the flexibility and control you need.


Monitoring GitHub Actions Workflows

CICube is a GitHub Actions monitoring tool that provides you with detailed insights into your workflows to further optimize your CI/CD pipeline. With CICube, you will be able to track your workflow runs, understand where the bottlenecks are, and tease out the best from your build times. Go to cicube.io now and create a free account to better optimize your GitHub Actions workflows!

CICube GitHub Actions Workflow Duration Monitoring