11.5. 使用 logging 系统角色应用远程日志解决方案

按照以下步骤准备并应用 Red Hat Ansible Core playbook 来配置远程日志记录解决方案。在本 playbook 中,一个或多个客户端从 systemd-journal 获取日志,并将它们转发到远程服务器。服务器从 remote_rsyslogremote_files 接收远程输入,并将日志输出到由远程主机名命名的目录中的本地文件。

先决条件

  • 对一个或多个 受管节点 的访问和权限,这些节点是您要使用 logging 系统角色配置的系统。
  • 控制节点 的访问和权限,这是 Red Hat Ansible Core 配置其他系统的系统。

    在控制节点上:

    • ansible-corerhel-system-roles 软件包已安装 。
    • 列出受管节点的清单文件。
注意

您不必安装 rsyslog 软件包,因为系统管理员在部署时会安装 rsyslog

流程

  1. 创建定义所需角色的 playbook:

    1. 创建新 YAML 文件,并在文本编辑器中打开,例如:

      # vi logging-playbook.yml
    2. 将以下内容插入到文件中:

      ---
      - 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 策略。目前还不支持通过系统角色配置防火墙。

  2. 创建列出您的服务器和客户端的清单文件:

    1. 创建新文件并在文本编辑器中打开该文件,例如:

      # vi inventory.ini
    2. 将以下内容插入到清单文件中:

      [servers]
      server ansible_host=host1.example.com
      [clients]
      client ansible_host=host2.example.com

      其中:

      • host1.example.com 是日志服务器。
      • host2.example.com 是日志客户端。
  3. 对清单运行 playbook。

    # ansible-playbook -i /path/to/file/inventory.ini /path/to/file/_logging-playbook.yml

    其中:

    • inventory.ini 是清单文件。
    • logging-playbook.yml 是您创建的 playbook。

验证

  1. 在客户端和服务器系统上测试 /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.
  2. 验证客户端系统向服务器发送信息:

    1. 在客户端系统中发送测试信息:

      # logger test
    2. 在服务器系统上,查看 /var/log/messages 日志,例如:

      # cat /var/log/messages
      Aug  5 13:48:31 host2.example.com root[6778]: test

      其中 host2.example.com 是客户端系统的主机名。请注意,该日志包含输入 logger 命令的用户的用户名,本例中为 root

其他资源