AWSにElasticsearch Serviceがあるので、そちらが使えれば使った方が良いが今回は勉強の為、あえてEC2にインストールした。
あとから思い出しながら書いているので、間違えている箇所があるかも・・。
前提
- EC2のt2.mediumを使用
- t2.microだとメモリーエラーになり、メモリーを減らすのが面倒だったので。
- インストール先は、/var/www(どこでもOK)とする。
事前準備
java8をインストール
yum install java-1.8.0-openjdk
yum install java-1.8.0-openjdk-devel
・切り替え
sudo alternatives --config java
There are 2 programs which provide ‘java’.
Selection Command
-----------------------------------------------
*+ 1 /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
2 /usr/lib/jvm/jre-1.8.0-openjdk.x86_64/bin/javaEnter to keep the current selection[+], or type selection number: 2
java -version
openjdk version “1.8.0_151”
OpenJDK Runtime Environment (build 1.8.0_151-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)
Elasticsearch
1. ソースのダウンロード、インストール
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.0.tar.gz
※最新は6.3.1だったが、6.3.1だとKibanaとの接続がうまくできなかったのでこちらを利用。
cd /var/www
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.0.tar.gz
tar zxvf elasticsearch-5.5.0.tar.gz
2. 設定変更
cd elasticsearch-5.5.0
vi config/elasticsearch.yml
network.host: 192.168.0.1
↓
network.host: 0.0.0.0
#http.port: 9200
↓
http.port: 9200
sudo sysctl -w vm.max_map_count=262144
※elasticsearchを起動時にエラーが出たのであとから実行した。環境によっては必要ないかも。
3. elasticsearchユーザーを追加
elasticsearchはrootでは起動できないので専用ユーザーを追加する
useradd elasticsearch
passwd elasticsearch
4.オーナー変更
chown -R elasticsearch:elasticsearch elasticsearch-5.5.0
5. 9200ポートを、AWSセキュリティーグループで許可
6. 起動
elasticsearchユーザーで、
/usr/local/share/elasticsearch-5.5.0/bin/elasticsearch -d
7. 確認
http://任意のIP:9200/
以下のようなのが表示できれば成功
{
"name" : "3hNe3yY",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "07rIcx46QNCtNUMV4nUyAw",
"version" : {
"number" : "5.5.0",
"build_hash" : "260387d",
"build_date" : "2017-06-30T23:16:05.735Z",
"build_snapshot" : false,
"lucene_version" : "6.6.0"
},
"tagline" : "You Know, for Search"
}
Kibana
1. yumでインストール
sudo yum -y install kibana
2. 設定変更
vi /etc/kibana/kibana.yml
以下を追加
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.url: "http://[ElasticSearchのIP]:9200"
3. 起動
sudo service kibana restart
4. 5601ポートを、AWSセキュリティーグループで許可
9200の許可したとの同様。
5. 確認
http://任意のIP:5601
特にエラーが出なければOK
fluentd
1. yumでインストール
sudo yum install td-agent
fluent-gem install fluent-plugin-elasticsearch
2. 設定変更。以下を追加。
vi /etc/td-agent/td-agent.conf
<source>
type tail
path /var/log/httpd/access_log
pos_file /var/log/td-agent/access_log.pos
format apache2
tag apache.access
</source>
<match apache.access>
type elasticsearch
host localhost
port 9200
type_name access_log
logstash_format true
flush_interval 3s
</match>
3. マッピング
事前に以下を記載したmapping.jsonを用意
{
"mappings" : {
"site_a" : {
"properties" : {
"remote-host" : { "type" : "string" },
"remote-log-name" : { "type" : "string" },
"request-user" : { "type" : "string" },
"request-time" : { "type" : "date", "format" : "yyyy-MM-dd HH:mm:ss z" },
"request-line" : { "type" : "string" },
"response-status" : { "type" : "long" },
"response-bytes" : { "type" : "long" },
"request-header-Referer" : { "type" : "string" },
"request-header-User-Agent" : { "type" : "string" }
}
}
}
}
4.elasticsearchに追加
curl -XPOST http://localhost:9200/access_log
curl -XPOST 'localhost:9200/access_log' -d @mapping.json
アクセスログのテスト用ログを出力する
1. apache-loggenをインストール
sudo gem install apache-loggen --no-ri --no-rdoc -V
2. 実行
apache-loggen --rate=10 --limit=100 --progress /var/log/httpd/access_log