本地安装 Grafana Loki

本地安装 Grafana Loki

  • [一、 安装 Loki](#一、 安装 Loki)
    • [1. 下载 Loki](#1. 下载 Loki)
    • [2. 创建 Loki 配置文件](#2. 创建 Loki 配置文件)
    • [3. 创建 Loki 服务](#3. 创建 Loki 服务)
  • [二、安装 Promtail](#二、安装 Promtail)
    • [1. 下载 Promtail](#1. 下载 Promtail)
    • [2. 创建 Promtail 配置文件](#2. 创建 Promtail 配置文件)
    • [3. 创建 Promtail 服务](#3. 创建 Promtail 服务)
  • [三、 安装 Grafana](#三、 安装 Grafana)
  • 四、启动所有服务
  • [五、添加loki 数据源](#五、添加loki 数据源)
    • [1. 添加仪表板](#1. 添加仪表板)
    • [2. 日志查询面板 json](#2. 日志查询面板 json)
  • 参考

一、 安装 Loki

1. 下载 Loki

bash 复制代码
# 下载 Loki
wget https://github.com/grafana/loki/releases/download/v3.4.2/loki-linux-amd64.zip
unzip loki-linux-amd64.zip

# 移动到合适的位置
sudo mv loki-linux-amd64 /usr/local/bin/loki

# 创建配置目录
sudo mkdir -p /etc/loki

2. 创建 Loki 配置文件

bash 复制代码
# 创建必要的目录
sudo mkdir -p /var/lib/loki/chunks
sudo mkdir -p /var/lib/loki/rules
sudo mkdir -p /var/lib/loki/index
sudo mkdir -p /var/lib/loki/cache 
bash 复制代码
vim /etc/loki/config.yaml
bash 复制代码
auth_enabled: false

server:
  http_listen_port: 3100

common:
  path_prefix: /loki
  storage:
    filesystem:
      chunks_directory: /var/lib/loki/chunks
      rules_directory: /var/lib/loki/rules
  replication_factor: 1
  ring:
    instance_addr: 127.0.0.1
    kvstore:
      store: inmemory

schema_config:
  configs:
    - from: 2020-10-24
      store: tsdb
      object_store: filesystem
      schema: v13
      index:
        prefix: index_
        period: 24h

storage_config:
  tsdb_shipper:
    active_index_directory: /var/lib/loki/index
    cache_location: /var/lib/loki/cache

ruler:
  alertmanager_url: http://localhost:9093
bash 复制代码
# 是否启用认证
auth_enabled: false

# 服务器配置
server:
  http_listen_port: 3100  # Loki API 监听端口

# 通用配置
common:
  path_prefix: /loki    # API 路径前缀
  storage:
    filesystem:         # 使用本地文件系统存储
      chunks_directory: /var/lib/loki/chunks  # 日志数据块存储目录
      rules_directory: /var/lib/loki/rules    # 告警规则存储目录
  replication_factor: 1   # 复制因子,1表示单实例
  ring:
    instance_addr: 127.0.0.1  # 实例地址
    kvstore:
      store: inmemory        # 使用内存存储作为键值存储

# schema 配置,定义如何存储和索引数据
schema_config:
  configs:
    - from: 2020-10-24      # 配置生效时间
      store: tsdb           # 使用 TSDB 存储引擎
      object_store: filesystem  # 使用文件系统作为对象存储
      schema: v13              # 使用 v13 版本的 schema
      index:
        prefix: index_         # 索引文件前缀
        period: 24h           # 索引周期,每24小时创建新的索引

# 存储配置
storage_config:
  tsdb_shipper:
    active_index_directory: /var/lib/loki/index  # 活跃索引目录
    cache_location: /var/lib/loki/cache          # 缓存位置

# 规则配置
ruler:
  alertmanager_url: http://localhost:9093  # Alertmanager 地址

3. 创建 Loki 服务

bash 复制代码
sudo tee /etc/systemd/system/loki.service<<EOF
[Unit]
Description=Loki service
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/local/bin/loki -config.file /etc/loki/config.yaml

[Install]
WantedBy=multi-user.target
EOF
bash 复制代码
systemctl daemon-reload
systemctl restart loki.service
systemctl status loki.service

systemctl enable loki.service

二、安装 Promtail

1. 下载 Promtail

bash 复制代码
# 下载 Promtail
wget https://github.com/grafana/loki/releases/download/v3.4.2/promtail-linux-amd64.zip
unzip promtail-linux-amd64.zip

# 移动到合适的位置
sudo mv promtail-linux-amd64 /usr/local/bin/promtail

# 创建配置目录
sudo mkdir -p /etc/promtail

2. 创建 Promtail 配置文件

bash 复制代码
vim /etc/promtail/config.yaml
bash 复制代码
server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /tmp/positions.yaml

clients:
  - url: http://localhost:3100/loki/api/v1/push

scrape_configs:
  - job_name: system
    static_configs:
      - targets:
          - localhost
        labels:
          job: varlogs
          __path__: /var/log/*log
  
  - job_name: docker
    static_configs:
      - targets:
          - localhost
        labels:
          job: docker
          __path__: /var/lib/docker/containers/*/*log

3. 创建 Promtail 服务

bash 复制代码
sudo tee /etc/systemd/system/promtail.service<<EOF
[Unit]
Description=Promtail service
After=network.target

[Service]	
Type=simple
User=root
ExecStart=/usr/local/bin/promtail -config.file /etc/promtail/config.yaml

[Install]
WantedBy=multi-user.target
EOF
bash 复制代码
systemctl daemon-reload
systemctl start promtail.service
systemctl status promtail.service

systemctl enable promtail.service

三、 安装 Grafana

四、启动所有服务

bash 复制代码
# 创建必要的目录
sudo mkdir -p /var/lib/loki/chunks
sudo mkdir -p /var/lib/loki/rules

# 启动服务
sudo systemctl daemon-reload
sudo systemctl enable loki promtail grafana-server
sudo systemctl start loki promtail grafana-server

# 检查服务状态
sudo systemctl status loki
sudo systemctl status promtail
sudo systemctl status grafana-server

五、添加loki 数据源

1. 添加仪表板

bash 复制代码
17514
https://grafana.com/grafana/dashboards/17514-ssh-logs/

2. 日志查询面板 json

bash 复制代码
{
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": {
          "type": "grafana",
          "uid": "-- Grafana --"
        },
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "type": "dashboard"
      }
    ]
  },
  "editable": true,
  "fiscalYearStartMonth": 0,
  "graphTooltip": 0,
  "id": 13,
  "links": [],
  "liveNow": false,
  "panels": [
    {
      "datasource": {
        "type": "loki",
        "uid": "cedpi2os8xbeoa"
      },
      "gridPos": {
        "h": 20,
        "w": 24,
        "x": 0,
        "y": 0
      },
      "id": 1,
      "options": {
        "dedupStrategy": "none",
        "enableLogDetails": true,
        "prettifyLogMessage": false,
        "showCommonLabels": false,
        "showLabels": false,
        "showTime": true,
        "sortOrder": "Descending",
        "wrapLogMessage": true
      },
      "targets": [
        {
          "datasource": {
            "type": "loki",
            "uid": "cedpi2os8xbeoa"
          },
          "editorMode": "builder",
          "expr": "{filename=~\"$log_file\"} |= \"$search_term\"",
          "queryType": "range",
          "refId": "A"
        }
      ],
      "title": "日志查询面板",
      "type": "logs"
    }
  ],
  "refresh": "1m",
  "schemaVersion": 39,
  "tags": [
    "logs",
    "monitoring"
  ],
  "templating": {
    "list": [
      {
        "current": {
          "selected": false,
          "text": "All",
          "value": "$__all"
        },
        "datasource": {
          "type": "loki",
          "uid": "cedpi2os8xbeoa"
        },
        "definition": "label_values(filename)",
        "hide": 0,
        "includeAll": true,
        "label": "日志文件",
        "multi": false,
        "name": "log_file",
        "options": [],
        "query": "label_values(filename)",
        "refresh": 1,
        "regex": "",
        "skipUrlSync": false,
        "sort": 1,
        "type": "query"
      },
      {
        "current": {
          "selected": false,
          "text": "",
          "value": ""
        },
        "hide": 0,
        "label": "搜索关键字",
        "name": "search_term",
        "options": [],
        "query": "",
        "skipUrlSync": false,
        "type": "textbox"
      }
    ]
  },
  "time": {
    "from": "now-1h",
    "to": "now"
  },
  "timepicker": {
    "refresh_intervals": [
      "5s",
      "10s",
      "30s",
      "1m",
      "5m",
      "15m",
      "30m",
      "1h",
      "2h",
      "1d"
    ]
  },
  "timezone": "",
  "title": "日志查询仪表板",
  "uid": "log_dashboard",
  "version": 3,
  "weekStart": ""
}

参考

  1. 本地安装 Grafana Loki
  2. loki releases
  3. SSH Logs
相关推荐
微风◝2 天前
4. grafana(7.5.17)功能菜单简介
grafana
陈译4 天前
Grafana——如何迁移Grafana到一台新服务器
运维·服务器·grafana
心灵宝贝4 天前
prometheus、grafana、windows、node exporter 安装包
grafana·prometheus
微风◝9 天前
3. 导入官方dashboard
grafana
陈译9 天前
Grafana-使用Button修改MySQL数据库
运维·数据库·mysql·grafana
葬爱家族小阿杰10 天前
Jmeter+Influxdb+Grafana平台监控性能测试过程
jmeter·grafana
葬爱家族小阿杰10 天前
Prometheus+Grafana+Jmeter监控服务器资源及中间件
jmeter·grafana·prometheus
日后定负责11 天前
使用grafana v11 建立k线(蜡烛图)仪表板
grafana
微风◝11 天前
2. grafana插件安装并接入zabbix
前端·zabbix·grafana