Kubernetes
deployment logs
Kubernetes logging
DevOps
container orchestration

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.

Practice system design

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:

  1. List Pods in the Deployment:
    Before fetching logs, identify the pods associated with your deployment. Execute:
bash
    kubectl get pods -l app=<app-label> -n <namespace>

Replace <app-label> with the label assigned to your deployment and <namespace> with the appropriate namespace.

  1. Get Logs from a Pod:
    Once you've identified your pod, use kubectl logs to fetch its logs:
bash
    kubectl logs <pod-name> -n <namespace>

If your pod contains multiple containers, specify the container name as well:

bash
    kubectl logs <pod-name> -c <container-name> -n <namespace>
  1. Stream Logs:
    To continuously stream and monitor logs in real-time, use:
bash
    kubectl logs -f <pod-name> -n <namespace>

Accessing Node and System Component Logs

Apart from application logs, you might also need access to system logs for troubleshooting.

  1. Node Logs:
    Logs from the kubelet or other node-level components can be accessed through SSH (Secure Shell). Assuming you have SSH access:
bash
    ssh <node-user>@<node-ip>

Check the logs under directories such as /var/log/ to locate kubelet logs, containerd logs, etc.

  1. Control Plane Logs:
    Kubernetes control plane components such as the API server, scheduler, and controller manager have their logs stored, typically accessible through:
bash
    kubectl logs <pod-name> -n kube-system

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.

  1. Deploy Fluentd as a DaemonSet:
    Deploy Fluentd on each node by creating a DaemonSet, which ensures a pod runs on every node:
yaml
1    apiVersion: apps/v1
2    kind: DaemonSet
3    metadata:
4      name: fluentd
5    spec:
6      selector:
7        matchLabels:
8          app: fluentd
9      template:
10        metadata:
11          labels:
12            app: fluentd
13        spec:
14          containers:
15          - name: fluentd
16            image: fluent/fluentd
17            resources:
18              limits:
19                memory: "200Mi"
20                cpu: "0.5"
  1. 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:
yaml
1    <source>
2      @type tail
3      @log_level debug
4      path /var/log/containers/*.log
5      pos_file /var/log/fluentd-containers.log.pos
6    </source>
7
8    <match **>
9      @type elasticsearch
10      host <elasticsearch-host>
11      port <elasticsearch-port>
12    </match>

Summary Table of Key Points

StepCommand/Description
List Pods in a Deploymentkubectl get pods -l app=<app-label> -n <namespace>
Get Logs from a Specific Podkubectl logs <pod-name> -n <namespace>
Get Logs from a Specific Containerkubectl logs <pod-name> -c <container-name> -n <namespace>
Stream Logs in Real-timekubectl logs -f <pod-name> -n <namespace>
Access Node Logs via SSHssh <node-user>@<node-ip>
Get Control Plane Logskubectl logs <pod-name> -n kube-system
Deploy Fluentd as a DaemonSetUse a custom YAML configuration to create a DaemonSet for Fluentd
Configure Fluentd for Log ProcessingDefine <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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.