本地安装 Grafana Loki

news/2025/2/25 3:33:11

本地安装 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__226">五、添加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://www.niftyadmin.cn/n/5864950.html

相关文章

一篇docker从入门到精通

Docker Docker 是一个开源的应用容器引擎&#xff0c;让开发者可以打包他们的应用以及依赖包到一个可移植的容器中&#xff0c;然后发布到任何流行的 Linux 机器上&#xff0c;也可以实现虚拟化。容器是完全使用沙盒机制&#xff0c;相互之间不会有任何接口&#xff08;类似 iP…

Vulhub靶机 Apache Druid(CVE-2021-25646)(渗透测试详解)

一、开启vulhub环境 docker-compose up -d 启动 docker ps 查看开放的端口 1、漏洞范围 在Druid0.20.0及更低版本中 二、访问靶机IP 8888端口 1、点击Load data进入新界面后&#xff0c;再点击local disk按钮。 2、进入新界面后&#xff0c;在标红框的Base directory栏写上…

Open WebUI本地部署教程

文章目录 1、系统环境配置2、源码下载2.1 github源码地址下载 3、环境启动3.1 后端环境3.2 前端环境 4、问题4.1 浏览器跨域问题4.2 all-MiniLM-L6-v2模型文件下载失败问题4.3 单独部署backend启动报错问题 1、系统环境配置 操作系统&#xff1a;windows/linux/macos Python版…

【NLP算法面经】本科双非,头条+腾讯 NLP 详细面经(★附面题整理★)

【NLP算法面经】本科双非&#xff0c;头条腾讯 NLP 详细面经&#xff08;★附面题整理★&#xff09; &#x1f31f; 嗨&#xff0c;你好&#xff0c;我是 青松 &#xff01; &#x1f308; 自小刺头深草里&#xff0c;而今渐觉出蓬蒿。 NLP Github 项目推荐&#xff1a; 【AI…

工程师 - VSCode的AI编码插件介绍: MarsCode

豆包 MarsCode MarsCode AI: Coding Assistant Code and Innovate Faster with AI 豆包 MarsCode - 编程助手 安装完成并使能后&#xff0c;会在下方状态栏上显示MarsCode AI。 安装完并重启VSCode后&#xff0c;要使用这个插件&#xff0c;需要注册一下账号。然后授权VSCod…

多人协同创作gitea

多人协同创作gitea 在多台设备上协同使用Gitea&#xff0c;主要是通过网络访问Gitea服务器上的仓库来进行代码管理和协作。以下是一些关键步骤和建议&#xff0c;帮助你在多台设备上高效地使用Gitea进行协作&#xff1a; 1. 确保Gitea服务可访问 首先&#xff0c;你需要确保…

JAVA版本游戏进程读写操作

1.导入游戏进程读写Maven依赖 <dependency><groupId>io.github.2lius</groupId><artifactId>MemoryProcess</artifactId><version>0.1</version></dependency> GitHub地址 2.代码操作游戏读写内存 package com.lius.test;impo…

Linux 权限系统和软件安装(二):深入理解 Linux 权限系统

在 Linux 的世界里&#xff0c;权限系统犹如一位忠诚的卫士&#xff0c;严密守护着系统中的文件与目录&#xff0c;确保只有具备相应权限的用户才能进行操作。与其他一些操作系统不同&#xff0c;Linux 并不依据文件后缀名来标识文件的操作权限&#xff0c;而是构建了一套独特且…