我应该如何使用systemd处理远程日志记录?

问题描述 投票:0回答:6

我在 Google 计算引擎 (GCE) 上运行多个 CoreOS 实例。 CoreOS 使用 systemd 的日志记录功能。如何将所有日志推送到远程目的地?据我了解,systemd 日志不具备远程日志记录功能。我当前的解决方法如下所示:

journalctl -o short -f | ncat <addr> <ip>

使用 https://logentries.com 通过 TCP 使用其 基于令牌的输入:

journalctl -o short -f | awk '{ print "<token>", $0; fflush(); }' | ncat data.logentries.com 10000

还有更好的方法吗?

编辑:https://medium.com/coreos-linux-for-massive-server-deployments/defb984185c5

logging docker systemd journal
6个回答
10
投票

systemd 过去的版本 216 包括通过客户端/服务器进程对进行远程日志记录功能。

http://www.freedesktop.org/software/systemd/man/systemd-journal-remote.html


7
投票

使用

-o short
的一个缺点是格式难以解析;
short-iso
更好。如果您使用 ELK 堆栈,导出为 JSON 会更好。像下面这样的 systemd 服务可以很好地将 JSON 格式的日志发送到远程主机。

[Unit]
Description=Send Journalctl to Syslog

[Service]
TimeoutStartSec=0
ExecStart=/bin/sh -c '/usr/bin/journalctl -o json -f | /usr/bin/ncat syslog 515'

Restart=always
RestartSec=5s

[Install]
WantedBy=multi-user.target

在另一边,

logstash.conf
对我来说包括:

input {
  tcp {
    port  => 1515
    codec => json_lines
    type  => "systemd"
  }
}

filter {
  if [type] == "systemd" {
    mutate { rename => [ "MESSAGE", "message" ] }
    mutate { rename => [ "_SYSTEMD_UNIT", "program" ] }
  }
}

这使得整个journalctl数据结构可供Kibana/Elasticsearch使用。


0
投票

Kelsey Hightower 的 Journal-2-logentries 对我们来说效果很好:https://logentries.com/doc/coreos/

如果您想加入并启用没有舰队的单位:

#!/bin/bash
#
# Requires the Logentries Token as Parameter

if [ -z "$1" ]; then echo "You need to provide the Logentries Token!"; exit
0; fi

cat << "EOU1" > /etc/systemd/system/systemd-journal-gatewayd.socket
[Unit]
Description=Journal Gateway Service Socket
[Socket]
ListenStream=/run/journald.sock
Service=systemd-journal-gatewayd.service
[Install]
WantedBy=sockets.target
EOU1

cat << EOU2 > /etc/systemd/system/journal-2-logentries.service
[Unit]
Description=Forward Systemd Journal to logentries.com
After=docker.service
Requires=docker.service
[Service]
TimeoutStartSec=0
Restart=on-failure
RestartSec=5
ExecStartPre=-/usr/bin/docker kill journal-2-logentries
ExecStartPre=-/usr/bin/docker rm journal-2-logentries
ExecStartPre=/usr/bin/docker pull
quay.io/kelseyhightower/journal-2-logentries
ExecStart=/usr/bin/bash -c \
"/usr/bin/docker run --name journal-2-logentries \
-v /run/journald.sock:/run/journald.sock \
-e LOGENTRIES_TOKEN=$1 \
quay.io/kelseyhightower/journal-2-logentries"
[Install]
WantedBy=multi-user.target
EOU2

systemctl enable systemd-journal-gatewayd.socket
systemctl start systemd-journal-gatewayd.socket
systemctl start journal-2-logentries.service

rm -f $0

0
投票

最近的一个 python 包可能很有用:journalpump

支持 Elastic Search、Kafka 和 logplex 输出。


0
投票

您还可以在

rsyslog-kafka
内使用
Rsyslog
模块。

Rsyslog with moduels:
 - imfile - input file
 - omkafka - output to Kafka

定义json模板并将其推送到Apache Kafka。 当日志在 Kafka 中时...


0
投票

CoreOS 使用 systemd 的日志记录功能。如何将所有日志推送到远程目的地?

此功能由 systemd-journal-remote 提供。它可以使用 systemd-journal-upload 将日志日志流式传输到中央日志服务器。如果中间的服务器或网络出现故障,一旦连接再次可用,它将立即传输日志。

实际上设置起来很简单:

  1. 在客户端和服务器上安装“systemd-journal-remote”
  2. 在客户端配置
    /etc/systemd/journal-upload.conf
    :
[Upload]
URL=http://your_central_logserver_ip:19532

并配置

/etc/systemd/system/systemd-journal-upload.service
:

[Unit]
Description=Journal Remote Upload Service
Documentation=man:systemd-journal-upload(8)
Wants=network-online.target
After=network-online.target

[Service]
ExecStartPre=/bin/sleep 10
Restart=on-failure
DynamicUser=yes
ExecStart=/lib/systemd/systemd-journal-upload --save-state
LockPersonality=yes
MemoryDenyWriteExecute=yes
PrivateDevices=yes
ProtectProc=invisible
ProtectControlGroups=yes
ProtectHome=yes
ProtectHostname=yes
ProtectKernelLogs=yes
ProtectKernelModules=yes
ProtectKernelTunables=yes
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
RestrictNamespaces=yes
RestrictRealtime=yes
StateDirectory=systemd/journal-upload
SupplementaryGroups=systemd-journal
SystemCallArchitectures=native
User=systemd-journal-upload
WatchdogSec=3min

[Install]
WantedBy=multi-user.target
EOF

在中央日志记录上为客户端日志创建一个目录:

mkdir -p /var/log/journal/remote/
chown systemd-journal-remote:systemd-journal-remote /var/log/journal/remote/
chmod 0750 /var/log/journal/remote/

并设置配置文件

/etc/systemd/system/systemd-journal-remote.service
:

[Unit]
Description=Journal Remote Sink Service
Documentation=man:systemd-journal-remote(8) man:journal-remote.conf(5)
Requires=systemd-journal-remote.socket

[Service]
ExecStart=/lib/systemd/systemd-journal-remote --listen-http=-3 --output=/var/log/journal/remote/all.journal
LockPersonality=yes
LogsDirectory=journal/remote
MemoryDenyWriteExecute=yes
NoNewPrivileges=yes
PrivateDevices=yes
PrivateNetwork=yes
PrivateTmp=yes
ProtectProc=invisible
ProtectClock=yes
ProtectControlGroups=yes
ProtectHome=yes
ProtectHostname=yes
ProtectKernelLogs=yes
ProtectKernelModules=yes
ProtectKernelTunables=yes
ProtectSystem=strict
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
RestrictNamespaces=yes
RestrictRealtime=yes
RestrictSUIDSGID=yes
SystemCallArchitectures=native
User=systemd-journal-remote
WatchdogSec=3min

# If there are many split up journal files we need a lot of fds to access them
# all in parallel.
LimitNOFILE=524288

[Install]
Also=systemd-journal-remote.socket
EOF

重新启动 systemd 服务并尝试在客户端上是否可以与

systemd-cat
配合使用:

systemd-cat ls /

运行

journalctl --file /var/log/journal/remote/all.journal -f
应该会在中央日志服务器上显示该命令的输出。

我将其用于我们的服务器,效果非常好。我关注了这篇关于如何使用 systemd-journal-remote 设置中央日志服务器的博文,以获得更详细和全面的说明。

© www.soinside.com 2019 - 2024. All rights reserved.