متقدم
البحث فيأدلة التثبيت
طريقة البحث
  • الصق رسالة الخطأ بين علامتي تنصيص، مثل "permission denied"، لتجد جدول الحل.
  • اختر مجالًا وجزءًا من الدليل (التثبيت، أول الإعدادات، الاستخدام اليومي، الأمان، الحلول) لتصل مباشرة إلى الخطوة الصحيحة.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

الدليل 35Grafana Loki · Monitoring

Install and set up Grafana Loki (with Grafana Alloy)

Loki stores logs from all your servers and apps in one place and lets you search them in Grafana — next to your metrics. It is cheap to run because it indexes only labels (like job, host, app), not every word. Grafana Alloy is the agent that reads the logs on each server and sends them to Loki (it replaces the old Promtail). This guide installs Loki as a single server with local storage and retention, installs Alloy on the servers, connects Grafana, and teaches the LogQL queries you need first.

  • 10 الدروس
  • 12 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

35.1

Know it before you install

A log stream = all lines with the same set of labels. You select streams with labels ({job="nginx", host="web1"}) and then filter the text (|= "error"). Few labels with few values = fast and cheap; a label with thousands of values (user ID, request ID) = slow and expensive.

ItemLokiAlloy (agent)
Packageloki (Grafana repository)alloy (Grafana repository)
Serviceloki.servicealloy.service
Config/etc/loki/config.yml/etc/alloy/config.alloy
Data/var/lib/loki/ (you set it)/var/lib/alloy/ (positions)
Ports3100/tcp (HTTP API), 9096/tcp (gRPC)12345/tcp (UI, localhost)

الأوامر في هذا الدرس

35.2

Install on RHEL, Rocky, Alma and Fedora

Add the Grafana repository (the same as in the Grafana guide), then install Loki on the log server and Alloy on every server:

cat > /etc/yum.repos.d/grafana.repo <<'EOF'
[grafana]
name=grafana
baseurl=https://rpm.grafana.com
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://rpm.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
EOF
dnf -y install loki
dnf -y install alloy

الأوامر في هذا الدرس

35.3

Install on Ubuntu and Debian

mkdir -p /etc/apt/keyrings
wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor > /etc/apt/keyrings/grafana.gpg
echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main" > /etc/apt/sources.list.d/grafana.list
apt-get update
apt-get install -y loki
apt-get install -y alloy

35.4

First configuration: Loki with local storage and retention

The package config is a demo that keeps data in /tmp. Replace it with a real single-server config:

/etc/loki/config.yml
auth_enabled: false
 
server:
  http_listen_address: 0.0.0.0
  http_listen_port: 3100
  grpc_listen_port: 9096
 
common:
  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: 2026-01-01
      store: tsdb
      object_store: filesystem
      schema: v13
      index:
        prefix: index_
        period: 24h
 
limits_config:
  retention_period: 744h
  ingestion_rate_mb: 8
  ingestion_burst_size_mb: 16
 
compactor:
  working_directory: /var/lib/loki/compactor
  retention_enabled: true
  delete_request_store: filesystem
SettingWhy
auth_enabled: falseOne tenant (one team). Loki has no login then — protect the port.
path_prefix: /var/lib/lokiData survives reboots (not /tmp).
schema: v13, store: tsdbThe current index format.
retention_period: 744h + retention_enabledLogs older than 31 days are deleted by the compactor.
ingestion_rate_mbLimits, so one noisy app can not flood Loki.
mkdir -p /var/lib/loki && chown -R loki: /var/lib/loki
systemctl enable --now loki
systemctl restart loki
curl -s http://127.0.0.1:3100/ready
ready
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.0/16" port port="3100" protocol="tcp" accept'
firewall-cmd --reload

For big setups use object storage (S3, Azure Blob, GCS) and the distributed mode or the Helm chart in Kubernetes.

35.5

Send logs with Alloy

On every server, tell Alloy which logs to read and where Loki is:

/etc/alloy/config.alloy
local.file_match "varlogs" {
  path_targets = [
    {"__path__" = "/var/log/nginx/*.log", "job" = "nginx", "host" = constants.hostname},
    {"__path__" = "/var/log/secure", "job" = "auth", "host" = constants.hostname},
  ]
}
 
loki.source.file "varlogs" {
  targets    = local.file_match.varlogs.targets
  forward_to = [loki.write.default.receiver]
}
 
loki.source.journal "journal" {
  forward_to = [loki.write.default.receiver]
  labels     = {"job" = "journal", "host" = constants.hostname}
}
 
loki.write "default" {
  endpoint {
    url = "http://loki.example.com:3100/loki/api/v1/push"
  }
}

Alloy must be allowed to read the logs (on Ubuntu the auth log is /var/log/auth.log):

usermod -aG adm,systemd-journal alloy
systemctl enable --now alloy
systemctl restart alloy
journalctl -u alloy -n 20

35.6

Connect Grafana and search

In Grafana: Connections → Data sources → Add → Loki, URL http://loki.example.com:3100 (or provision it like the Prometheus data source). Then Explore → Loki.

The LogQL you use every day:

LogQL
{job="nginx", host="web1"}
{job="nginx"} |= "error"
{job="nginx"} != "healthcheck" |~ "5[0-9]{2}"
{job="auth"} |= "Failed password"
{job="journal"} | json | level="error"
sum by (host) (count_over_time({job="auth"} |= "Failed password" [5m]))
sum by (status) (rate({job="nginx"} | pattern `<_> - - <_> "<_> <_> <_>" <status> <_>` [5m]))

From the terminal: logcli query '{job="nginx"} |= "error"' --addr=http://loki.example.com:3100 --since=1h.

Real uses at work:

  • One search for the logs of all servers and containers during an incident.
  • Metrics from logs: failed logins per host, 5xx per minute — and alerts on them (Loki ruler → Alertmanager).
  • Logs and metrics side by side in one Grafana dashboard.

الأوامر في هذا الدرس

35.7

Make it safe

Security checklist:

  • Loki with auth_enabled: false has no login: firewall 3100 to the agents' networks and Grafana only; or put a proxy with auth/TLS in front.
  • Use HTTPS (loki.write → https://…) when logs cross networks.
  • Do not log secrets and personal data; drop or mask them in Alloy (loki.process stages) before they leave the server.
  • Retention set and working (the compactor), so the disk does not fill.
  • Few labels with few values; never IDs as labels.

35.8

Update and remove

dnf -y upgrade loki alloy
apt-get install --only-upgrade -y loki alloy
dnf -y remove loki alloy

Read the release notes before a Loki major upgrade (config fields change).

الأوامر في هذا الدرس

35.9

Fix common problems

ProblemCauseFix
No logs in GrafanaAlloy can not reach Loki, or can not read the files.journalctl -u alloy; curl http://loki:3100/ready from the server; check groups adm, systemd-journal.
permission denied for /var/log/… in the Alloy logThe alloy user may not read the file.Add it to adm; check file modes.
entry too far behind / out of orderOld lines or wrong time on a server.Fix the clock (chrony); do not re-send old files.
Ingestion rate limit exceeded (429)An app logs too much.Raise ingestion_rate_mb, or drop noisy lines in Alloy.
Queries are slow / too many outstanding requestsHuge time range or high-cardinality labels.Shorter range; better label selectors; remove ID labels.
Disk fills although retention is setCompactor retention off or wrong delete_request_store.retention_enabled: true in compactor; check the Loki log.
Loki will not start after editingYAML or a field from another Loki version.journalctl -u loki; loki -config.file=/etc/loki/config.yml -verify-config.

35.10

Next steps

1Why should a user ID never be a Loki label?

Every value makes a new stream; thousands of values make Loki slow and expensive. Filter IDs in the text instead.

2Which query counts failed SSH logins per host in 5 minutes?

sum by (host) (count_over_time({job="auth"} |= "Failed password" [5m])).

الأوامر في هذا الدرس

كل الأوامر في هذا الفصل