Fluentd とは †Docker/Apache起動イメージのログ集約を行うのに Fluentd を使ってみます。 ログをすべて JSON として扱い、 想定状況 †コンテナを起動すると Apache が立ち上がる docker イメージ内の Apache ログを install †今回は docker のコンテナ内もホストOSも CentOS6 を使用しているので、 docker コンテナ内へのインストールは Dockerfile を使わず、 $ sudo docker run -i -t -p 80:80 centos:apache bash-4.1# curl -L http://toolbelt.treasuredata.com/sh/install-redhat.sh | sh インストール完了後動作するか確認を行います。 bash-4.1# /etc/init.d/td-agent start Starting td-agent: [ OK ] 設定はまだ行っていませんが正常に起動することを確認できたので停止しておきます。 bash-4.1# /etc/init.d/td-agent stop Stopping td-agent: [ OK ] この後設定ファイルを編集し bash を抜けてコンテナを終了させてから 設定ファイル †Fluentd の設定ファイルは /etc/td-agent/td-agent.conf です。 <source> ..... </source> ログの出力先は Matchディレクティブ <match> ... </match> として記述します。 1. コンテナ内とリモートサーバ上の Fluentd によるログ記録 †コンテナ内の送信側 Fluentd の設定は以下のようになります。 <source> type tail path /var/log/httpd/access_log tag apache.access pos_file /var/log/td-agent/httpd-access_log.pos format apache2 </source> <match apache.access> type forward send_timeout 60s recover_wait 30s heartbeat_interval 1s <server> name fluentd-receiver host 192.168.0.204 port 24224 </server> </match> Apache のアクセスログを読み込んでリモートサーバの Fluentd に送出します。 $ sudo docker run -d -p 80:80 -p 24224:24224 -p 24224:24224/udp centos:apache+fluentd のように Apache と Fluentd で使用する port を forward してコンテナを起動します。 <source> type forward port 24224 </source> <match apache.access> type mongo host localhost port 27017 database fluentd collection apache flush_interval 10s </match> forward されてきたログを mongodb に保存します。 $ mongo > show dbs fluentd 0.203125GB local 0.078125GB proj_20140314 0.203125GB > use fluentd switched to db fluentd > show collections apache system.indexes > db.apache.find() { "_id" : ObjectId("53a8e94314cbd22e4e000012"), "host" : "192.168.0.128", "user" : null, "method" : "GET", "path" : "/", "code" : 403, "size" : 5039, "referer" : null, "agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36", "time" : ISODate("2014-06-24T02:57:58Z") } { "_id" : ObjectId("53a8e94314cbd22e4e000013"), "host" : "192.168.0.128", "user" : null, "method" : "GET", "path" : "/icons/apache_pb.gif", "code" : 304, "size" : null, "referer" : "http://192.168.0.161", "agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36", "time" : ISODate("2014-06-24T02:57:58Z") } { "_id" : ObjectId("53a8e94314cbd22e4e000014"), "host" : "192.168.0.128", "user" : null, "method" : "GET", "path" : "/icons/poweredby.png", "code" : 304, "size" : null, "referer" : "http://192.168.0.161", "agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36", "time" : ISODate("2014-06-24T02:57:58Z") } { "_id" : ObjectId("53a8e98514cbd22e4e000015"), "host" : "192.168.0.138", "user" : null, "method" : "GET", "path" : "/", "code" : 403, "size" : 5039, "referer" : null, "agent" : "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0", "time" : ISODate("2014-06-24T02:58:28Z") } { "_id" : ObjectId("53a8e98514cbd22e4e000016"), "host" : "192.168.0.138", "user" : null, "method" : "GET", "path" : "/icons/apache_pb.gif", "code" : 200, "size" : 2326, "referer" : "http://192.168.0.161", "agent" : "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0", "time" : ISODate("2014-06-24T02:58:28Z") } { "_id" : ObjectId("53a8e98514cbd22e4e000017"), "host" : "192.168.0.138", "user" : null, "method" : "GET", "path" : "/icons/poweredby.png", "code" : 200, "size" : 3956, "referer" : "http://192.168.0.161", "agent" : "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0", "time" : ISODate("2014-06-24T02:58:28Z") } { "_id" : ObjectId("53a8e98514cbd22e4e000018"), "host" : "192.168.0.138", "user" : null, "method" : "GET", "path" : "/favicon.ico", "code" : 404, "size" : 290, "referer" : null, "agent" : "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0", "time" : ISODate("2014-06-24T02:58:28Z") } > 2. ホストOS上の Fluentd によるログ記録 †docker コンテナ内では Fluentd を動作させず、 $ sudo docker run -d -p 80:80 -v /var/log/httpd/:/var/log/td-agent/httpd/ centos:apache この状態でコンテナを起動しホストOS上の /var/log/td-agent/httpd/access_log へアクセスすれば <source> type tail path /var/log/td-agent/httpd/access_log tag apache.access pos_file /var/log/td-agent/httpd-access_log.pos format apache2 </source> <match apache.access> type mongo host localhost port 27017 database fluentd collection apache flush_interval 10s </match> match ディレクティブの設定内容を変更すれば、 更新履歴 †2014/06/24 初稿公開 Satoshi OTSUKA
|