promtail-loki-grafana轻量日志监控系统

一、安装

1 promtail

1.1 安装

curl -O -l "https://github.com/grafana/loki/releases/download/v1.5.0/promtail-linux-amd64.zip"
unzip promtail-linux-amd64.zip
chmod a+x promtail-linux-amd64

1.2 创建配置文件

创建配置文件(注意缩进),这边命名为 promtail-local-config.yaml,内容如下

# Promtail Server Config
server:
  http_listen_port: 9080
  grpc_listen_port: 0

# Positions
positions:
  filename: /tmp/positions.yaml

# Loki服务器的地址
clients:
  - url: http://localhost:3100/loki/api/v1/push

scrape_configs:
  - job_name: ts-egg-test
    pipeline_stages:
      - match:
          selector: '{job="ts-egg-web-log"}'
          stages:
            - regex:
                expression: '^(?P<time>[\d\s-:,]*)(?P<level>[a-zA-Z]+)\s(?P<pid>[\d]+)\s(?P<content>.*)$'
            - labels:
                level:
                content:
                pid:
                time:
    static_configs:
      - targets:
          - localhost
        labels:
          job: ts-egg-web-log
          host: localhost
          __path__: /root/logs/ts-egg-test/ts-egg-test-web.log
  - job_name: ts-egg-error
    static_configs:
      - targets:
          - localhost
        labels:
          job: common-error
          host: localhost
          __path__: /root/logs/ts-egg-test/common-error.log
  - job_name: json-test-j
    pipeline_stages:
      - match:
          selector: '{job="json-test"}'
          stages:
            - json:
                expressions:
                  timej: time
                  pidj: pid
                  levelj: level
            - labels:
                levelj:
                pidj:
                timej:
    static_configs:
      - targets:
          - localhost
        labels:
          job: json-test
          __path__: /mnt/json-test.log

这边创建三个源,第一个使用正则匹配日志内容提取标签、第三个使用 json 提取。

1.3 启动

nohup ./promtail-linux-amd64 -config.file=/mnt/promtail-local-config.yaml > log-promtail.file 2>&1 &

后台启动promtail,配置文件目录就是刚才创建时的目录,下面loki也是一样,启动日志重定向到 log-promtail.file

2 loki

2.1 安装loki

curl -O -L "https://github.com/grafana/loki/releases/download/v1.5.0/loki-linux-amd64.zip"
unzip loki-linux-amd64.zip
chmod a+x loki-linux-amd64

2.2 创建配置文件

创建 loki-local-config.yaml

auth_enabled: false

server:
  http_listen_port: 3100

ingester:
  lifecycler:
    address: 0.0.0.0
    ring:
      kvstore:
        store: inmemory
      replication_factor: 1
    final_sleep: 0s
  chunk_idle_period: 5m
  chunk_retain_period: 30s
  max_transfer_retries: 0

schema_config:
  configs:
    - from: 2018-04-15
      store: boltdb
      object_store: filesystem
      schema: v11
      index:
        prefix: index_
        # 每张表的时间范围 6天
        period: 144h
      chunks:
        period: 144h

storage_config:
#  流文件存储地址
  boltdb:
    directory: /tmp/loki/index
#  索引存储地址
  filesystem:
    directory: /tmp/loki/chunks

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

chunk_store_config:
  # 最大可查询历史日期 90天
  max_look_back_period: 2160h

# 表的保留期90天
table_manager:
  retention_deletes_enabled: true
  retention_period: 2160h

1.3 启动

nohup ./loki-linux-amd64 -config.file=/mnt/loki-local-config.yaml > log-loki.file 2>&1 &

3 grafana

3.1 安装

3.1.1 官网提供的方式

3.1.1.1 设置源

vim /etc/yum.repos.d/grafana.repo

源配置输入如下

[grafana]
name=grafana
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
sudo yum install grafana

 

3.1.2 从 docker 镜像安装

docker run -d -p 3000:3000 grafana/grafana

3.2 启动

3.2.1 从官网安装的

sudo systemctl daemon-reload
sudo systemctl start grafana-server
sudo systemctl status grafana-server

3.2.2 从 docker 安装

docker start containerId

二、grafana使用

1 登陆

地址:http://localhost:3000/,默认账号为 admin  admin

2 添加数据源 datasource

3 explore查询数据

可以根据筛选条件过滤loki日志,{} 里面可以填promtail配置文件里自己定义的label,promtail会自动根据label将日志分组

4 二次开发(机器内存4G以上)

1)安装 go 1.14以上、node 12 以上 、yarn

2)下载项目 https://github.com/grafana/grafana必须放在 $GOPATH/src/github.com/grafana/grafana 目录下

3)进入$GOPATH/src/github.com/grafana/grafana文件夹, 编译前端环境

有个比较难装的依赖独立安装:yarn install node-sass --sass_binary_site=https://npm.taobao.org/mirrors/node-sass
安装依赖:yarn install --pure-lockfile
执行编译:yarn start
编译完成后,在public文件夹会看到多了个build文件夹

4)编译后端环境

go run build.go setup
go run build.go build
编译完成后,会看到多了个bin文件夹

5)运行

bin/grafana-server start

 6)修改完前端代码后,需要重新编译前端环境

yarn start

7)打包

go run build.go build package

打包后文件在dist目录下

ps:不同系统之间交叉编译报错。如 macos 下交叉编译为 linux 环境的包, env GOOS=linux go run build.go build package,报错 undefined: sqlite3.Error,原因是sqlite 因此c的动态库,不支持交叉编译(似乎可以借助第三方工具完成

pps:相同系统包可能无法正常使用,如在docker ubuntu16.04环境下打包,包放到线上机器,解压运行,报错 version `GLIBC_2.28' not found,GLIBC升级难度较大,并且有风险。所以直接在线上机器编译运行最稳妥

8)前端代码替换

由于在其他环境编译的包放到线上存在问题,二次开发修改的也只有页面部分,

所以这边将yarn start 编译后的public文件夹压缩上传到线上机器后解压,覆盖掉/usr/share/grafana下的public文件夹(路径因安装方式而异) 即可替换掉前端部分。这样grafana的配置、日志、数据库内容都不会变。

5 其他

grafana之Variables变量的使用

Grafana的介绍与使用

grafana 文档

loki 文档  

label的使用  官网label介绍

promtail 文档 

 LogQL

grafana二次开发

  • 1
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值