16.5. 使用 logging 系统角色应用远程日志解决方案
按照以下步骤准备并应用 Red Hat Ansible Core playbook 来配置远程日志记录解决方案。在本 playbook 中,一个或多个客户端从 systemd-journal
获取日志,并将它们转发到远程服务器。服务器从 remote_rsyslog
和 remote_files
接收远程输入,并将日志输出到由远程主机名命名的目录中的本地文件。
先决条件
-
对一个或多个 受管节点 的访问和权限,这些节点是您要使用
logging
系统角色配置的系统。 对 控制节点 的访问和权限,这是 Red Hat Ansible Core 配置其他系统的系统。
在控制节点上:
-
ansible-core
和rhel-system-roles
软件包已安装 。 - 列出受管节点的清单文件。
-
您不必安装 rsyslog
软件包,因为系统管理员在部署时会安装 rsyslog
。
流程
创建定义所需角色的 playbook:
创建新 YAML 文件,并在文本编辑器中打开,例如:
# vi logging-playbook.yml
将以下内容插入到文件中:
--- - name: Deploying remote input and remote_files output hosts: server roles: - rhel-system-roles.logging vars: logging_inputs: - name: remote_udp_input type: remote udp_ports: [ 601 ] - name: remote_tcp_input type: remote tcp_ports: [ 601 ] logging_outputs: - name: remote_files_output type: remote_files logging_flows: - name: flow_0 inputs: [remote_udp_input, remote_tcp_input] outputs: [remote_files_output] - name: Deploying basics input and forwards output hosts: clients roles: - rhel-system-roles.logging vars: logging_inputs: - name: basic_input type: basics logging_outputs: - name: forward_output0 type: forwards severity: info target: <host1.example.com> udp_port: 601 - name: forward_output1 type: forwards facility: mail target: <host1.example.com> tcp_port: 601 logging_flows: - name: flows0 inputs: [basic_input] outputs: [forward_output0, forward_output1] [basic_input] [forward_output0, forward_output1]
其中
<host1.example.com>
是日志记录服务器。注意您可以修改 playbook 中的参数以符合您的需要。
警告日志解决方案只适用于在服务器或者客户端系统的 SELinux 策略中定义的端口并在防火墙中打开。默认 SELinux 策略包括端口 601、514、6514、10514 和 20514。要使用其他端口,请修改客户端和服务器系统上的 SELinux 策略。
创建列出您的服务器和客户端的清单文件:
创建新文件并在文本编辑器中打开该文件,例如:
# vi <inventory.ini>
将以下内容插入到清单文件中:
[servers] server ansible_host=<host1.example.com> [clients] client ansible_host=<host2.example.com>
其中:
-
<host1.example.com>
是日志记录服务器。 -
<host2.example.com>
是日志记录客户端。
-
对清单运行 playbook。
# ansible-playbook -i </path/to/file/inventory.ini> </path/to/file/logging-playbook.yml>
其中:
-
<inventory.ini>_
是清单文件。 -
<logging-playbook.yml>_
是您创建的 playbook。
-
验证
在客户端和服务器系统上测试
/etc/rsyslog.conf
文件的语法:# rsyslogd -N 1 rsyslogd: version 8.1911.0-6.el8, config validation run (level 1), master config /etc/rsyslog.conf rsyslogd: End of config validation run. Bye.
验证客户端系统向服务器发送信息:
在客户端系统中发送测试信息:
# logger test
在服务器系统上,查看
/var/log/<host2.example.com>/messages
日志,例如:# cat /var/log/<host2.example.com>/messages Aug 5 13:48:31 <host2.example.com> root[6778]: test
其中
<host2.example.com>
是客户端系统的主机名。请注意,该日志包含输入 logger 命令的用户的用户名,本例中为root
。
其他资源
- 准备控制节点和受管节点以使用 RHEL 系统角色
-
与
rhel-system-roles
软件包一起安装的文档在/usr/share/ansible/roles/rhel-system-roles.logging/README.html
- RHEL 系统角色 知识库文章