متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 178Observability · Fluent Bit

How to ship logs with Fluent Bit

Logs that stay on the server are lost when the server dies, and nobody searches 20 servers by hand. Fluent Bit is a small, fast agent that reads log files and the journal, adds fields like the host name, buffers on disk when the network is down, and sends everything to Loki, Elasticsearch, S3 or a SIEM. This how-to ships nginx and journal logs to Loki, tests the pipeline with stdout first, and makes it survive restarts.

  • 5 الدروس
  • 8 الأوامر

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

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

178.1

The quick answer

/etc/fluent-bit/fluent-bit.yaml
service:
  flush: 1
  http_server: on
  http_listen: 127.0.0.1
  http_port: 2020
  storage.path: /var/lib/fluent-bit/buffer
  parsers_file: /etc/fluent-bit/parsers.conf
 
pipeline:
  inputs:
    - name: tail
      tag: nginx.access
      path: /var/log/nginx/access.log
      parser: nginx
      db: /var/lib/fluent-bit/tail-nginx.db
      storage.type: filesystem
    - name: systemd
      tag: host.journal
      read_from_tail: on
      storage.type: filesystem
  outputs:
    - name: loki
      match: '*'
      host: loki.example.com
      port: 3100
      labels: job=fluent-bit, host=web01
/opt/fluent-bit/bin/fluent-bit -c /etc/fluent-bit/fluent-bit.yaml --dry-run
systemctl restart fluent-bit

178.2

Step by step

  1. Install Fluent Bit (see the full guide) and make the service use the YAML file with a systemctl edit fluent-bit override (ExecStart= then the new line with -c /etc/fluent-bit/fluent-bit.yaml).
  2. Test with stdout first — replace the output with - name: stdout and match: '*', run Fluent Bit in the foreground, and look at the records:
/opt/fluent-bit/bin/fluent-bit -c /etc/fluent-bit/fluent-bit.yaml

You see records with the parsed nginx fields (remote, code, path …). When they look right, switch back to the Loki output.

  1. Inputs — tail reads files and remembers the position in the db file (no duplicates after a restart). systemd reads the journal; add systemd_filter: _SYSTEMD_UNIT=sshd.service to take one unit only.
  2. Buffer on disk — storage.path plus storage.type: filesystem keeps logs when Loki is down and sends them later.
  3. Labels — keep them few and fixed (job, host, env). Do not use user IDs or IPs as labels.
  4. Filters — drop noise before it costs money:
filter example
  filters:
    - name: grep
      match: nginx.access
      exclude: path ^/healthz
  1. Check health and counters — the built-in HTTP server shows records in and out, errors and retries:
curl -s http://127.0.0.1:2020/api/v1/metrics | jq '.output'
curl -s http://127.0.0.1:2020/api/v1/health
  1. Kubernetes — use the Helm chart (DaemonSet on every node) with the kubernetes filter, which adds namespace, pod and container labels.

178.3

Check that it worked

--dry-run says the configuration is valid; the metrics show growing proc_records for the output and zero errors; in Loki logcli query '{job="fluent-bit", host="web01"}' --since=5m returns the newest nginx and journal lines.

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

178.4

Common problems

ProblemCauseFix
No records from the fileWrong path, or no read rights on /var/log/nginx.Check the path; run as root or add the user to adm.
Old lines sent again after restartNo db file for tail.Add db:; keep /var/lib/fluent-bit on disk.
Output errors and retries growLoki not reachable, wrong port, or rejects labels.curl Loki /ready; check firewall; fewer labels.
Java stack traces split into many linesOne record per line.A multiline.parser (for example java) on the input.
Memory grows when the network is downMemory buffer only.storage.type: filesystem and storage.path.
The service still uses the old configThe unit starts fluent-bit.conf.The systemctl edit override; systemctl daemon-reload.

178.5

Go further

1Why does the tail input need a db file?

It stores how far each file was read, so after a restart Fluent Bit continues there instead of sending old lines again.

2What does filesystem storage protect against?

When the output is down, records wait on disk instead of filling memory or being lost.

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