什么是Loki
Loki是一个轻量级的日志系统,没有ELK那么臃肿。
分为日志服务器(Loki) 代理(Promtail) UI(Grafana)
具体架构相关可以查看官方文档
https://grafana.com/docs/loki/latest/fundamentals/
环境准备
OS: RHEL7 / CentOS7
CPU: 4核心
内存:8GB
使用基础环境安装
参考文档
官方文档
安装
https://grafana.com/docs/loki/latest/installation/local/
配置
https://grafana.com/docs/loki/latest/getting-started/get-logs-into-loki/
https://grafana.com/docs/loki/latest/getting-started/logcli/
https://grafana.com/docs/loki/latest/getting-started/grafana/
https://grafana.com/docs/loki/latest/getting-started/troubleshooting/
之前有参考过其他的博客,后来发现文档内容过久,已不符合当前版本。
博客都具备时效性,还是官方文档靠谱。
工具包下载
Grafana下载
进入如下网址查看最新的安装包:
https://grafana.com/grafana/download?pg=get&plcmt=selfmanaged-box1-cta1
当前下载安装方式:
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-8.2.5-1.x86_64.rpm
sudo yum install grafana-enterprise-8.2.5-1.x86_64.rpm
Loki下载
进入如下网址查看最新的安装包:
https://grafana.com/docs/loki/latest/installation/?pg=get&plcmt=selfmanaged-box2-cta1
当前下载安装方式:
wget https://github.com/grafana/loki/releases/download/v2.4.1/loki-linux-amd64.zip
unzip loki-linux-amd64.zip
chmod a+x "loki-linux-amd64"
接着进行下一步安装
Promtail下载
还是在刚才下载Loki的页面,下载Promtail安装包
当前下载安装方式:
wget https://github.com/grafana/loki/releases/download/v2.4.1/promtail-linux-amd64.zip
unzip promtail-linux-amd64.zip
chmod a+x "promtail-linux-amd64"
Loki和Promtail是开箱即用的,所以运行之前请放到你想让这两个程序运行的目录下
mv loki-linux-amd64 /opt
mv promtail-linux-amd64 /opt
配置文件下载
cd /opt
wget https://raw.githubusercontent.com/grafana/loki/master/cmd/loki/loki-local-config.yaml
wget https://raw.githubusercontent.com/grafana/loki/main/clients/cmd/promtail/promtail-local-config.yaml
官方的yaml配置文件下载
运行
./loki-linux-amd64 -config.file=loki-local-config.yaml
Loki默认运行在 http://localhost:3100/metrics,接着要开防火墙
# Loki 3100 Grafana 3000 Promtail 9080
firewall-cmd --zone=public --add-port=3100/tcp --permanent
firewall-cmd --zone=public --add-port=3000/tcp --permanent
firewall-cmd --zone=public --add-port=9080/tcp --permanent
firewall-cmd --reload
# 最后检查一下有没有添加端口成功
firewall-cmd --zone=public --list-ports
但是这样窗口一关闭Loki就跟着关闭了,因此要用nohup运行,写成了脚本
#!/bin/bash
nohup ./loki-linux-amd64 -config.file=loki-local-config.yaml & # 注意&后一定要加一个空格
echo "loki runngin..."
nohup ./promtail-linux-amd64 -config.file=promtail-local-config.yaml &
echo "promtail runngin..."
echo ""
将日志配入Loki
应用服务器安装Promtail
Promtail在应用服务器收集日志并发给日志服务器的Loki,因此需要安装Promtail
安装可以参考官方地址
https://grafana.com/docs/loki/latest/clients/promtail/installation/
对于Linux上的安装上文已有,对于Windows上的安装也是大同小异开箱即用
修改Promtail配置
在日志接入Loki之前,需要先修改Promtail的配置。
首先打开之前的promtail-local-config.yaml,内容如下
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /tmp/positions.yaml
clients:
- url: http://localhost:3100/loki/api/v1/push
scrape_configs:
- job_name: system
static_configs:
- targets:
- localhost
labels:
job: varlogs
__path__: /var/log/*log
scrape_configs下的七行是将系统生成的日志发送给Loki,然后Loki在命令行和http://localhost:3100/metrics中输出它们,因此接下来就是要改scrape_configs这部分。复制这七行,可以新增其他日志信息。
官方文档对这七行的解释
job_name - This differentiates the logs collected from other log groups.
targets - Optional for static_configs. However, is often defined because in older versions of Promtail it was not optional. This was an artifact from directly using the Prometheus service discovery code, which required this entry.
labels - Static label to apply to every log line scraped by this definition. Good examples include the environment name, job name, or app name.
path - The path to where the logs that Loki is to consume are stored.
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /tmp/positions.yaml
clients:
- url: http://localhost:3100/loki/api/v1/push
scrape_configs:
- job_name: system
static_configs:
- targets:
- localhost
labels:
job: varlogs
__path__: /var/log/*log
- job_name: grafana
static_configs:
- targets:
- grafana
labels:
job: grafana
__path__: /var/log/grafana/grafana.log
# Windows的路径
# __path__: "C:/Program Files/GrafanaLabs/grafana/data/log/grafana.log"
启动Promtail
Windows
.\promtail-windows-amd64.exe --config.file=promtail-local-config.yaml
Linux
./promtail-linux-amd64 -config.file=promtail-local-config.yaml
LogCLI安装
LogCLI就是Loki的命令行。