简述
需要一套轻量级日志系统,来做业务的日志管理工作。因ELK过于重量,且很多功能并不需要,所以在此选用PLG来实现。
PLG:Promtail + Loki + Grafana
Promtail:部署在应用服务器收集应用日志
Loki: 存储日志,提供查询功能,2.x版本提供日志告警功能
Grafana: 显示应用日志
而且PLG可以和Prometheus复用组件,如grafana alertManager 能减少维护成本。
PLG ELK日志对比
ELK (Elasticsearch 、 Logstash和Kibana) 是功能丰富,允许复杂的操作。但是,这些方案往往规模复杂,资源占用高,操作苦难。
PLG (Promtail + Loki + Grafana) 轻量级的,配置要求不高,功能简单,就是日志采集。
PLG属于grafana团队开发,官方介绍:https://grafana.com/logs/
PLG docker方式安装
docker安装
需要先在机器上安装docker,如机器已有docker服务,可跳过此步骤
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #insecure-registries 私有仓库地址,可按需配置
yum install yum-utils -y yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo yum -y install docker-ce-19.03.9-3.el7 docker-ce-cli containerd.io mkdir /etc/docker cat >> /etc/docker/daemon.json << EOF { "insecure-registries": ["http://10.10.10.10:19000"], "graph": "/data/docker", "log-opts": {"max-size":"500m", "max-file":"3"} } EOF systemctl start docker systemctl enable docker systemctl status docker
|
docker安装loki
loki详细配置文件说明请查看官方文档:https://grafana.com/docs/loki/latest/configuration/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| [root@Prometheus loki] loki-config.yaml rules tmp_rules
[root@Prometheus loki] auth_enabled: false
server: http_listen_port: 3100 grpc_listen_port: 9096
common: path_prefix: /tmp/loki storage: filesystem: chunks_directory: /tmp/loki/chunks rules_directory: /tmp/loki/rules replication_factor: 1 ring: instance_addr: 10.10.10.21 kvstore: store: inmemory
query_range: results_cache: cache: embedded_cache: enabled: true max_size_mb: 100
schema_config: configs: - from: 2020-10-24 store: boltdb-shipper object_store: filesystem schema: v11 index: prefix: index_ period: 24h
ruler: alertmanager_url: http://10.10.10.21:9093 enable_alertmanager_v2: true enable_api: true enable_sharding: true ring: kvstore: store: inmemory rule_path: /mnt/config/tmp_rules storage: type: local local: directory: /mnt/config/rules
limits_config: ingestion_rate_mb: 50 ingestion_burst_size_mb: 50 [root@Prometheus loki] [root@Prometheus loki] [root@Prometheus loki] loki-config.yaml rules/ tmp_rules/
[root@Prometheus loki] groups: - name: log alert rules: - alert: test expr: sum(rate({application="app1"} |= "service" [2m])) by (job) > 0 for: 2m labels: severity: warning alertType: log annotations: summary: test [root@Prometheus loki]
[root@Prometheus loki]
[root@Prometheus loki]
|
docker部署promtail
详细介绍请参考官方文档:https://grafana.com/docs/loki/latest/clients/promtail/
在业务机器上部署
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| [root@test122 promtail] /data/promtail [root@test122 promtail] promtail-config.yaml [root@test122 promtail] server: http_listen_port: 9080 grpc_listen_port: 0
positions: filename: /tmp/positions.yaml
clients: - url: http://10.10.10.21:3100/loki/api/v1/push
scrape_configs:
- job_name: app1 static_configs: - targets: - localhost labels: application: app1 host: 10.10.10.122 __path__: /var/log/*/app1/*log - job_name: app2 static_configs: - targets: - localhost labels: application: app2 host: 10.10.10.122 __path__: /var/log/*/app2/*log pipeline_stages: - match: selector: '{host="10.10.10.122"}' stages: - regex: expression: '(?P<timestamp>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+)(?P<level>\s\w{4}\s)(?P<class>\s\S{4,}\.\S+\.\S+\s)(?P<tid>\STID:.*\])(?P<msg>\s\-\s.*\s)' - multiline: firstline: '^\d{4}-\d{2}-\d{2}T\d{1,2}:\d{2}:\d{2}\.\d{3}' max_wait_time: 3s - labels: level: class: tid: msg: - timestamp: format: RFC3339Nano source: timestamp [root@test122 promtail]
[root@test122 promtail]
[root@test122 promtail]
|
promtail日志处理正则是golang语言的,可参考:http://c.biancheng.net/view/5124.html
正则在线测试地址:https://www.lddgo.net/string/golangregex
grafana配置
grafana的安装此处不在赘述,yum或者docker都可以
grafana添加数据源
登录grafana后,添加loki类型数据源
输入数据源名称和loki访问地址,默认端口为3100
日志搜索图表配置
导入图表看板:13639 里面的默认标签需要修改为自己实际标签
图表看板地址:https://grafana.com/grafana/dashboards/13639-logs-app/
nginx日志接入
需要将nginx日志输出改为json格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| log_format promtail_json '{"@timestamp":"$time_iso8601",' '"@version":"Promtail json",' '"server_addr":"$server_addr",' '"remote_addr":"$remote_addr",' '"host":"$host",' '"uri":"$uri",' '"body_bytes_sent":$body_bytes_sent,' '"bytes_sent":$body_bytes_sent,' '"request":"$request",' '"request_length":$request_length,' '"request_time":$request_time,' '"status":"$status",' '"http_referer":"$http_referer",' '"http_user_agent":"$http_user_agent"' '}'; ____________________________________________________________ #在server配置中将日志格式修改为json类型 access_log logs/app1_access.log promtail_json;
|
grafana导入nginx图表看板:https://grafana.com/grafana/dashboards/13865-fgc-nginx01-web-analytics/
看板内容请根据实际标签进行修改。
参考文档
promtail官方文档
loki官方文档
PLG安装文档