使用RSyslog将Nginx Access Log写入Kafka

个人博客地址:使用RSyslog将Nginx Access Log写入Kafka | 一张假钞的真实世界

环境说明

  • CentOS Linux release 7.3.1611
  • kafka_2.12-0.10.2.2
  • nginx/1.12.2
  • rsyslog-8.24.0-34.el7.x86_64.rpm

创建测试Topic

bash 复制代码
$ ./kafka-topics.sh --zookeeper 192.168.72.25:2181/kafka --create --topic develop-test-topic --partitions 10 --replication-factor 3

RSyslog安装

一般系统自带RSyslog服务无需另外安装。但是因为数据需要通过RSyslog的omkafka模块写入到Kafka,而omkafka在RSyslog的v8.7.0+版本才支持,所以要看当前系统中RSyslog的版本是否需要升级。使用以下命令查看:

bash 复制代码
# rsyslogd -v
rsyslogd 7.4.7, compiled with:
	FEATURE_REGEXP:				Yes
	FEATURE_LARGEFILE:			No
	GSSAPI Kerberos 5 support:		Yes
	FEATURE_DEBUG (debug build, slow code):	No
	32bit Atomic operations supported:	Yes
	64bit Atomic operations supported:	Yes
	Runtime Instrumentation (slow code):	No
	uuid support:				Yes

See http://www.rsyslog.com for more information.

执行以下命令安装:

bash 复制代码
# sudo yum install rsyslog

安装依赖关系如下:

添加omkafka模块

bash 复制代码
# yum install rsyslog-kafka

RSyslog Client Nginx配置

注意,Nginx 1.7.1之后才支持syslog的方式处理日志。具体配置项参见官网文档Logging to syslog

Nginx配置主要是日志格式和Access Log配置项:

bash 复制代码
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

server {
    listen 11000;

    location / {
        proxy_pass http://10.16.0.144:11000;
        access_log syslog:server=localhost,facility=local7,tag=nginx11000Root,severity=info main;
    }
}

RSyslog Server端配置

RSyslog的主配置文件/etc/rsyslog.conf,其中会包含引入/etc/rsyslog.d下扩展名为conf的配置文件。

修改配置文件/etc/rsyslog.conf将下面两行前面的注释去掉:

bash 复制代码
$ ModLoad imudp
$ UDPServerRun 514

/etc/rsyslog.d目录下创建rsyslog_nginx_kafka_cluster.conf,配置内容如下:

bash 复制代码
module(load="imudp")
input(type="imudp" port="514")

# nginx access log ==> rsyslog server(local) ==> kafka
module(load="omkafka")

template(name="nginx-11000-root" type="string" string="%msg%")

if $inputname == "imudp" then {
    if ($programname == "nginx11000Root") then
        action(type="omkafka"
            template="nginx-11000-root"
            broker=["192.168.72.10:9092","192.168.72.20:9092","192.168.72.25:9092","192.168.72.26:9092","192.168.72.27:9092","192.168.72.48:9092","192.168.72.55:9092","192.168.72.80:9092","192.168.72.81:9092","192.168.72.97:9092"]
            topic="develop-test-topic"
            partitions.auto="on"
            confParam=[
                "socket.keepalive.enable=true"
            ]
        )
}

:rawmsg, contains, "nginx11000Root" ~

联调测试

启动RSyslog服务:

bash 复制代码
# service rsyslog start
Redirecting to /bin/systemctl start  rsyslog.service

遇到的问题

syslog tag 只能包含字母和数字

bash 复制代码
# nginx -t
nginx: [emerg] syslog "tag" only allows alphanumeric characters and underscore in     /etc/nginx/conf.d/jx-11000-jenkins149-36-144.conf:7
nginx: configuration file /etc/nginx/nginx.conf test failed

'omkafka' is unknown

RSyslog中没有包含omkafka模块,需要另外安装。查看/var/log/messages日志信息会有以下提示:

bash 复制代码
# tail -f messages
Mar 15 15:13:40 192-168-72-29 systemd: Started System Logging Service.
Mar 15 15:13:40 192-168-72-29 rsyslogd: could not load module '/usr/lib64/rsyslog/omkafka.so', dlopen:     /usr/lib64/rsyslog/omkafka.so: cannot open shared object file: No such file or directory  [v8.24.0-34.el7 try     http://www.rsyslog.com/e/2066 ]
Mar 15 15:13:40 192-168-72-29 rsyslogd: could not load module '/usr/lib64/rsyslog/omkafka.so', dlopen:     /usr/lib64/rsyslog/omkafka.so: cannot open shared object file: No such file or directory  [v8.24.0-34.el7 try     http://www.rsyslog.com/e/2066 ]
Mar 15 15:13:40 192-168-72-29 rsyslogd: module name 'omkafka' is unknown [v8.24.0-34.el7 try     http://www.rsyslog.com/e/2209 ]
Mar 15 15:13:40 192-168-72-29 rsyslogd: module name 'omkafka' is unknown [v8.24.0-34.el7 try     http://www.rsyslog.com/e/2209 ]

CentOS 6.5升级Rsyslog

CentOS 6.5自带的RSyslog版本是rsyslogd 5.8.10。按照以下方式安装新版本:

bash 复制代码
# cd /etc/yum.repos.d/
# wget http://rpms.adiscon.com/v8-stable/rsyslog.repo
# yum install rsyslog
相关推荐
随遇而安622&50817 分钟前
完整化安装kubesphere,ks-jenkins的状态一直为init
运维·jenkins
想做富婆39 分钟前
linux: 文本编辑器vim
linux·运维·服务器
别断我一连胜1 小时前
系统看门狗配置--以ubuntu为例
linux·运维·ubuntu
KeyPan1 小时前
【Ubuntu与Linux操作系统:二、图形界面与命令行】
linux·运维·服务器·pytorch·ubuntu·机器学习·计算机视觉
KeyPan1 小时前
【Ubuntu与Linux操作系统:四、文件与目录管理】
linux·运维·服务器·算法·ubuntu
雷学习2 小时前
shell脚本练习
linux·运维·服务器
KeyPan3 小时前
【Ubuntu与Linux操作系统:十二、LAMP平台与PHP开发环境】
android·java·linux·运维·服务器·ubuntu
NightReader3 小时前
Ubuntu 24.04蓝牙失效之复活
linux·运维·ubuntu
苹果醋35 小时前
MySQL Binlog 同步工具go-mysql-transfer Lua模块使用说明
java·运维·spring boot·mysql·nginx