Loki 安装部署

发布时间: 更新时间: 总字数:1835 阅读时间:4m 作者:IP:上海 网址

介绍常见的 Loki 安装部署方式

部署模式

  • 参考
  • Loki 程序基于微服务架构设计,有多个组件构成,常见的部署模块包括:
    • all 单体模式:将所有微服务组件打包部署到单一进程中
      • 适合小规模系统的日志存储场景(每天不错过 100G)
      • 必要时,可部署共享外部对象存储来水平扩展
        • 在配置文件 loki.yaml 的 ring 配置 secion 中定义日志数据的跨实例分发
      • 支持高可用的部署方式
        • 多个实例需要配置共享的外部对象存储
        • 需要设定合理的复制因子
    • individual 微服务模式
    • simple scalable deployment 模式包括 read, write, backend
Componentindividualallreadwritebackend
Distributorxxx
Ingesterxxx
Query Frontendxxx
Query Schedulerxxx
Querierxxx
Index Gatewayxx
Compactorxxx
Rulerxxx
Bloom Compactor (Experimental)xx
Bloom Gateway (Experimental)xx

单体模式(all)

  • 参考
  • 环境依赖
  • 部署的组件
    • Loki Server
      • 3100 read/write
      • 7946
      • 9095
    • Grafana
      • 3000
    • Promtail
    • MinIO
      • 9000 api
      • 9001 console, 账号信息:loki/supersecret
    • gateway
      • 3100
    • Flog 专用于生成 http 服务访问日志示例
bash
# 1. Create a directory called evaluate-loki
mkdir evaluate-loki && cd evaluate-loki

# 2. Download loki-config.yaml, promtail-local-config.yaml, and docker-compose.yaml
wget https://raw.githubusercontent.com/grafana/loki/v2.9.8/examples/getting-started/loki-config.yaml -O loki-config.yaml
wget https://raw.githubusercontent.com/grafana/loki/v2.9.8/examples/getting-started/promtail-local-config.yaml -O promtail-local-config.yaml
wget https://raw.githubusercontent.com/grafana/loki/v2.9.8/examples/getting-started/docker-compose.yaml -O docker-compose.yaml

# 3. Deploy the sample Docker image
docker compose up -d

$ docker compose ps
NAME                                     IMAGE                           COMMAND                  SERVICE                  CREATED              STATUS                             PORTS
evaluate-loki-gateway-1                  nginx:latest                    "sh -euc 'cat <<EOF …"   gateway                  About a minute ago   Up 17 seconds (healthy)            80/tcp, 0.0.0.0:3100->3100/tcp, :::3100->3100/tcp
evaluate-loki-grafana-1                  grafana/grafana:10.2.6-ubuntu   "sh -euc 'mkdir -p /…"   grafana                  About a minute ago   Up 16 seconds (health: starting)   0.0.0.0:3000->3000/tcp, :::3000->3000/tcp
evaluate-loki-loggen-apache-combined-1   mingrammer/flog:0.4.3           "flog -f apache_comb…"   loggen-apache-combined   About a minute ago   Up 21 seconds
evaluate-loki-loggen-apache-common-1     mingrammer/flog:0.4.3           "flog -f apache_comm…"   loggen-apache-common     About a minute ago   Up 21 seconds
evaluate-loki-loggen-json-1              mingrammer/flog:0.4.3           "flog --loop --forma…"   loggen-json              About a minute ago   Up 21 seconds
evaluate-loki-minio-1                    minio/minio                     "sh -euc 'mkdir -p /…"   minio                    About a minute ago   Up 21 seconds (healthy)            0.0.0.0:9001->9001/tcp, :::9001->9001/tcp, 0.0.0.0:32800->9000/tcp, :::32800->9000/tcp
evaluate-loki-promtail-1                 grafana/promtail:2.9.8          "/usr/bin/promtail -…"   promtail                 About a minute ago   Up 16 seconds                      0.0.0.0:32805->9080/tcp, :::32805->9080/tcp
evaluate-loki-read-1                     grafana/loki:2.9.8              "/usr/bin/loki -conf…"   read                     About a minute ago   Up 18 seconds (health: starting)   0.0.0.0:3101->3100/tcp, :::3101->3100/tcp, 0.0.0.0:32803->7946/tcp, :::32803->7946/tcp, 0.0.0.0:32801->9095/tcp, :::32801->9095/tcp
evaluate-loki-write-1                    grafana/loki:2.9.8              "/usr/bin/loki -conf…"   write                    About a minute ago   Up 18 seconds (health: starting)   0.0.0.0:3102->3100/tcp, :::3102->3100/tcp, 0.0.0.0:32804->7946/tcp, :::32804->7946/tcp, 0.0.0.0:32802->9095/tcp, :::32802->9095/tcp

# 4. (Optional) Verify that the Loki cluster is up and running
$ curl http://localhost:3101/ready
ready
$ curl http://localhost:3102/ready
ready

# 监控指标
curl http://localhost:3101/metrics
curl http://localhost:3102/metrics

# 配置
curl http://localhost:3101/config
curl http://localhost:3102/config

# 当前Memberlist Status、KV Store、Memberlist Cluster Members、Message History
http://localhost:3101/memberlist
http://localhost:3102/memberlist

# 5. (Optional) Verify that Grafana Alloy is running.
$ curl http://localhost:3000
loki-config.yaml ...
promtail-local-config.yaml ...
docker-compose.yaml ...
  • loki 也可以合并为一个服务,参考
yaml
  loki:
    image: grafana/loki:2.9.8
    command: "-config.file=/etc/loki/config.yaml -target=all"
    ports:
      - 3100:3100
      - 7946
      - 9095
    volumes:
      - ./loki-config.yaml:/etc/loki/config.yaml
    depends_on:
      - minio
    healthcheck:
      test: [ "CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:3100/ready || exit 1" ]
      interval: 10s
      timeout: 5s
      retries: 5

grafana

  • grafana 默认配置了 loki 的 datasource
  • 访问 http://localhost:3000 打开 grafana,在 Home -> Connections -> Configuration -> Data sources 可以看到 loki 已经配置
    • 点击 loki 进入配置页,点击 Test 测试成功
    • 点击左侧的 Explore 进入 logQL 编写页面,支持两种模式
      • Builder logQL 构建器
      • Code 手动写 logQL

minio

  • 访问 127.0.0.1:9001 账号信息:loki/supersecret
  • 默认创建 2 个 bucket
    • loki-data
    • loki-ruler

模拟日志

使用 mingrammer/flog 生成了模拟日志,直接参考了马哥的配置,生成的文件:

bash
ls loki/generated-logs.txt
loki/generated-logs.txt

使用 Docker 安装 loki

  • 可选,测试目的,参考
bash
mkdir loki
cd loki

wget https://raw.githubusercontent.com/grafana/loki/v2.9.2/cmd/loki/loki-local-config.yaml -O loki-config.yaml
wget https://raw.githubusercontent.com/grafana/loki/v2.9.2/clients/cmd/promtail/promtail-docker-config.yaml -O promtail-config.yaml

docker run --name loki -d -v $(pwd):/mnt/config -p 3100:3100 grafana/loki:2.9.2 -config.file=/mnt/config/loki-config.yaml
docker run --name promtail -d -v $(pwd):/mnt/config -v /var/log:/var/log --link loki grafana/promtail:2.9.2 -config.file=/mnt/config/promtail-config.yaml

Helm

  • https://grafana.com/docs/loki/latest/setup/install/helm/
  • https://grafana.github.io/helm-charts
  • 集中常见的 helm 部署模式
    • grafana/loki-stack 单体模式
    • grafana/loki-canary 金丝雀
    • grafana/loki-distributed 分布式;微服务模式,适合生产较大规模场景
    • grafana/loki-simple-scalable 简单可扩展,读写分离模式
  • 注意:Loki 相关 chart 已从 grafana 仓库(grafana.github.io/helm-charts)迁移至 grafana-community 仓库,原仓库已不再收录 loki chart(alloy 等仍保留在原仓库):
bash
helm repo add grafana-community https://grafana-community.github.io/helm-charts
helm repo update

# 导出全量默认配置,按需修改后安装
helm show values grafana-community/loki > values.default.yaml
helm install loki grafana-community/loki -n logging -f values.yaml --create-namespace

配置