ElasticsearchをDockerで動かしてGrafanaで可視化する

elasticsearchdockermonitoring

https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html

vm.max_map_count (バーチャルメモリにマッピングできる最大ページ数) を262144以上にする。

$ sysctl vm.max_map_count
$ grep vm.max_map_count /etc/sysctl.conf
vm.max_map_count=262144
# sysctl -w vm.max_map_count=262144

30日トライアル後、有償ライセンスが必要になるxpackのsecurity(旧sheild)がデフォルトで有効になっているのでfalseにしている。

-cap-add=IPC_LOCKでLock memory(スワップアウトしないようにする)を 許可する。

https://www.elastic.co/guide/en/elasticsearch/reference/5.2/heap-size.html

ES_HEAP_SIZEでヒープ領域を設定する。デフォルトでは2GB。多いほどキャッシュに使用できる。 ただし、物理RAMの50%以下で、32GB近辺の境界を超えないようにする

$ mkdir -p ~/do/elasticsearch/data
$ docker run -itd -v elasticsearch:/usr/share/elasticsearch/data \
--name elasticsearch \
-p 9200:9200 \
-e xpack.security.enabled=false \
-e bootstrap.memory_lock=true -e cluster.name=hogehoge-cluster -e ES_JAVA_OPTS="-Xms28g -Xmx28g" \
--cap-add=IPC_LOCK --ulimit memlock=-1:-1 --ulimit nofile=65536:65536 \
--restart=always \
docker.elastic.co/elasticsearch/elasticsearch:5.1.2

$ docker volume ls
local               elasticsearch

問題なく起動しているか確認する。

$ curl localhost:9200 | jq
{
  "name": "eqIkJ48",
  "cluster_name": "docker-cluster",
  "cluster_uuid": "Lsu_C7wORS6G-0m9PJ9sFQ",
  "version": {
    "number": "5.1.2",
    "build_hash": "c8c4c16",
    "build_date": "2017-01-11T20:18:39.146Z",
    "build_snapshot": false,
    "lucene_version": "6.3.0"
  },
  "tagline": "You Know, for Search"
}

dynamic mapping を無効にする場合

$ curl -XPUT 'localhost:9200/_template/template_all?pretty' -d'
{
  "template": "*",
  "order":0,
  "settings": {
    "index.mapper.dynamic": false 
  }
}
'

Kuromojiを入れる場合

$ docker exec -it ***** bin/elasticsearch-plugin install analysis-kuromoji
$ docker restart  *****

Grafanaで可視化する

前はInfluxDBとつなげたが、Elasticsearchにも対応している。

$ mkdir -p /var/lib/grafana/plugins
$ docker run -itd --restart=always -p 3000:3000 -v grafana:/var/lib/grafana grafana/grafana

http://docs.grafana.org/datasources/elasticsearch/

admin/adminでログインして、data sourceを追加する。AccessはProxyにする。

適当にデータを入れて表示してみる。

$ curl -XPUT 'localhost:9200/test_index?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "test_type": { 
      "_all":       { "enabled": false  }, 
      "properties": { 
        "name": { "type": "string" },
        "age":    { "type": "integer"  },
        "timestamp": { "type": "date", "format": "epoch_millis" }
      }
    }
  }
}
'

$ curl 'localhost:9200/_cat/indices?format=json&pretty'
[
  {
    "health" : "yellow",
    "status" : "open",
    "index" : "test_index",
    "uuid" : "kVbt2V-rS2m6vhplIkMKNg",
    "pri" : "5",
    "rep" : "1",
    "docs.count" : "0",
    "docs.deleted" : "0",
    "store.size" : "260b",
    "pri.store.size" : "260b"
  },
  ...
]

$ curl -XPOST 'localhost:9200/test_index/test_type?pretty' -H 'Content-Type: application/json' -d'
{
    "name": "hoge fuga",
    "age": 24,
    "timestamp": 1485676393044
}
'

$ curl 'localhost:9200/test_index/test_type/_search?pretty'
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_index",
        "_type" : "test_type",
        "_id" : "AVnpOGrseo3fDHi0SK-P",
        "_score" : 1.0,
        "_source" : {
          "name" : "hoge fuga",
          "age" : 24,
          "timestamp" : 1485676393044
        }
      }
    ]
  }
}

表示してみた