其他分享
首页 > 其他分享> > docker-compose安装grafana+prometheus+loki 监控docker容器收集服务日志

docker-compose安装grafana+prometheus+loki 监控docker容器收集服务日志

作者:互联网

使用docker-compose部署springboot项目后想找一个比较好用的日志收集工具,用elk感觉太重了,小项目承担不起,无意中发现loki,与grafana集成度很高,最重要的是使用成本低,记录下安装过程。

创建grafana,prometheus文件夹及配置文件

# 创建grafana文件夹
mkdir /docker/grafana/grafana
mkdir /docker/grafana/grafana/data

# 创建prometheus文件夹
mkdir /docker/grafana/prometheus
mkdir /docker/grafana/prometheus/data
mkdir /docker/grafana/prometheus/log

# 创建prometheus配置文件
vi /docker/grafana/prometheus/prometheus.yml
global:
  scrape_interval:     15s
  evaluation_interval: 15s

# 报警暂时不需要,后续可以添加
#alerting:
#  alertmanagers:
#  - static_configs:
#    - targets:
#      - alertmanager:9093

rule_files:
  - "*rules.yml"

scrape_configs:
  # prometheus
  - job_name: 'prometheus'
    static_configs:
    - targets: ['host-ip:9090']
  # 主机监控
  - job_name: 'node'
    static_configs:
    - targets: ['host-ip:9100']
    
  # docker资源监控
  - job_name: 'cadvisor'
    static_configs:
    - targets: ['host-ip:8080']
    
# springboot 项目监控
# springboot 监控需要项目中集成prometheus
#  - job_name: 'test-server'
#    metrics_path: '/actuator/prometheus'
#    static_configs:
#    - targets: ['tesete-server-ip:8081']

#  - job_name: 'alertmanager'
#    static_configs:
#    - targets: ['alertmanager:9093']

创建loki 文件夹及配置文件

mkdir /docker/grafana/loki
mkdir /docker/grafana/loki/chunks
mkdir /docker/grafana/loki/rules

vi /docker/grafana/loki/local-config.yaml
auth_enabled: false

server:
  http_listen_port: 3100

common:
  path_prefix: /loki
  storage:
    filesystem:
      chunks_directory: /loki/chunks
      rules_directory: /loki/rules
  replication_factor: 1
  ring:
    instance_addr: 127.0.0.1
    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

创建compose文件

vi /docker/grafana/docker-compose.yml
version: "2" 
 
networks: 
  prom: 
 
services: 
  prometheus: 
    image: prom/prometheus:latest 
    container_name: prometheus 
    hostname: prometheus 
    restart: always 
    user: root
    volumes: 
      # 挂载prometheus配置文件
      - /docker/grafana/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml 
      - /docker/grafana/prometheus/data:/prometheus 
      - /docker/grafana/prometheus/log:/var/log/ 
    ports: 
      - "9090:9090" 
    networks: 
      - prom 
  grafana: 
    image: grafana/grafana:latest 
    container_name: grafana 
    hostname: grafana 
    restart: always
    user: root
    volumes: 
      - /docker/grafana/grafana/data:/var/lib/grafana 
    ports: 
      - "3000:3000" 
    networks: 
      - prom 
  node-exporter: 
    image: prom/node-exporter:latest 
    container_name: node-exporter 
    hostname: exporter 
    restart: always 
    ports: 
      - "9100:9100" 
    networks: 
      - prom 
  cadvisor: 
    image: google/cadvisor:latest 
    container_name: cadvisor 
    hostname: cadvisor 
    restart: always 
    volumes: 
      # 挂载docker目录,为了监控docker使用情况,必须
      - /:/rootfs:ro 
      - /var/run:/var/run:rw 
      - /sys:/sys:ro 
      - /var/lib/docker/:/var/lib/docker:ro 
    ports: 
      - "8080:8080" 
    networks: 
      - prom 
  loki: 
    image: grafana/loki:latest 
    container_name: loki 
    hostname: loki
    restart: always
    user: root
    volumes: 
      # 挂载chunks文件夹及配置文件,loki收集到日志后会存储到chunks中
      - /docker/grafana/loki/chunks:/loki/chunks 
      - /docker/grafana/loki/rules:/loki/rules 
      - /docker/grafana/loki/local-config.yaml:/etc/loki/local-config.yaml 
    ports: 
      - "3100:3100" 
    networks: 
      - prom
cd /docker/grafana
docker-compose up -d

收集日志

使用loki-docker-driver

docker 安装loki驱动收集日志
docker plugin install  grafana/loki-docker-driver:latest --alias loki --grant-all-permissions
需要使用loki记录日志的docker-compose文件中替换日志收集驱动
    logging:
      driver: loki
      options:
        loki-url: "http://loki-server-ip:3100/api/prom/push"
        loki-retries: "5"
        loki-batch-size: "400"

使用promtail收集日志文件

开启promtail服务
version: "2"

services:
  grafana_promtail:
    container_name: promtail2
    image: grafana/promtail:2.4.0
    restart: always
    user: root
    volumes:
      - /docker/test-service/logs:/var/log
      - /docker/grafana/promatil/config.yml:/etc/promtail/config.yml
vi /docker/grafana/promatil/config.yml
server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /var/log-position/positions.yaml # 记录日志上传的行号

# loki服务地址
client:
  url: http://loki-server-ip:3100/loki/api/v1/push

scrape_configs:
- job_name: my-service
  static_configs:
  - targets:
      - localhost
    labels:
      job: test-service
      __path__: /var/log/*.log

好用的grafana模板下载

https://grafana.com/dashboards


Spring Boot Statistics
	6756
1 Node Exporter for Prometheus Dashboard EN 20201010
	11074
Docker and system monitoring
	893
Docker Container & Host Metrics
	10619

标签:compose,name,grafana,prometheus,loki,docker,configs
来源: https://www.cnblogs.com/yg0070/p/16437578.html