LoginSignup
32
31

More than 5 years have passed since last update.

ELB s3 Log + Fluentd + Elasticsearch + Kibana + Ubuntu 14.04 LTS PV + EC2 on AWS

Last updated at Posted at 2014-10-23

ELBのログをs3に置いていたので、最近よく見かける方法で、解析してみることににした。
Elasticsearchがどんな感じで動くかまだ把握できていないのですが、以下の手順でサーバーを構築し動かしてみることにした。
久々にサーバーを設定するので、基本的な設定もメモすることした。

構成概要

ELB -> s3(log storage) <-> Fluentd -> Elasticsearch -> Kibana <- Proxy -> End Users

作業手順

AWS

項目 内容
OS Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-36-generic x86_64)(PV)
Instance m3.medium
Storage 容量に応じて

サーバーの準備

時間設定

$ sudo apt-get update
$ sudo apt-get install ntp
$ sudo vi /etc/ntp.conf
# server ntp.ubuntu.com
server ntp.nict.jp
$ sudo /etc/init.d/ntp restart
# UTC -> JST
$ sudo cp /etc/localtime /etc/localtime.bak
$ sudo cp /usr/share/zoneinfo/Japan /etc/localtime

言語設定

$ apt-get install language-pack-ja
$ echo 'export LANG=ja_JP.UTF-8'|tee -a ~/.bashrc
$ source ~/.bashrc
$ locale
LANG=ja_JP.UTF-8
LANGUAGE=
LC_CTYPE="ja_JP.utf8"
LC_NUMERIC="ja_JP.utf8"
LC_TIME="ja_JP.utf8"
LC_COLLATE="ja_JP.utf8"
LC_MONETARY="ja_JP.utf8"
LC_MESSAGES="ja_JP.utf8"
LC_PAPER="ja_JP.utf8"
LC_NAME="ja_JP.utf8"
LC_ADDRESS="ja_JP.utf8"
LC_TELEPHONE="ja_JP.utf8"
LC_MEASUREMENT="ja_JP.utf8"
LC_IDENTIFICATION="ja_JP.utf8"
LC_ALL=ja_JP.utf8

ホスト名設定

$ echo "127.0.0.1 $HOSTNAME" | sudo tee -a /etc/hosts

ファイルディスクリプタ設定

Fluentdで推奨されている設定を使ってみる

$ ulimit -n
1024
$ sudo vi /etc/security/limits.conf
# 以下を追加 
root soft nofile 65536
root hard nofile 65536
* soft nofile 65536
* hard nofile 65536

ネットワーク関連パラメータ設定

Fluentdで推奨されている設定を使ってみる

$ sudo vi /etc/sysctl.conf
# 以下を追加
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 10240    65535

再起動

$ sudo reboot

各種インストール

事前準備

必要なパッケージをインストール

$ sudo apt-get install git-core build-essential ruby1.9.3 openjdk-7-jdk

Elasticsearchインストール

$ wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.3.4.deb
$ sudo dpkg -i elasticsearch-1.3.4.deb
$ sudo service elasticsearch start
$ curl localhost:9200
{
  "status" : 200,
  "name" : "Jennifer Kale",
  "version" : {
    "number" : "1.3.4",
    "build_hash" : "a70f3ccb52200f8f2c87e9c370c6597448eb3e45",
    "build_timestamp" : "2014-09-30T09:07:17Z",
    "build_snapshot" : false,
    "lucene_version" : "4.9"
  },
  "tagline" : "You Know, for Search"
}

Fluentdインストール (参照)

# ubuntu trusty用をインストール
$ curl -L http://toolbelt.treasuredata.com/sh/install-ubuntu-trusty-td-agent2.sh | sh
$ sudo apt-get install libcurl4-openssl-dev -y
# Elasticsearchのプラグイン
$ sudo /opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-elasticsearch
# ELBのプラグイン
$ sudo /opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-elb-log

Kibanaインストール

# nginxで動かす
$ sudo apt-get install nginx
$ sudo vi /etc/nginx/sites-enabled/default
location / { # location kibana を展開したディレクトリを指定
    root /home/ubuntu/kibana;
    index index.html index.htm;
}
$ cd $HOME
$ wget https://download.elasticsearch.org/kibana/kibana/kibana-3.1.1.tar.gz
$ tar zxvf kibana-3.1.1.tar.gz
$ ln -s kibana-3.1.1 kibana
# nginxの起動と確認
$ sudo service nginx start
$ sudo service nginx status
 * nginx is running

各種設定

td-agentの設定

  • /etc/td-agent/td-agent.confを編集する
  • s3にあるELBのログを取得し、Elasticsearchに流す設定 (参照)
<source>
  type elb_log
  access_key_id     *****************
  secret_access_key *************************
  s3_endpoint       s3-ap-northeast-1.amazonaws.com
  s3_bucketname     バケット名
  s3_prefix         バケットの次に続く名前
  timestamp_file    /tmp/elb_last_at.dat
  # 5分単位にすることに
  refresh_interval  300
</source>
<match **>
  type elasticsearch
  type_name access_log
  host localhost
  port 9200
  logstash_format true
  include_tag_key true
  tag_key @log_name
</match>

td-agentの起動

バッファファイルの書き込みの権限がrootでないと有効でないので、sudo vi /etc/init.d/td-agentでファイルを編集して、USERとGROUPをrootにしておくと動く。取り急ぎ、他良いやり方思いつかなかったので、この対応にした。

$ sudo /etc/init.d/td-agent start
$ sudo /etc/init.d/td-agent status

自動起動の設定

$ sudo apt-get install sysv-rc-conf
$ sudo sysv-rc-conf td-agent on
$ sudo sysv-rc-conf --list | grep td-agent
td-agent     2:on   3:on    4:on    5:on

Kibanaへの接続

vpc内にKibanaを構築しているので、プロキシサーバー経由でKibanaに接続するnginxの設定サンプル。

server {
    listen 80;
    server_name ほげほげ.com;

    auth_basic            "Basic Auth";
    auth_basic_user_file  "/home/www/.htpasswd";

    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    location / {
        proxy_pass http://**.**.**.**;
    }
}
server {
    listen 9200;
    server_name ほげほげ.com;

    auth_basic            "Basic Auth";
    auth_basic_user_file  "/home/www/.htpasswd";

    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    location / {
        proxy_pass http://**.**.**.**:9200;
    }
}
32
31
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
32
31