LoginSignup
1
1

More than 5 years have passed since last update.

fluentd で Compute Engine インスタンスのログを収集する (2015年7月28日版)

Last updated at Posted at 2015-07-28

はじめに

Google Compute Engine インスタンス (CoreOS) 上で走っている Docker コンテナの出力を Cloud Logging に集めたい.fluentdfluent-plugin-google-cloudを使うとサービスアカウントを使って認証してくれるので簡単に収集できる.

ここに書いた時からバージョンが上がったらしく,動かなくなったため再調査した.

fluentd コンテナの準備

ドキュメントによると,インストールスクリプトが用意されているので,これを用いる.ただし,このスクリプトでインストールするとデーモンとして起動してしまうので,一旦停止したのちフォアグラウンドジョブとして実行する必要がある.さらに,/etc/google-fluentd/config.d/ に色々設定が含まれているので削除しておく.

Dockerfile
FROM ubuntu:latest
RUN apt-get update && apt-get install -y curl

# fluentd のインストールとデーモンの停止
RUN curl -L https://storage.googleapis.com/signals-agents/logging/google-fluentd-install.sh | bash \
         && service google-fluentd stop

# fluent-plugin-record-reformer のインストール
RUN /opt/google-fluentd/embedded/bin/gem install fluent-plugin-record-reformer

# 不要な設定ファイルの削除
RUN rm /etc/google-fluentd/config.d/*.conf

# 設定とエントリーポイントの追加
ADD docker.conf /etc/google-fluentd/config.d/
ADD entrypoint.sh /root/

ENTRYPOINT ["/root/entrypoint.sh"]
CMD ["google-fluentd"]

設定は前回と同じく,公式の設定を使う場合は,

docker.conf
<source>
  type tail
  path /var/lib/docker/containers/*/*-json.log
  pos_file /var/log/fluentd-docker.pos
  time_format %Y-%m-%dT%H:%M:%S
  tag docker.*
  format json
</source>
<match docker.var.lib.docker.containers.*.*.log>
  type record_reformer
  tag docker.all
  <record>
    container_id ${tag_parts[5]}
  </record>
</match>

を用意しておく.また,エントリーポイントとして,

entrypoing.sh
#!/bin/bash
set -e

export PATH=/sbin:/usr/sbin:/bin:/usr/bin

TD_AGENT_NAME=google-fluentd
TD_AGENT_HOME=/opt/google-fluentd
TD_AGENT_DEFAULT=/etc/default/google-fluentd
TD_AGENT_USER=root
TD_AGENT_GROUP=root
TD_AGENT_RUBY=/opt/google-fluentd/embedded/bin/ruby
TD_AGENT_BIN_FILE=/usr/sbin/google-fluentd
TD_AGENT_LOG_FILE=/var/log/google-fluentd/google-fluentd.log
TD_AGENT_OPTIONS="--use-v1-config --suppress-repeated-stacktrace"
STOPTIMEOUT=120

if [ -f "${TD_AGENT_DEFAULT}" ]; then
  . "${TD_AGENT_DEFAULT}"
fi
TD_AGENT_ARGS="${TD_AGENT_ARGS:-${TD_AGENT_BIN_FILE} --log ${TD_AGENT_LOG_FILE} ${TD_AGENT_OPTIONS}}"
. /lib/lsb/init-functions
if [ -f "${TD_AGENT_HOME}/embedded/lib/libjemalloc.so" ]; then
  export LD_PRELOAD="${TD_AGENT_HOME}/embedded/lib/libjemalloc.so"
fi

if [ "$1" == "google-fluentd" ]; then
  echo -n "Starting ${TD_AGENT_NAME}"
  ulimit -n 65536 1>/dev/null 2>&1 || true

  echo "exec ${TD_AGENT_RUBY} ${TD_AGENT_ARGS}"
  exec ${TD_AGENT_RUBY} ${TD_AGENT_ARGS}

else
  exec "$@"
fi

を用意しておく.

ビルドと実行

ビルドと実行は,前回と同じく,

$ docker build -t fluentd-agent .

にてビルドし,docker のログが保存されているディレクトリをマウントしながら起動する.

$ docker run -d --name fluentd -v /var/lib/docker:/var/lib/docker fluentd-agent

Docker イメージ

以上をまとめた,イメージ jkawamoto/docker-google-fluentd に用意した.

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1