docker搭建grafana+loki+promtail日志收集系统

docker搭建grafana+loki+promtail日志收集系统1.拉取镜像
docker pull grafana/grafana:7.4.3
docker pull grafana/promtail:2.1.0
docker pull grafana/loki:2.1.0

2.loki搭建

1)新建配置文件

创建文件:

mkdir -p /usr/local/docker/loki

配置文件: vi /usr/local/docker/loki/loki-config.yaml

auth_enabled: false

server:
  http_listen_port: 3100
  grpc_listen_port: 3110
  grpc_server_max_recv_msg_size: 1073741824  #grpc最大接收消息值,默认4m
  grpc_server_max_send_msg_size: 1073741824  #grpc最大发送消息值,默认4m

ingester:
  lifecycler:
    address: 127.0.0.1
    ring:
      kvstore:
        store: inmemory
      replication_factor: 1
    final_sleep: 0s
  chunk_idle_period: 5m
  chunk_retain_period: 30s
  max_transfer_retries: 0
  max_chunk_age: 20m  #一个timeseries块在内存中的最大持续时间。如果timeseries运行的时间超过此时间,则当前块将刷新到存储并创建一个新块

schema_config:
  configs:
    - from: 2021-01-01
      store: boltdb
      object_store: filesystem
      schema: v11
      index:
        prefix: index_
        period: 168h

storage_config:
  boltdb:
    directory: /opt/loki/index #存储索引地址
  filesystem:
    directory: /opt/loki/chunks

limits_config:
  enforce_metric_name: false
  reject_old_samples: true
  reject_old_samples_max_age: 168h
  ingestion_rate_mb: 30  #修改每用户摄入速率限制,即每秒样本量,默认值为4M
  ingestion_burst_size_mb: 15  #修改每用户摄入速率限制,即每秒样本量,默认值为6M

chunk_store_config:
        #max_look_back_period: 168h   #回看日志行的最大时间,只适用于即时日志
  max_look_back_period: 0s

table_manager:
  retention_deletes_enabled: false #日志保留周期开关,默认为false
  retention_period: 0s  #日志保留周期

2)启动容器

创建文件和授权

mkdir -p /usr/local/docker/loki/index
mkdir -p /usr/local/docker/loki/chunks
chmod -R 777 /usr/local/docker/loki/index
chmod -R 777 /usr/local/docker/loki/chunkscd 

启动

docker run -d \
--name loki \
--privileged=true \
-v /usr/local/docker/loki:/mnt/config \
-v /usr/local/docker/loki/index:/opt/loki/index \
-v /usr/local/docker/loki/chunks:/opt/loki/chunks \
-p 3100:3100 \
grafana/loki:2.1.0 -config.file=/mnt/config/loki-config.yaml

3.promtail搭建

1)新建配置文件

创建文件:

mkdir -p /usr/local/docker/promtail

配置文件: vi /usr/local/docker/promtail/promtail-config.yaml

server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /tmp/positions.yaml

#把loki当客户端连接
clients:
  - url: http://lokiIP:3100/loki/api/v1/push

scrape_configs:
 - job_name: system
   pipeline_stages:
   static_configs:
   - targets:
      - localhost
     labels:
      #标签,用于后面的查询
      job: joyoh
      __path__: /var/log/*.log
 - job_name: system
   pipeline_stages:
   static_configs:
   - targets:
      - localhost
     labels:
     #标签,用于后面的查询
      job: test
      __path__: /var/test/*.log

2)启动容器

docker run -d \
--name promtail \
--privileged=true \
-v /usr/local/docker/promtail:/mnt/config \
-v /usr/local/joyoh/log:/var/log \
-v /usr/local/joyoh/test:/var/test \
grafana/promtail:2.1.0 -config.file=/mnt/config/promtail-config.yaml

注意:日志文件目录要挂载在容器上,不然获取不到宿主机的日志文件

4.grafana搭建

1)启动容器

docker run -d \
--name grafana \
-p 3000:3000 \
grafana/grafana:7.4.3

2)访问

http://localhost:3000/explore

标签