本地部署开源日志聚合系统 Loki 并实现外部访问( Windows 版本)

Loki 是一款高效可扩展的开源日志聚合系统,由 Grafana Labs 创建,它的核心理念是只索引日志的元数据(如标签)而非全文,从而以极低的存储成本和极高的查询性能,像 Prometheus 监控指标一样轻松地处理和探索海量日志数据。本文将详细介绍如何在 Windows 系统本地部署 Loki 并结合路由侠实现外网访问本地部署的 Loki 。

第一步,本地部署安装 Loki

1,本教程操作环境为 Windows11 系统,首先访问 Loki 的 GitHub 发布页面,找到最新的版本,下载适用于 Windows 的 Loki 和 Promtail 压缩包,点此下载,下载完成后将这两个 ZIP 文件解压到项目目录,如 D:\Software\loki 。

2,在项目目录 D:\Software\loki 中,创建一个名为 loki-local-config.yaml 的文件,写入以下基础的 Loki 配置内容,确保 D:\Software\temp、D:\Software\loki\chunks 和 D:\Software\loki\rules 等目录真实存在,不存在可手动创建。

auth_enabled: false

server:
  http_listen_port: 3100
  grpc_listen_port: 9096

common:
  instance_addr: 127.0.0.1
  path_prefix: D:\Software\temp
  storage:
    filesystem:
      chunks_directory: D:\Software\chunks
      rules_directory: D:\Software\rules
  replication_factor: 1
  ring:
    kvstore:
      store: inmemory

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

ruler:
  alertmanager_url: http://localhost:9093

limits_config:
  reject_old_samples: true
  reject_old_samples_max_age: 168h

4,打开命令提示符( CMD ) 或 PowerShell ,导航到 Loki 的项目目录。

cd /d D:\Software\loki

使用配置文件启动 Loki 服务。

.\loki-windows-amd64.exe --config.file=loki-local-config.yaml

5,一切正常终端开始输出日志,保持这个窗口打开,打开浏览器访问地址 http://localhost:3100/ready ,若返回 ready ,证明 Loki 已经开始正常工作了。

6,为了有数据可看,我们可以配置 Promtail 来采集本地日志。

在项目目录 D:\Software\loki 中,创建一个名为 promtail-local-config.yaml 的文件,写入以下基础的 Promtail 配置内容,手动创建 D:\test-logs 目录,并在里面创建一个 application.log 文件,随便写入一些文本内容,例如“ Hello, this is a test log entry. ”。

server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: D:\Software\loki\positions.yaml

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

scrape_configs:
- job_name: system
  static_configs:
  - targets:
      - localhost
    labels:
      job: varlogs
      __path__: D:\test-logs\*.log

7,打开另一个命令提示符或 PowerShell 窗口,导航到项目目录。

cd /d D:\Software\loki

使用配置文件启动 Promtail 服务,Promtail 会开始监视 D:\test-logs\application.log 并将内容发送给 Loki 。

.\promtail-windows-amd64.exe --config.file=promtail-local-config.yaml

8,在浏览器访问地址 http://localhost:3100/metrics ,就能看到你部署的 Loki 的内部监控指标数据。

第二步,外网访问本地 Loki

在内网的电脑上安装路由侠,点此下载

1,下载安装完成后,打开路由侠界面,点击【内网映射】。

2,点击【添加映射】。

3,选择【原生端口】。

4,在内网端口填写 Loki 端口 3100 后点击【创建】按钮,如下图。

5,创建好后,就可以看到一条映射的公网地址,鼠标右键点击【复制地址】。

6,在外网电脑上,打开浏览器,在地址栏输入从路由侠生成的外网地址加上 /ready ,就可以看到内网部署的 Loki 正常工作了。

在地址栏输入从路由侠生成的外网地址加上 /metrics ,就可以看到内网部署的 Loki 的内部监控指标数据。

以上就是 Windows 系统在本地部署 Loki 服务以及利用路由侠的内网穿透平台实现外网访问本地部署的 Loki 的全部教程了,如果想要对路由侠有更多的了解,可以进官网详细了解,路由侠官网地址:www.luyouxia.com