如何配置 systemd 日志远程?

Joh*_*Siu 24 journald

如何配置 systemd journal-remote 以侦听特定端口?

我能找到的只是命令行示例。根据手册页,journal-remote.conf 中似乎没有任何选项。

Joh*_*Siu 42

看到连一条评论都没有,我决定继续研究,最后把配置拼凑起来。

操作系统:Ubuntu 16.04

systemd:229-1ubuntu2

systemd-journal-remote: 229-1ubuntu2

上传服务器配置

这个其实很简单,网上的例子都是正确的,只需要接触一个配置文件。

使用以下命令安装 systemd-journal-remote

sudo apt-get install systemd-journal-remote
Run Code Online (Sandbox Code Playgroud)

编辑/etc/systemd/journal-upload.conf

/etc/systemd/journal-upload.conf

[Upload]
URL=http://10.0.0.1:19532
# ServerKeyFile=/etc/ssl/private/journal-upload.pem
# ServerCertificateFile=/etc/ssl/certs/journal-upload.pem
# TrustedCertificateFile=/etc/ssl/ca/trusted.pem
Run Code Online (Sandbox Code Playgroud)

确保日志上传在启动时自动启动

sudo systemctl enable systemd-journal-upload.service
Run Code Online (Sandbox Code Playgroud)

配置完成后重启journal-upload。

sudo systemctl restart systemd-journal-upload.service
Run Code Online (Sandbox Code Playgroud)

如果您使用的是 http,则可以按照上述操作并保留底部 3 行注释。对于主动模式 https,取消注释它们并创建这些证书文件。

URL 实际上规定了传输协议 (http/https) 和要使用的目标端口。

此外,如果您想防止将来的软件包更新意外覆盖,您可以创建一个 /etc/systemd/journal-upload.conf.d 目录并将您的配置文件放入其中,只要该文件以 .conf 扩展名结尾。

作为旁注,我在 LXC 容器中执行此操作,似乎该服务不会使用 /etc/hosts 进行 dns 解析,我最终在这里使用 IP 地址。因此,如果您使用主机名并看到日志上传无法到达目标的错误消息,请尝试使用 IP 地址。

接收服务器配置

接收服务器在查找配置信息时给我带来了大部分麻烦。与上传服务器不同的是,配置分散在这一侧。

使用以下命令安装 systemd-journal-remote 并启用监听端口

sudo apt-get install systemd-journal-remote
sudo systemctl enable systemd-journal-remote.socket
Run Code Online (Sandbox Code Playgroud)

有两种方式来配置journal-remote,主动和被动。我在这里使用被动模式。

端口号

日志监听端口的配置文件/etc/systemd/system/sockets.target.wants/systemd-journal-remote.socket如下。ListenStream 是端口号。

与上传端不同,此设置与使用哪种协议(http/https)无关。它只指定侦听端口号。

[Unit]
Description=Journal Remote Sink Socket

[Socket]
ListenStream=19532

[Install]
WantedBy=sockets.target
Run Code Online (Sandbox Code Playgroud)

协议(http/https)和日志/日志位置

要更改日志传输的协议和保存位置,请复制/lib/systemd/system/systemd-journal-remote.service/etc/systemd/system/,然后编辑/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=/etc/systemd/systemd-journal-remote \
          --listen-http=-3 \
          --output=/var/log/journal/remote/
User=systemd-journal-remote
Group=systemd-journal-remote
PrivateTmp=yes
PrivateDevices=yes
PrivateNetwork=yes
WatchdogSec=3min

[Install]
Also=systemd-journal-remote.socket
Run Code Online (Sandbox Code Playgroud)

--listen-http=-3指定传入的日记是使用HTTP。如果要使用 https,请将其更改为--listen-https=-3.

--output=/var/log/journal/remote/指定传入日志的接收器(保存目录)。如果它不存在,请创建它并将其所有者更改为systemd-journal-remote

sudo mkdir /var/log/journal/remote
sudo chown systemd-journal-remote /var/log/journal/remote
Run Code Online (Sandbox Code Playgroud)

配置后重启journal-remote.socket。

sudo systemctl daemon-reload
Run Code Online (Sandbox Code Playgroud)

最明显的/etc/systemd/journal-remote.conf呢?

[Remote]
# Seal=false
# SplitMode=host
# ServerKeyFile=/etc/ssl/private/journal-remote.pem
# ServerCertificateFile=/etc/ssl/certs/journal-remote.pem
# TrustedCertificateFile=/etc/ssl/ca/trusted.pem
Run Code Online (Sandbox Code Playgroud)

由于我没有使用 https,因此不需要更改任何内容。

  • 您不应该编辑 /lib/systemd/system 中的文件。systemd 为您提供 /etc/systemd/system/ 用于自定义单元。只需将 /lib/systemd/system 中的文件复制并粘贴到该 /etc/systemd/system 中,它将覆盖 lib 中的文件。 (3认同)
  • @MattW 不需要上传服务器。对于接收服务器,journald 配置可以处理轮换。 (2认同)