使用Loki promtail 收集Linux原生部署應(yīng)用日志
在Kubernetes集群中部署Loki后,默認(rèn)只能收集Kubernetes集群中的系統(tǒng)日志。然而,對(duì)于在Linux服務(wù)器上原生部署的業(yè)務(wù)系統(tǒng),無法直接進(jìn)行日志收集。這就導(dǎo)致了在日常業(yè)務(wù)交互中需要查詢這些系統(tǒng)的日志時(shí)存在困難。為了解決這個(gè)問題,我們計(jì)劃在Linux服務(wù)器上安裝Promtail來收集日志,并將其推送到Loki,以實(shí)現(xiàn)統(tǒng)一的日志分析和檢索。
Promtail是Loki的客戶端代理,它負(fù)責(zé)在Linux服務(wù)器上收集日志。通過在每個(gè)Linux服務(wù)器上安裝和配置Promtail,我們可以將業(yè)務(wù)系統(tǒng)的日志數(shù)據(jù)發(fā)送到Loki進(jìn)行集中存儲(chǔ)和分析。
圖片
1. 前置依賴
- Loki 集群,如果不知如何按照,請(qǐng)參考:10分鐘在K8s中部署輕量級(jí)日志系統(tǒng)Loki
- Linux 服務(wù)器可以訪問Loki集群
2. 下載安裝 promtail
wget https://github.com/grafana/loki/releases/download/v2.7.3/promtail-linux-amd64.zip
3. 解壓并遷移文件至/usr/local/promtail文件夾下
unzip promtail-linux-amd64.zip
mkdir /usr/local/promtail
mv promtail-linux-amd64 /usr/local/promtail
4. 創(chuàng)建并修改配置文件
vi config.yaml
server:
http_listen_port: 9080
grpc_listen_port: 0
#讀取位置保存文件
positions:
filename: /opt/promtail/positions.yaml # This location needs to be writeable by Promtail.
## 指定推送Loki地址
clients:
- url: http://172.18.1.47:31300/loki/api/v1/push
scrape_configs:
- job_name: system
pipeline_stages:
#日志換行,匹配開頭
- multiline:
firstline: ^\d{1,2}:\d{2}:\d{2}.\d{1,3}
max_lines: 128
max_wait_time: 3s
#靜態(tài)配置
static_configs:
- targets:
- localhost
#定義靜態(tài)標(biāo)簽
labels:
job: smartpark-agent # A `job` label is fairly standard in prometheus and useful for linking metrics and logs.
node_name: 172.17.1.55 # A `host` label will help identify logs from this machine vs others
app: smartpark-agent
__path__: /data/logs/*log # The path matching uses a third party library: https://github.com/bmatcuk/doublestar
3.編譯自啟動(dòng)腳本
cat > /usr/lib/systemd/system/promtail.service <<EOF
[Unit]
Descriptinotallow=promtail
Documentatinotallow=https://github.com/grafana/loki/tree/master
After=network.target
[Service]
Type=simple
User=root
ExecStart=/usr/local/promtail/promtail-linux-amd64 -config.file=/usr/local/promtail/config.yaml
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
4.啟動(dòng)命令
#啟動(dòng)
systemctl start promtail
#查看狀態(tài)
systemctl status promtail
#設(shè)置開機(jī)啟動(dòng)
systemctl enable promtail
圖片
5. 查看驗(yàn)證
登錄Grafana在Explore 中選擇標(biāo)簽 app = smartpark-agent, 可以查詢到相關(guān)日志,如下圖
圖片