fluent と hoop を使って HDFS にリアルタイムにログを流す

概要

複数台のWebサーバのログを fluent と hoop を使ってリアルタイムにHDFSに追記していくテスト。
より頻度の高い行動解析を行うことができるようになる?
参考にしたブログ: tagomorisのメモ置き場

テスト構成

# 初めてテキストで図書いた。
# 線を交差させる所で面倒くさくなって適当に...
web01 server      fluent master
+---------+       +---------+
| Fluentd |--+--->| Fluentd |--+
+---------+  |+-->+---------+  |   Proxy
             ||                +-->+--------+   +--------+
             ||                    |  hoop  |-->|  HDFS  |
web02 server ||   fluent slave +-->+--------+   +--------+
+---------+  +|-->+---------+  |
| Fluentd |---+-->| Fluentd |--+
+---------+       +---------+

環境

CentOS 5.7 64bit
Hadoop 0.20.2-CDH3u3

hoop

今回はNameNode(hadoop-nn01)にインストール

maven インストール(hoop ビルドに必要)

wget http://ftp.riken.jp/net/apache//maven/binaries/apache-maven-3.0.4-bin.tar.gz -P /home/src/archives
tar zxvfC /home/src/archives/apache-maven-3.0.4-bin.tar.gz /usr/local
cd /usr/local/
ln -s apache-maven-3.0.4 maven
vi /etc/profile
 #=============
 MAVEN_HOME=/usr/local/maven
 PATH=$PATH:$MAVEN_HOME/bin
 #=============

source /etc/profile

hoop インストール

wget --no-check-certificate https://github.com/cloudera/hoop/zipball/master -O cloudera-hoop-43f595d.zip -P /home/src/archives
cd /home/src
unzip archives/cloudera-hoop-43f595d.zip
cd cloudera-hoop-43f595d
# ビルドのテスト通らなかったのでテストスキップさせてます
mvn clean package site assembly:single -Dmaven.test.skip=true
cp -r ./hoop-distro/target/hoop-0.1.0-SNAPSHOT/hoop-0.1.0-SNAPSHOT /usr/local/
cd /usr/local
ln -s hoop-0.1.0-SNAPSHOT/ hoop
chown -R hadoop. hoop
cd hoop
# hadoopのcore-site.xmlに以下設定を追記(許可設定?)
vi /usr/local/hadoop/conf/core-site.xml
 #=============
  <property>
    <name>hadoop.proxyuser.hadoop.hosts</name>
    <value>*</value>
  </property>

  <property>
    <name>hadoop.proxyuser.hadoop.groups</name>
    <value>*</value>
  </property>
 #=============

vi conf/hoop-site.xml
 #=============
 <configuration>
   <property>
     <name>hoop.hadoop.conf:fs.default.name</name>
     <value>hdfs://hadoop-nn01:9000</value>
   </property>
 </configuration>
 #=============

vi conf/hoop-env.sh
 #=============
 export HOOP_LOG=/var/log/hoop
 #=============

mkdir /var/log/hoop
chown hadoop. /var/log/hoop
chown -R hadoop. /usr/local/hoop/

hoop 起動

[hadoop@hadoop-nn01]$ /usr/local/hoop/bin/hoop.sh start

hoop 動作確認

echo "test" > test.txt
curl -X POST 'http://hadoop-nn01:14000/user/hadoop/test.txt?op=create&user.name=hadoop' --data-binary @test.txt --header "content-type: application/octet-stream"
curl -X GET 'http://hadoop-nn01:14000/user/hadoop/test.txt?user.name=hadoop&offset=-1&len=1000'
test

fluent

各サーバ(今回はwebサーバ、fluentdサーバ)にインストールする

Ruby インストール

# libyaml目当て
wget http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gz -P /home/src/archives
tar zxvfC /home/src/archives/yaml-0.1.4.tar.gz /home/src
cd /home/src/yaml-0.1.4
./configure
make
make install

# ruby
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p125.tar.bz2 -P /home/src/archives
tar jxvfC /home/src/archives/ruby-1.9.3-p125.tar.bz2 /home/src
cd /home/src/ruby-1.9.3-p125
./configure
make
make install

fluent インストール

# まず gem をアップデート(習慣)
gem update --system

# fluent
gem install fluent

# fluent -> hoop のためのプラグイン
gem install fluent-pluing-hoop

# セットアップ(設定ファイルテンプレなど生成)
fluentd --setup /etc/fluent

設定ファイル書き

・web01

[root@web01]# vi /etc/fluent/web_server.conf

<source>
  type tail
  format apache
  tag apache.access.01
  path /var/log/apache2/access_log
  pos_file /var/tmp/access_log.pos
</source>
<match apache.access.01>
  type forward
  retry_limit 2

  <server>
    # fluentd master
    host 192.168.56.102
    port 24224
  </server>

  <secondary>
    <server>
      # fluentd slave
      host 192.168.56.103
      port 24224
    </server>
  </secondary>
</match>
・web02

[root@web02]# vi /etc/fluent/web_server.conf

<source>
  type tail
  format apache
  tag apache.access.02
  path /var/log/apache2/access_log
  pos_file /var/tmp/access_log.pos
</source>
<match apache.access.02>
  type forward
  retry_limit 2

  <server>
    # fluentd master
    host fluent01
    port 24225
  </server>

  <secondary>
    <server>
      # fluentd slave
      host fluent02
      port 24225
    </server>
  </secondary>
</match>
・fluentd server(master, slave 同じ設定)

# 1プロセス1webサーバに対応させてみる
[root@fluent01]# vi /etc/fluent/fluentd_server_web01.conf
[root@fluent02]# vi /etc/fluent/fluentd_server_web01.conf

<source>
  type forward
  port 24224
</source>
<match apache.access.01>
  type hoop
  hoop_server hadoop-nn01:14000
  # 毎時でログを区切る
  path /user/hadoop/%Y%m%d%H_apache.log
  username hadoop
  #time_slice_wait 30s
  flush_interval 5s
</match>

[root@fluent01]# vi /etc/fluent/fluentd_server_web02.conf
[root@fluent02]# vi /etc/fluent/fluentd_server_web02.conf

<source>
  type forward
  port 24225
</source>
<match apache.access.02>
  type hoop
  hoop_server hadoop-nn01:14000
  # 毎時でログを区切る
  path /user/hadoop/%Y%m%d%H_apache.log
  username hadoop
  #time_slice_wait 30s
  flush_interval 5s
</match>

fluentd 起動

# web01
[root@web01]# fluentd -c /etc/fluent/web_server.conf &

# web02
[root@web01]# fluentd -c /etc/fluent/web_server.conf &

# fluent01
[root@fluent01]# fluentd -c /etc/fluent/fluentd_server_web01.conf &
[root@fluent01]# fluentd -c /etc/fluent/fluentd_server_web02.conf &

# fluent02
[root@fluent02]# fluentd -c /etc/fluent/fluentd_server_web01.conf &
[root@fluent02]# fluentd -c /etc/fluent/fluentd_server_web02.conf &

動作確認

# webサーバでホストされているページにアクセス
curl -i http://example.localhost/

# ログファイル生成確認
hadoop fs -ls /user/hadoop/*_apache.log
-rw-r--r--   3 hadoop supergroup        815 2012-03-21 20:36 /user/hadoop/2012032120_apache.log

# 生成されたログファイル閲覧(出力結果長かったので一部抜粋)
hadoop fs -cat /user/hadoop/2012032120_apache.log
2012-03-21T11:35:41Z    apache.access.01        {"host":"192.168.56.1","user":"-","method":"GET","path":"/","code":"200","size":"413","referer":"-","agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.117 Safari/535.19"}
2012-03-21T11:35:41Z    apache.access.02        {"host":"192.168.56.1","user":"-","method":"GET","path":"/","code":"200","size":"413","referer":"-","agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.117 Safari/535.19"}