返回 文章目录 Article Loki+Grafana轻量级日志采集方案 介绍在 Linux 环境下使用 Loki + Grafana 搭建轻量级日志采集方案的完整流程。 2025年8月23日 四核 教程 #Linux Loki+Grafana轻量级日志采集方案一、前置准备(创建统一用户 + 目录)12345678910111213141516# 安装依赖apt update && sudo apt install -y wget unzip tar || sudo dnf install -y wget unzip tar# 创建统一用户 www (UID=666, GID=666)groupadd -g 666 wwwuseradd -u 666 -g 666 -s /sbin/nologin -M www# 创建统一目录mkdir -p /opt/loki /var/lib/lokimkdir -p /opt/promtail /var/lib/promtailmkdir -p /opt/grafana /var/lib/grafana# 统一赋权 www 用户chown -R www:www /opt/loki /var/lib/lokichown -R www:www /opt/promtail /var/lib/promtailchown -R www:www /opt/grafana /var/lib/grafana 二、部署 Loki123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172# 下载二进制cd /opt/lokiwget https://github.com/grafana/loki/releases/download/v3.5.1/loki-linux-amd64.zipunzip loki-linux-amd64.zipchmod +x loki-linux-amd64rm -f loki-linux-amd64.zip#配置配置文件loki-config.yamlcat > /opt/loki/loki-config.yaml <<'EOF'auth_enabled: falseserver: http_listen_port: 3100 http_listen_address: 10.0.0.244 #修改为自己的ipcommon: instance_addr: 10.0.0.244 #修改为自己的ip path_prefix: /var/lib/loki storage: filesystem: chunks_directory: /var/lib/loki/chunks rules_directory: /var/lib/loki/rules replication_factor: 1 ring: kvstore: store: inmemoryschema_config: configs: - from: 2020-01-01 store: tsdb object_store: filesystem schema: v13 index: prefix: index_ period: 24hruler: alertmanager_url: http://localhost:9093analytics: reporting_enabled: falseEOF# 修改存储路径sed -i 's|/loki/chunks|/var/lib/loki/chunks|g' loki-config.yaml# Systemd 服务(运行用户:www)tee /etc/systemd/system/loki.service > /dev/null <<EOF[Unit]Description=Loki Log Aggregation SystemAfter=network.target[Service]Type=simpleUser=wwwGroup=wwwExecStart=/opt/loki/loki-linux-amd64 --config.file=/opt/loki/loki-config.yamlRestart=on-failureRestartSec=5LimitNOFILE=65536[Install]WantedBy=multi-user.targetEOF# 启动systemctl daemon-reloadsystemctl start lokisystemctl enable lokisystemctl status loki 三、部署 Promtail(统一 www 用户)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354# 下载二进制cd /opt/promtailwget https://github.com/grafana/loki/releases/download/v3.5.1/promtail-linux-amd64.zipunzip promtail-linux-amd64.zipchmod +x promtail-linux-amd64rm -f promtail-linux-amd64.zip# 配置文件tee > /opt/promtail/promtail-config.yaml <<'EOF'server: http_listen_port: 9080 grpc_listen_port: 0positions: filename: /var/lib/promtail/positions.yamlclients: - url: http://10.0.0.244:3100/loki/api/v1/push #修改为自己的ipscrape_configs:- job_name: system static_configs: - targets: - localhost labels: job: varlogs __path__: /var/log/*.log host: k8s-worker04EOF# Systemd 服务(运行用户:www)tee /etc/systemd/system/promtail.service > /dev/null <<EOF[Unit]Description=Promtail Log CollectorAfter=network.target loki.service[Service]Type=simpleUser=wwwGroup=wwwExecStart=/opt/promtail/promtail-linux-amd64 --config.file=/opt/promtail/promtail-config.yamlRestart=on-failureRestartSec=5LimitNOFILE=65536[Install]WantedBy=multi-user.targetEOF# 启动systemctl daemon-reloadsystemctl start promtailsystemctl enable promtailsystemctl status promtail 四、部署 Grafana(统一 www 用户)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647# 下载二进制cd /optwget https://dl.grafana.com/oss/release/grafana-11.2.0.linux-amd64.tar.gztar -zxvf grafana-11.2.0.linux-amd64.tar.gzmv grafana-v11.2.0 grafanarm -f grafana-11.2.0.linux-amd64.tar.gz# 配置文件tee /opt/grafana/conf/grafana.ini > /dev/null <<'EOF'[server]http_port = 3000[paths]data = /var/lib/grafanalogs = /var/log/grafanaplugins = /var/lib/grafana/plugins[security]admin_user = adminadmin_password = adminEOF# Systemd 服务(运行用户:www)tee /etc/systemd/system/grafana-server.service > /dev/null <<EOF[Unit]Description=Grafana VisualizationAfter=network.target loki.service[Service]Type=simpleUser=wwwGroup=wwwWorkingDirectory=/opt/grafanaExecStart=/opt/grafana/bin/grafana-server --config=/opt/grafana/conf/grafana.iniRestart=on-failureRestartSec=5LimitNOFILE=65536[Install]WantedBy=multi-user.targetEOF# 启动systemctl daemon-reloadsystemctl start grafana-serversystemctl enable grafana-serversystemctl status grafana-server 五、一键验证(所有服务状态)1234567891011# 验证端口curl 127.0.0.1:3100/ready # Lokicurl 127.0.0.1:3000 # Grafana#查看网页10.0.0.244:3000用户:admin密码:admin