How to get logs of deployment from Kubernetes?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Understanding Kubernetes Logging
Kubernetes is a widely-used container orchestration platform that automates the deployment, scaling, and management of containerized applications. Monitoring and logging are crucial aspects of maintaining a healthy Kubernetes environment, as they provide valuable insights into the state and performance of your applications.
Logs in Kubernetes can come from various sources, such as the containers themselves, the cluster's nodes, or the Kubernetes control plane components. In this article, we will focus on retrieving logs related to deployments, particularly how to interact with the Kubernetes API and command-line tools like kubectl to access these logs efficiently.
Logs Retrieval Methods
Kubernetes supports multiple logging mechanisms. The logs of a deployment primarily originate from the pods that run your applications. Below, we detail several methods for retrieving these logs.
Using kubectl to Access Logs
The kubectl command-line tool is the most straightforward way to obtain logs from your Kubernetes deployments. Follow these steps to gather logs from your pods:
- List Pods in the Deployment:Before fetching logs, identify the pods associated with your deployment. Execute:
Replace <app-label> with the label assigned to your deployment and <namespace> with the appropriate namespace.
- Get Logs from a Pod:Once you've identified your pod, use
kubectl logsto fetch its logs:
If your pod contains multiple containers, specify the container name as well:
- Stream Logs:To continuously stream and monitor logs in real-time, use:
Accessing Node and System Component Logs
Apart from application logs, you might also need access to system logs for troubleshooting.
- Node Logs:Logs from the kubelet or other node-level components can be accessed through SSH (Secure Shell). Assuming you have SSH access:
Check the logs under directories such as /var/log/ to locate kubelet logs, containerd logs, etc.
- Control Plane Logs:Kubernetes control plane components such as the API server, scheduler, and controller manager have their logs stored, typically accessible through:
Advanced Logging Strategies
Centralized Logging Solutions
For environments with significant scale, centralized logging solutions, using tools like Elasticsearch, Fluentd, and Kibana (EFK Stack) or Prometheus and Grafana, come into play. These tools aggregate logs across the cluster for enhanced searching, visualizations, and management. Implement these solutions by deploying their operator or chart in your cluster.
Fluentd Example:
Fluentd can be integrated with Kubernetes to collect, process, and ship logs to an Elasticsearch backend.
- Deploy Fluentd as a DaemonSet:Deploy Fluentd on each node by creating a DaemonSet, which ensures a pod runs on every node:
- Configure Fluentd Input and Output:Fluentd requires a configuration to process logs. Here is a basic snippet to define input from the logs directory and output to Elasticsearch:
Summary Table of Key Points
| Step | Command/Description |
| List Pods in a Deployment | kubectl get pods -l app=<app-label> -n <namespace> |
| Get Logs from a Specific Pod | kubectl logs <pod-name> -n <namespace> |
| Get Logs from a Specific Container | kubectl logs <pod-name> -c <container-name> -n <namespace> |
| Stream Logs in Real-time | kubectl logs -f <pod-name> -n <namespace> |
| Access Node Logs via SSH | ssh <node-user>@<node-ip> |
| Get Control Plane Logs | kubectl logs <pod-name> -n kube-system |
| Deploy Fluentd as a DaemonSet | Use a custom YAML configuration to create a DaemonSet for Fluentd |
| Configure Fluentd for Log Processing | Define <source> and <match> in Fluentd config to process and ship logs to log aggregation services |
Conclusion
Retrieving and managing Kubernetes logs is not only about fetching outputs from pods but also requires insights into node and system component logs. Understanding and leveraging tools and solutions like kubectl, centralized logging systems, and advanced aggregation solutions ensures that you capture comprehensive logs for better troubleshooting and performance monitoring in a Kubernetes environment.
Related reading
- How to get logs of two pods without opening two terminals and using kubectl tail command?
- How to get number of pods running in prometheus
- How to get pods under the service with client-go the client library of kubernetes?
- How to get real-time resource usage of a pod in k8s?
- How to get mac host IP address from a docker container?
- How to get pipenv running in docker?
- How to get running pod status via Rest API
- How to get specific namespace for log fluentd

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.