Fluentd初探 简介与安装

Fluentd是一个开源的数据收集器,专为处理数据流设计,有点像 syslogd ,但是使用JSON作为数据格式。它采用了插件式的架构,具有高可扩展性高可用性,同时还实现了高可靠的信息转发。

据分(Y)析(Y),Fluentd是由Fluent+d得来,d生动形象地标明了它是以一个守护进程的方式运行。官网上将其描述为data collector,在使用上,我们可以把各种不同来源的信息,首先发送给Fluentd,接着Fluentd根据配置通过不同的插件把信息转发到不同的地方,比如文件、SaaS Platform、数据库,甚至可以转发到另一个Fluentd。

总结一下,数据流殊途,同归与Fluentd,Fluentd做一些诸如过滤、缓存、路由等工作,将其转发到不同的最终接收方。

用Fluentd处理数据还能保证一定的实时性,其提供种类丰富的客户端lib,很适合处理单位时间emit出log数量巨大的场景。从v10后,Fluentd 不支持 Windows。

其采用Ruby编写,比较注重性能的地方采用C编写,Ruby的创始人松本老师这样评价Fluentd:

Fluentd proves you can achieve programmer happiness and performance at the same time. A great example of Ruby beyond the Web.

Heroku的联合创始人Adam Wiggins说的挺有道理:

Logs are streams, not files. I love that Fluentd puts this concept front-and-center, with a developer-friendly approach for distributed systems logging.

更多信息请看 这里 

安装

安装前的准备工作

  1. 安装 ntpd

    ntpd ,以d结尾,是一个守护进程,全称是Network Time Protocol (NTP) daemon,它通过与一个Internet标准时钟服务器同步来维护系统时钟,同时也可以将本机做为一个时钟服务器对外提供时钟同步服务。更多信息请man一下ntpd。

    安装ntpd是为了防止Fluentd在收集log的时候出现非法的时间戳。

    在Ubuntu下,apt-get即可安装:

    $ sudo apt-get install ntp

    之后查看ntpd是否启动成功(pgrep返回进程号):

    $ pgrep ntpd
  1. 增加系统文件描述符的最大数量

    通过ulimit查看当前的文件描述符的最大数量:

    $ ulimit -n

    如果是1024,那是远远不够的,将下面4行添加到 /etc/security/limits.conf 中,重启机器。

    root soft nofile 65536
    root hard nofile 65536
    * soft nofile 65536
    * hard nofile 65536

    之后用ulimit查看,应该是65536了。

  1. (Optional)优化机器内核TCP/IP参数

    如果Fluentd处于一个高负载的运行环境,比如一个机器中运行了多个Fluentd实例,那么最好将下面的参数添加到 /etc/sysctl.conf 中:

    net.ipv4.tcp_tw_recycle = 1
    net.ipv4.tcp_tw_reuse = 1
    net.ipv4.ip_local_port_range = 10240    65535

    然后执行 sysctl -或者重启机器使之生效。

    这个跟TCP/IP的细节有关系,我没有深究,貌似是为了防止短连接数过多,机器处于 TIME_WAIT 状态的TCP连接数过多,开启快速回收与重用。具体请参考 这里 

安装Fluentd

写在前面

有 很多种方式 来安装Fluentd,比如通过Ruby的Gem,以及Ubuntu的deb、OS X的Homebrew。

采用Ruby的Gem安装比较简单,安装的是基本版的Fluentd,即不自带 /etc/init.脚本,在运行时内存分配采用的是操作系统默认的方式;

而采用.rpm/.deb for Linux的方式安装呢,安装的是一个由 Treasure Data, Inc 维护的稳定版本Fluentd,又被称为td-agent。为了保持一个良好的灵活性,Fluentd采用Ruby编写,性能要求比较高的地方采用C编写,考虑到一般的用户可能安装操作Ruby有困难,因此该公司维护并提供了一个稳定版本的Fluentd。该版本的Fluentd在内存分配上采用了着重避免内存碎片分配实现 jemalloc 。两个版本的区别可以在这 查看 

那么我们到底选用哪种方式呢,根据官方的文档。td-agent强调new features的稳定性,如果希望自己控制Fluentd的features,自己手动去更新,采用gem的方式安装。如果是第一次使用Fluentd或者在大规模的生产环境使用,推荐安装td-agent,每2-3个月,td-agent就会发布一个新的版本。

在Ubuntu 12.04上安装Fluentd

注意 这种方式截止到笔者发稿仅支持一下两个Ubuntu的版本:

  • Ubuntu 12.04 LTS / Precise
  • Ubuntu 10.04 LTS / Lucid

可以在 这里 查看最新进展。

  1. (Optional but Recommend)设置GPG Key

    deb包使用 Treasure Data GPG key 签名,首先将 GPG key 导入apt:

    $ apt-key add /path/to/RPM-GPG-KEY-td-agent
  1. 执行下面的脚本

    echo "This script requires superuser access to install apt packages."
    echo "You will be prompted for your password by sudo."
    
    # clear any previous sudo permission
    sudo -k
    
    # run inside sudo
    sudo sh <<SCRIPT
    
    # add treasure data repository to apt
    echo "deb http://packages.treasure-data.com/precise/ precise contrib" > /etc/aptsources.list.d/treasure-data.list
    
    # update your sources
    apt-get update
    
    # install the toolbelt
    apt-get install -y --force-yes td-agent
    
    SCRIPT

    即安装完毕。

    也可执行下面的的命令:

    $ curl -L http://toolbelt.treasuredata.com/sh/install-ubuntu-precise.sh | sh

    其实脚本内容就是上面的,我粘过去的。

  1. 启动Fluentd的守护进程

    因为我们安装的是td-agent,自带 /etc/init.d/td-agent 脚本,通过该脚本启动Fluentd服务。

    $ /etc/init.d/td-agent restart
    * Restarting td-agent td-agent
    $ /etc/init.d/td-agent status
    * ruby is running

    同时还支持以下命令:

    $ /etc/init.d/td-agent start
    $ /etc/init.d/td-agent stop
    $ /etc/init.d/td-agent restart
    $ /etc/init.d/td-agent status

    配置文件在: /etc/td-agent/td-agent.conf

  1. 测试

    默认在配置文件中td-agent在8888端口监听,接收来自HTTP的log信息,并把其路由到stdout( /var/log/td-agent/td-agent.log ),可以用curl命令测试:

    $ curl -X POST -d 'json={"json":"message"}' http://localhost:8888/debug.test

    之后可以打开 /var/log/td-agent/td-agent.log查看。

     

posted @ 2017-12-20 18:51  大数据从业者FelixZh  阅读(1178)  评论(0编辑  收藏  举报