当前位置: 首页 > news >正文

本地安装 Grafana Loki

本地安装 Grafana Loki

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

一、 安装 Loki

1. 下载 Loki

# 下载 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 配置文件

# 创建必要的目录
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 
vim /etc/loki/config.yaml
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
# 是否启用认证
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 服务

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
systemctl daemon-reload
systemctl restart loki.service
systemctl status loki.service

systemctl enable loki.service

二、安装 Promtail

1. 下载 Promtail

# 下载 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 配置文件

vim /etc/promtail/config.yaml
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 服务

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
systemctl daemon-reload
systemctl start promtail.service
systemctl status promtail.service

systemctl enable promtail.service

三、 安装 Grafana

四、启动所有服务

# 创建必要的目录
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. 添加仪表板

17514
https://grafana.com/grafana/dashboards/17514-ssh-logs/

在这里插入图片描述

2. 日志查询面板 json

{
  "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

相关文章:

  • HTTP SSE 实现
  • RabbitMq 基础
  • 贪心算法
  • 前端面试真题 2025最新版
  • ecovadis社会企业责任认证
  • C++面试笔记(持续更新...)
  • Transformer解析——(四)Decoder
  • Modbus协议基础
  • AWS云从业者认证题库 AWS Cloud Practitioner(2.21)
  • 【练习】【回溯:组合:一个集合 元素可重复】力扣 39. 组合总和
  • 如何实现使用DeepSeek的CV模型对管道内模糊、低光照或水渍干扰的图像进行去噪、超分辨率重建。...
  • 推理模型时代:大语言模型如何从对话走向深度思考?
  • java后端开发day18--学生管理系统
  • 多门店协同管理困难重重,管理系统如何破局?
  • MySQL 中的回表是什么?MySQL 中使用索引一定有效吗?如何排查索引效果?在 MySQL 中建索引时需要注意哪些事项?
  • matlab 轮边驱动系统汽车垂向动力学分析
  • NVM是什么,以及NVM的作用?
  • 代码讲解系列-CV(六)——视觉生成模型
  • Unity学习笔记-Unity了解,安装,简单配置(一)
  • Trae AI驱动开发实战:30分钟从0到1实现Django REST天气服务
  • 用vs2010做网站并连数据库/百度营销登录
  • 社交类网站开发需求/十大搜索引擎
  • 海淘网站是谁做的/网站指数查询
  • 网站手机端做排名/电商seo优化
  • 网站建设深圳哪家公司好/seo推广软件
  • 网站建设实训日志/泉州seo网站排名