【云原生】Loki + Fluentd

69 阅读2分钟

一、Loki

1.什么是Loki

收集 存储 查询日志的一个服务

2.Loki的全拼是什么

没有全拼 是北欧神话中的一个人物 恶作剧之神 Loki,擅长制造变化和混乱,符合日志这种信息杂乱,需要解析的特点

3.Loki如何存储数据

把日志内容压缩保存起来,再单独保存一份标签索引用来查找日志

4.索引标签怎么写

{job="nginx", host="server-1", level="error"}

5.如何将数据存在Loki

通过http的话是这样的:

curl -X POST http://localhost:3100/loki/api/v1/push -H "Content-Type: application/json" -d '{
  "streams": [
    {
      "stream": {"job": "test", "level": "info"},
      "values": [["1728912000000000000", "Hello Loki!"]]
    }
  ]
}'

6.如何与Grananfa结合

1.用Docker 部署Loki
2.在Grananfa中添加Loki数据源
3.Granafa会提供Loki查询日志界面
4.Loki存数据 然后用Grafana展示

二、Fluentd

1.Fluentd是什么

处理 转发日志

2.Fluentd如何使用

1.写Fluentd的配置文件(源和输出)
2.启动Flutentd
3.写日志到文件后,Flutend自动收集

3.Fluentd就可以收集日志,为什么还要传给Loki

fluentd不存储日志 只是收集 转发日志
fluentd将日志发送到Loki

三、Loki+Fluentd+Grafana实践

1.写 docker-compose

version: "3.8"

services:
  # Loki 日志存储
  loki:
    image: grafana/loki:2.8.2
    container_name: loki
    command: -config.file=/etc/loki/local-config.yaml
    volumes:
      - ./loki-config.yaml:/etc/loki/local-config.yaml
      - ./loki-data/index:/loki/index
      - ./loki-data/chunks:/loki/chunks
    ports:
      - "3100:3100"

  # Grafana 可视化
  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    environment:
      GF_SECURITY_ADMIN_USER: admin
      GF_SECURITY_ADMIN_PASSWORD: admin
    ports:
      - "3000:3000"
    depends_on:
      - loki

  # Fluentd 日志收集
  fluentd:
    image: fluent/fluentd:v1.16-debian-1
    container_name: fluentd
    volumes:
      - ./fluentd.conf:/fluentd/etc/fluentd.conf
    ports:
      - "24224:24224"
    depends_on:
      - loki

  # 模拟应用产生日志
  app:
    image: busybox
    container_name: log_app
    command: sh -c "while true; do echo 'hello loki'; sleep 2; done"

2.写fluentd的配置

<source>
  @type forward
  port 24224
  bind 0.0.0.0
</source>

<match **>
  @type loki
  url "http://loki:3100/loki/api/v1/push"
  tenant_id "default"
  label_keys ["job","container"]
</match>

3.写Loki的配置

auth_enabled: false

server:
  http_listen_port: 3100

ingester:
  wal:
    enabled: true
  chunk_idle_period: 5m
  max_chunk_age: 1h

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

storage_config:
  boltdb_shipper:
    active_index_directory: /loki/index
    shared_store: filesystem
  filesystem:
    directory: /loki/chunks

limits_config:
  enforce_metric_name: false
  reject_old_samples: true
  reject_old_samples_max_age: 168h

chunk_store_config:
  max_look_back_period: 0s

table_manager:
  retention_deletes_enabled: true
  retention_period: 168h