> For AI agents: the complete documentation index is available at https://www.tteam.icu/llms.txt, the full documentation bundle is available at https://www.tteam.icu/llms-full.txt.

# loki

Grafana 和 Loki 属于同一家公司开发的产品。

## 目录示例

```tree
.
├── config
│   └── loki-config.yaml
└── docker-compose.yml
```

## 启动


**docker**

```bash file="./loki/setup.sh"
#!/bin/bash

name=loki
mkdir -p loki-data/
chown -R 10001:10001 loki-data/
docker rm -f ${name}

docker run -d \
  --name ${name} \
  --network host \
  --restart always \
  -v /etc/timezone:/etc/timezone:ro \
  -v /etc/localtime:/etc/localtime:ro \
  -v $(pwd)/config/loki-config.yaml:/etc/config/loki-config.yaml:ro \
  -v $(pwd)/loki-data:/loki \
  --log-driver json-file \
  --log-opt max-size=10m \
  --log-opt max-file=3 \
  --cpus 7 \
  --memory 12g \
  grafana/loki:3.7.3 \
  -config.file=/etc/config/loki-config.yaml -target=all,table-manager
```


**docker-compose**

```yml file="./loki/docker-compose.yml"
version: "3"

services:
  loki:
    container_name: loki
    image: grafana/loki:3.5.8
    volumes:
      - /etc/timezone:/etc/timezone
      - /etc/localtime:/etc/localtime
      - ./config/loki-config.yaml:/etc/config/loki-config.yaml
      - ./loki-data:/loki/data
    network_mode: host
    command: -config.file=/etc/config/loki-config.yaml -target=all,table-manager
    restart: always
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
    deploy:
      resources:
        limits:
          cpus: '3'
          memory: 4G
```



**本地硬盘配置**

```yaml file="./loki/loki-config.yaml"
auth_enabled: false

server:
  http_listen_port: 3100
  grpc_server_max_recv_msg_size: 1048576000
  grpc_server_max_send_msg_size: 1048576000

query_scheduler:
  grpc_client_config:
    max_send_msg_size: 1048576000

ingester_client:
  grpc_client_config:
    max_send_msg_size: 1048576000

common:
  ring:
    instance_addr: 127.0.0.1
    kvstore:
      store: inmemory
  replication_factor: 1
  path_prefix: /loki

ingester:
  wal:
    enabled: true
    # 允许写入WAL
    dir: /loki/data/wal
    replay_memory_ceiling: 1GB
  lifecycler:
    address: 127.0.0.1
    ring:
      kvstore:
        store: inmemory
      replication_factor: 1
    final_sleep: 0s
  chunk_idle_period: 5m
  chunk_retain_period: 30s

schema_config:
  configs:
    - from: 2020-10-24
      store: tsdb
      object_store: filesystem
      schema: v13
      index:
        prefix: index_
        period: 24h

limits_config:
  # enforce_metric_name: false
  reject_old_samples: true
  reject_old_samples_max_age: 168h
  # 令牌桶注入token的速率
  # ingestion_rate_mb: | default = 4]
  ingestion_rate_mb: 10240
  # 令牌桶的容量
  # ingestion_burst_size_mb: | default = 6]
  ingestion_burst_size_mb: 1024
  # 每个租户的最大日志流个数
  # max_streams_per_user: | default = 10000
  # 1000w
  max_streams_per_user: 10000000
  max_global_streams_per_user: 1000000
  # label的key最大长度
  # max_label_name_length:   | default = 1024
  max_label_name_length: 1024
  # label的value最大长度
  # max_label_value_length:  | default = 2048
  max_label_value_length: 204800
  # 每个流中的最大label个数
  # max_label_names_per_series: | default = 30
  max_label_names_per_series: 300
  # 单个查询最多匹配的chunk个数
  # max_chunks_per_query: | default = 2000000
  # 限制查询是匹配到的chunk大小，默认0为不限制
  # max_query_length:  | default = 0
  # 单词查询最多匹配到的日志流个数
  # max_streams_matchers_per_query: | default = 1000
  max_streams_matchers_per_query: 10000
  # 限制查询时最大的日志度量个数
  # max_query_series: | default = 500
  max_query_series: 10000
  # 查询的并发数
  # max_query_parallelism  | default = 14
  # 允许租户缓存结果的有效时间
  # max_cache_freshness_per_query   |default = 1m.
  # max_cache_freshness_per_query: 5m

# 允许删除日志
table_manager:
  retention_deletes_enabled: true
  retention_period: 168h
```


**s3配置**

```yaml file="./loki/loki-config-s3.yaml"
auth_enabled: false

server:
  http_listen_port: 3100
  grpc_server_max_recv_msg_size: 1048576000
  grpc_server_max_send_msg_size: 1048576000

query_scheduler:
  grpc_client_config:
    max_send_msg_size: 1048576000

ingester_client:
  grpc_client_config:
    max_send_msg_size: 1048576000

common:
  ring:
    instance_addr: 127.0.0.1
    kvstore:
      store: inmemory
  replication_factor: 1
  path_prefix: /loki

schema_config:
  configs:
  - from: 2020-05-15
    store: tsdb
    object_store: s3
    schema: v13
    index:
      prefix: index_
      period: 24h

storage_config:
  tsdb_shipper:
    active_index_directory: /loki/index
    cache_location: /loki/index_cache
  aws:
    s3: s3://xxx:xxx@oss.com/bucket
    s3forcepathstyle: false

limits_config:
  # enforce_metric_name: false
  reject_old_samples: true
  reject_old_samples_max_age: 168h
  # 令牌桶注入token的速率
  # ingestion_rate_mb: | default = 4]
  ingestion_rate_mb: 10240
  # 令牌桶的容量
  # ingestion_burst_size_mb: | default = 6]
  ingestion_burst_size_mb: 1024
  # 每个租户的最大日志流个数
  # max_streams_per_user: | default = 10000
  # 1000w
  max_streams_per_user: 10000000
  max_global_streams_per_user: 1000000
  # label的key最大长度
  # max_label_name_length:   | default = 1024
  max_label_name_length: 1024
  # label的value最大长度
  # max_label_value_length:  | default = 2048
  max_label_value_length: 204800
  # 每个流中的最大label个数
  # max_label_names_per_series: | default = 30
  max_label_names_per_series: 300
  # 单个查询最多匹配的chunk个数
  # max_chunks_per_query: | default = 2000000
  # 限制查询是匹配到的chunk大小，默认0为不限制
  # max_query_length:  | default = 0
  # 单词查询最多匹配到的日志流个数
  # max_streams_matchers_per_query: | default = 1000
  max_streams_matchers_per_query: 10000
  # 限制查询时最大的日志度量个数
  # max_query_series: | default = 500
  max_query_series: 10000
  # 查询的并发数
  # max_query_parallelism  | default = 14
  # 允许租户缓存结果的有效时间
  # max_cache_freshness_per_query   |default = 1m.
  # max_cache_freshness_per_query: 5m

# 允许删除日志
table_manager:
  retention_deletes_enabled: true
  retention_period: 168h
```


:::tip
以上是将日志存储在本地的配置，如果需要使用 OSS 存储，需要修改相应配置。
:::

:::tip
如果需要删除部分数据，需要在配置中加入以下配置，`<delete_request_store>` 为要删除的存储介质:

```yaml
compactor:
  retention_enabled: true
  delete_request_store: filesystem
```

重启后调用以下命令:

```bash
curl -g -X POST \
  'http://127.0.0.1:3100/loki/api/v1/delete?query={foo="bar"}&start=1733122200&end=1733122800' \
  -H 'X-Scope-OrgID: 1'
```

:::

## 一键启动脚本

::: details docker-compose.yml

```yml file="./loki/integration/docker-compose.yml" title="docker-compose.yml"
version: "3"

networks:
  loki:

services:
  loki:
    image: grafana/loki:3.1.0
    ports:
      - "3100:3100"
    volumes:
      - ./loki-config/:/mnt/config/
      - ./loki-tmp:/tmp/loki
    command: -config.file=/etc/loki/local-config.yaml
    networks:
      - loki

  grafana:
    environment:
      - GF_PATHS_PROVISIONING=/etc/grafana/provisioning
    entrypoint:
      - sh
      - -euc
      - |
        mkdir -p /etc/grafana/provisioning/datasources
        cat <<EOF > /etc/grafana/provisioning/datasources/ds.yaml
        apiVersion: 1
        datasources:
        - name: Loki
          type: loki
          access: proxy 
          orgId: 1
          url: http://loki:3100
          basicAuth: false
          isDefault: true
          version: 1
          editable: false
        EOF
        /run.sh
    image: grafana/grafana:11.1.0
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - ./plugins/:/var/lib/grafana/plugins
    ports:
      - "3000:3000"
    networks:
      - loki

```

:::

## Docker 插件

```bash
docker plugin install grafana/loki-docker-driver:2.8.0 --alias loki --grant-all-permissions
```
