Taming Container Logs with Loki and systemd Journal

Introduction to Container Logging

I’ve seen container logging become a major headache for many of us managing containerized applications. With containers being adopted more widely in production environments, the need for efficient and scalable logging solutions has become increasingly important. Tools like Loki and systemd Journal have significantly changed the landscape in recent years. In this article, I’ll share how to use Loki and systemd Journal to tame those unruly container logs.

What is Loki?

Loki, developed by Grafana Labs, is a logging system designed to handle large volumes of log data from containers and other sources. It’s highly scalable, efficient, and integrates well with popular monitoring tools like Prometheus and Grafana. Some of Loki’s key features include:

  • Scalable log ingestion and storage
  • Support for multiple log formats, including JSON and plain text
  • Integration with Kubernetes and other container orchestration systems
  • Real-time log filtering and querying

To get started with Loki, you can install it on your system using the official Helm chart:

helm repo add loki https://grafana.github.io/loki/charts
helm install loki loki/loki

This will deploy Loki to your Kubernetes cluster, where you can configure it to collect logs from your containers.

What is systemd Journal?

systemd Journal is a system logging daemon that comes bundled with systemd, a popular init system used in many Linux distributions. It provides a centralized logging solution for system events, including container logs. Some of systemd Journal’s key features include:

  • Centralized logging for system events and container logs
  • Support for multiple log formats, including JSON and plain text
  • Real-time log filtering and querying
  • Integration with popular logging tools like journald and rsyslog

To configure systemd Journal to collect container logs, you can create a drop-in configuration file:

sudo tee /etc/systemd/journald.conf.d/container-logs.conf <<EOF
[Journal]
ForwardToSyslog=yes
EOF

This will forward container logs to the systemd Journal, where you can query and filter them using the journalctl command.

Integrating Loki with systemd Journal

The real trick is integrating Loki with systemd Journal. To do this, you can use the loki-systemd-journal plugin, which allows Loki to collect logs from systemd Journal. To configure the plugin, you can create a configuration file:

sudo tee /etc/loki/config.yaml <<EOF
server:
  http_listen_port: 3100

ingester:
  lifecycler:
    ring:
      kvstore:
        store: inmemory

store:
  boltdb:
    path: /tmp/loki.db

schema_config:
  configs:
  - from: 2022-01-01
    store: boltdb
    object_store: filesystem
    schema: v11
    index:
      prefix: loki_
      period: 24h

positions:
  filename: /tmp/positions.yaml

limits_config:
  max_batch_size: 1000

chunk_idle_period: 5m
chunk_retain_period: 30s
chunk_queue_size: 10000

table_manager:
  retention_deletes_enabled: true
  retention_period: 30d

ingester_client:
  remote_timeout: 10m

auth:
  enabled: false

EOF

Then, you can configure the loki-systemd-journal plugin to collect logs from systemd Journal:

sudo tee /etc/loki/loki-systemd-journal.conf <<EOF
[plugin]
name = systemd-journal
config = {
  "journal_match": "_TRANSPORT=container",
  "journal_path": "/var/log/journal"
}
EOF

This will configure Loki to collect logs from systemd Journal, where you can query and filter them using the loki command.

Security Considerations

Don’t bother with half-measures when it comes to security - make sure to:

  • Use secure communication protocols, such as HTTPS, to transmit log data
  • Configure access controls, such as authentication and authorization, to restrict access to log data
  • Regularly review and rotate log files to prevent unauthorized access
  • Monitor log data for suspicious activity and anomalies

For more information on securing Loki and systemd Journal, you can refer to the official documentation on systemd.io and github.com.

Troubleshooting

When troubleshooting issues with Loki and systemd Journal, I usually start with the basics:

  • journalctl -u loki to view Loki logs
  • journalctl -u systemd-journald to view systemd Journal logs
  • loki -h to view Loki command-line options
  • systemctl status loki to view Loki service status

You can also refer to the official documentation on github.com and systemd.io for more troubleshooting tips and guides.


See also