← Back to Blog

πŸ–₯️ Why Use the -c Flag in Kubernetes with kubectl logs?

πŸ–₯️ Why Use the -c Flag in Kubernetes with kubectl logs? πŸ–₯️ Multi Container pod In Kubernetes, pods often contain multiple containers. Monitoring and troubleshooting these containers requires targeted log retrieval.

πŸ–₯️ Why Use the -c Flag in Kubernetes with kubectl logs?


πŸ–₯️ Multi Container pod

In Kubernetes, pods often contain multiple containers. Monitoring and troubleshooting these containers requires targeted log retrieval. This is where the -c flag in the kubectl logs command becomes extremely useful.

In this post, we’ll explore the why and how behind the -c flag with a real-world example from a monitoring setup using Prometheus and Thanos.


<img src="./screenshot.png" alt="Kubernetes Logs Example with the -c


πŸš€ What Does the -c Flag Do?

The -c flag in kubectl logs is used to specify a container within a pod. A Kubernetes pod can contain one or more containers, and each container can generate its own set of logs. The -c flag allows you to fetch logs from a specific container inside the pod.

Without this flag, Kubernetes will return the logs from the first container in the pod (if there are multiple containers), which might not always be the container you need logs from.


πŸ› οΈ Example: Using the -c Flag with Prometheus and Thanos

In the example above, we are running two important components in a Kubernetes monitoring stack:

  • prometheus-operator

  • prometheus-staging-0 (a Prometheus pod)

Now, let’s say you're debugging an issue specifically with the Thanos sidecar component that sits alongside Prometheus to enable long-term storage or other advanced functionalities.

By running the following command:

kubectl logs -f prometheus-staging-0 -c thanos-sidecar -n monitoring

You are asking Kubernetes to fetch logs specifically from the thanos-sidecar container inside the prometheus-staging-0 pod. This way, you’re avoiding clutter from Prometheus logs and focusing only on the relevant information for Thanos.


πŸ’‘ Why Should You Use the -c Flag?

Here are a few scenarios where the -c flag is a game-changer:

  • Multiple Containers: When your pod has multiple containers, you don’t want to sift through irrelevant logs. The -c flag helps you focus on the container you care about.

  • Debugging: If only one container is experiencing issues, fetching logs from that container directly simplifies the debugging process.

  • Performance: By pulling logs from a specific container, you reduce the amount of log data being processed and sent back, which can be faster and more efficient.


πŸ“ Common Pitfalls

  • Missing -c Flag: If you omit the -c flag in a multi-container pod, Kubernetes will return logs from the first container. This can lead to confusion, especially if the issue is in a different container.

  • Container Not Found: Always ensure you have the correct container name, as incorrect names will result in an error.

  • Not Using Namespaces: If your services are in specific namespaces like monitoring in the example, don't forget to include the -n flag, or Kubernetes will assume you're querying the default namespace.


🎯 Conclusion

The -c flag in kubectl logs is an essential tool for targeted log retrieval, especially when you're working with pods running multiple containers. It allows for more efficient log fetching and simplifies the debugging process by giving you the logs for just the container you’re interested in.


πŸ”— Connect with me:


This post incorporates the screenshot to visualize the process and helps guide the reader through understanding how -c is used in Kubernetes.

Let me know if you'd like to refine this further or make any adjustments!


Imported from rifaterdemsahin.com Β· 2025