π₯οΈ 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
-cflag 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
-cFlag: If you omit the-cflag 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
monitoringin the example, don't forget to include the-nflag, 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:
-
πΌ LinkedIn: Connect on LinkedIn
-
π¦ Twitter: Follow me on Twitter
-
π₯ YouTube: Watch on YouTube
-
π» GitHub: See my work on GitHub
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