1. Answers
  2. Creating interactive log analysis dashboards with Grafana and Loki

How Do I Create Interactive Log Analysis Dashboards With Grafana and Loki?

Introduction

This guide aims to help you create interactive log analysis dashboards using Grafana and Loki on a Kubernetes cluster. Grafana is a widely-used open-source platform for monitoring and observability, and Loki is a log aggregation system that integrates seamlessly with Grafana. Together, they provide a powerful tool for log analysis. In this guide, we will walk through deploying both Grafana and Loki on a Kubernetes cluster using Pulumi.

Steps:

  1. Provision a Kubernetes cluster: Ensure you have a Kubernetes cluster running before proceeding with the deployment of Loki and Grafana.

  2. Deploy Loki:

    • Use a Kubernetes Deployment resource to deploy Loki.
    • Specify the necessary container image and configuration for Loki.
    • Create a ConfigMap to manage Loki’s configuration.
  3. Deploy Grafana:

    • Deploy Grafana using a Kubernetes Deployment resource.
    • Set the admin password through environment variables.
    • Ensure Grafana is configured to visualize logs collected by Loki.
  4. Configure Grafana to use Loki:

    • Set up Grafana to use Loki as a data source to visualize the logs.
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

// Define the Loki deployment
const lokiDeployment = new k8s.apps.v1.Deployment("loki-deployment", {
    spec: {
        selector: { matchLabels: { app: "loki" } },
        replicas: 1,
        template: {
            metadata: { labels: { app: "loki" } },
            spec: {
                containers: [{
                    name: "loki",
                    image: "grafana/loki:2.4.1",
                    ports: [{ containerPort: 3100 }],
                    args: [
                        "-config.file=/etc/loki/local-config.yaml"
                    ],
                    volumeMounts: [{
                        name: "config",
                        mountPath: "/etc/loki"
                    }]
                }],
                volumes: [{
                    name: "config",
                    configMap: {
                        name: "loki-config"
                    }
                }]
            }
        }
    }
});

// Define the Loki service
const lokiService = new k8s.core.v1.Service("loki-service", {
    spec: {
        selector: { app: "loki" },
        ports: [{ port: 3100, targetPort: 3100 }]
    }
});

// Define the Loki config map
const lokiConfigMap = new k8s.core.v1.ConfigMap("loki-config", {
    metadata: { name: "loki-config" },
    data: {
        "local-config.yaml": `
auth_enabled: false

server:
  http_listen_port: 3100

ingester:
  lifecycler:
    address: 127.0.0.1
    ring:
      kvstore:
        store: inmemory
    final_sleep: 0s
  chunk_idle_period: 5m
  chunk_retain_period: 30s
  max_transfer_retries: 0

schema_config:
  configs:
  - from: 2020-10-24
    store: boltdb-shipper
    object_store: filesystem
    schema: v11
    index:
      prefix: index_
      period: 168h

storage_config:
  boltdb_shipper:
    active_index_directory: /loki/index
    cache_location: /loki/index_cache
    shared_store: filesystem
  filesystem:
    directory: /loki/chunks

limits_config:
  enforce_metric_name: false
  reject_old_samples: true
  reject_old_samples_max_age: 168h

chunk_store_config:
  max_look_back_period: 0s

table_manager:
  retention_deletes_enabled: false
  retention_period: 0s
`
    }
});

// Define the Grafana deployment
const grafanaDeployment = new k8s.apps.v1.Deployment("grafana-deployment", {
    spec: {
        selector: { matchLabels: { app: "grafana" } },
        replicas: 1,
        template: {
            metadata: { labels: { app: "grafana" } },
            spec: {
                containers: [{
                    name: "grafana",
                    image: "grafana/grafana:8.3.3",
                    ports: [{ containerPort: 3000 }],
                    env: [{
                        name: "GF_SECURITY_ADMIN_PASSWORD",
                        value: "admin"
                    }]
                }]
            }
        }
    }
});

// Define the Grafana service
const grafanaService = new k8s.core.v1.Service("grafana-service", {
    spec: {
        selector: { app: "grafana" },
        ports: [{ port: 80, targetPort: 3000 }]
    }
});

// Export the URLs to access Grafana and Loki
export const grafanaUrl = grafanaService.status.loadBalancer.ingress[0].hostname;
export const lokiUrl = lokiService.status.loadBalancer.ingress[0].hostname;

Summary

In this guide, we successfully deployed Grafana and Loki on a Kubernetes cluster using Pulumi. We configured Loki to aggregate logs and set up Grafana to visualize these logs. The services were exposed, enabling easy access to both Grafana and Loki. This setup facilitates the creation of interactive log analysis dashboards, enhancing your ability to monitor and analyze system logs effectively.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up