头像 四核的基地
Article

Loki+Grafana轻量级日志采集方案

介绍在 Linux 环境下使用 Loki + Grafana 搭建轻量级日志采集方案的完整流程。

Loki+Grafana轻量级日志采集方案

一、前置准备(创建统一用户 + 目录)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 安装依赖
apt update && sudo apt install -y wget unzip tar || sudo dnf install -y wget unzip tar

# 创建统一用户 www (UID=666, GID=666)
groupadd -g 666 www
useradd -u 666 -g 666 -s /sbin/nologin -M www

# 创建统一目录
mkdir -p /opt/loki /var/lib/loki
mkdir -p /opt/promtail /var/lib/promtail
mkdir -p /opt/grafana /var/lib/grafana

# 统一赋权 www 用户
chown -R www:www /opt/loki /var/lib/loki
chown -R www:www /opt/promtail /var/lib/promtail
chown -R www:www /opt/grafana /var/lib/grafana

二、部署 Loki

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
# 下载二进制
cd /opt/loki
wget https://github.com/grafana/loki/releases/download/v3.5.1/loki-linux-amd64.zip
unzip loki-linux-amd64.zip
chmod +x loki-linux-amd64
rm -f loki-linux-amd64.zip

#配置配置文件loki-config.yaml
cat > /opt/loki/loki-config.yaml <<'EOF'
auth_enabled: false

server:
http_listen_port: 3100
http_listen_address: 10.0.0.244 #修改为自己的ip

common:
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: inmemory

schema_config:
configs:
- from: 2020-01-01
store: tsdb
object_store: filesystem
schema: v13
index:
prefix: index_
period: 24h

ruler:
alertmanager_url: http://localhost:9093

analytics:
reporting_enabled: false
EOF

# 修改存储路径

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 System
After=network.target

[Service]
Type=simple
User=www
Group=www
ExecStart=/opt/loki/loki-linux-amd64 --config.file=/opt/loki/loki-config.yaml
Restart=on-failure
RestartSec=5
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

# 启动
systemctl daemon-reload
systemctl start loki
systemctl enable loki
systemctl status loki

三、部署 Promtail(统一 www 用户)

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
# 下载二进制
cd /opt/promtail
wget https://github.com/grafana/loki/releases/download/v3.5.1/promtail-linux-amd64.zip
unzip promtail-linux-amd64.zip
chmod +x promtail-linux-amd64
rm -f promtail-linux-amd64.zip

# 配置文件
tee > /opt/promtail/promtail-config.yaml <<'EOF'
server:
http_listen_port: 9080
grpc_listen_port: 0

positions:
filename: /var/lib/promtail/positions.yaml

clients:
- url: http://10.0.0.244:3100/loki/api/v1/push #修改为自己的ip

scrape_configs:
- job_name: system
static_configs:
- targets:
- localhost
labels:
job: varlogs
__path__: /var/log/*.log
host: k8s-worker04
EOF

# Systemd 服务(运行用户:www)
tee /etc/systemd/system/promtail.service > /dev/null <<EOF
[Unit]
Description=Promtail Log Collector
After=network.target loki.service

[Service]
Type=simple
User=www
Group=www
ExecStart=/opt/promtail/promtail-linux-amd64 --config.file=/opt/promtail/promtail-config.yaml
Restart=on-failure
RestartSec=5
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

# 启动
systemctl daemon-reload
systemctl start promtail
systemctl enable promtail
systemctl status promtail

四、部署 Grafana(统一 www 用户)

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
# 下载二进制
cd /opt
wget https://dl.grafana.com/oss/release/grafana-11.2.0.linux-amd64.tar.gz
tar -zxvf grafana-11.2.0.linux-amd64.tar.gz
mv grafana-v11.2.0 grafana
rm -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/grafana
logs = /var/log/grafana
plugins = /var/lib/grafana/plugins

[security]
admin_user = admin
admin_password = admin
EOF

# Systemd 服务(运行用户:www)
tee /etc/systemd/system/grafana-server.service > /dev/null <<EOF
[Unit]
Description=Grafana Visualization
After=network.target loki.service

[Service]
Type=simple
User=www
Group=www
WorkingDirectory=/opt/grafana
ExecStart=/opt/grafana/bin/grafana-server --config=/opt/grafana/conf/grafana.ini
Restart=on-failure
RestartSec=5
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

# 启动
systemctl daemon-reload
systemctl start grafana-server
systemctl enable grafana-server
systemctl status grafana-server

五、一键验证(所有服务状态)

1
2
3
4
5
6
7
8
9
10
11

# 验证端口
curl 127.0.0.1:3100/ready # Loki
curl 127.0.0.1:3000 # Grafana

#查看网页
10.0.0.244:3000

用户:admin
密码:admin