読者です 読者をやめる 読者になる 読者になる

Elasticsearch 1.4.0 を Docker で動かしてみる -- boot2docker

Docker MacBookAir Elasticsearch

準備の準備

まずは、boot2dockerをバージョンアップする。

OSX Installer

インストーラを新しくする。

https://github.com/boot2docker/osx-installer/releases/latest

DawnLoad後、Installする。

その後、以下のコマンドを実行する。
この辺りは、Dockerのサイトにのっているとうり。。

MBA20120331:dockerfile-elasticsearch4 guutara$ boot2docker stop
MBA20120331:dockerfile-elasticsearch4 guutara$ boot2docker download
Latest release for boot2docker/boot2docker is v1.3.1
Downloading boot2docker ISO image...
Success: downloaded https://github.com/boot2docker/boot2docker/releases/download/v1.3.1/boot2docker.iso
	to /Users/guutara/.boot2docker/boot2docker.iso
MBA20120331:dockerfile-elasticsearch4 guutara$ boot2docker start
Waiting for VM and Docker daemon to start...
.....................ooooooooooooooooooooooooo
Started.
Writing /Users/guutara/.boot2docker/certs/boot2docker-vm/ca.pem
Writing /Users/guutara/.boot2docker/certs/boot2docker-vm/cert.pem
Writing /Users/guutara/.boot2docker/certs/boot2docker-vm/key.pem

To connect the Docker client to the Docker daemon, please set:
    export DOCKER_HOST=tcp://192.168.59.103:2376
    export DOCKER_CERT_PATH=/Users/guutara/.boot2docker/certs/boot2docker-vm
    export DOCKER_TLS_VERIFY=1

使う前に、上記、3つの変数は、設定しておこう。。

Docker Image の作成。

DockerFile とかを、適当に作って置いたので、これを使っている。
dockerfile-elasticsearch4

まずは、boot2dockerを起動する。

MBA20120331:dockerfile-elasticsearch4 guutara$ boot2docker start
Waiting for VM and Docker daemon to start...
.....................ooooooooooooooooooooooooo
Started.
Writing /Users/guutara/.boot2docker/certs/boot2docker-vm/ca.pem
Writing /Users/guutara/.boot2docker/certs/boot2docker-vm/cert.pem
Writing /Users/guutara/.boot2docker/certs/boot2docker-vm/key.pem

To connect the Docker client to the Docker daemon, please set:
    export DOCKER_HOST=tcp://192.168.59.103:2376
    export DOCKER_CERT_PATH=/Users/guutara/.boot2docker/certs/boot2docker-vm
    export DOCKER_TLS_VERIFY=1

MBA20120331:dockerfile-elasticsearch4 guutara$ export DOCKER_HOST=tcp://192.168.59.103:2376
MBA20120331:dockerfile-elasticsearch4 guutara$     export DOCKER_CERT_PATH=/Users/guutara/.boot2docker/certs/boot2docker-vm
MBA20120331:dockerfile-elasticsearch4 guutara$     export DOCKER_TLS_VERIFY=1
MBA20120331:dockerfile-elasticsearch4 guutara$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Dockerfile のあるディレクトリで行う。

MBA20120331:dockerfile-elasticsearch4 guutara$ pwd
/Users/guutara/git/dockerfile-elasticsearch4
MBA20120331:dockerfile-elasticsearch4 guutara$ ls
Dockerfile  README.md  config  data
MBA20120331:dockerfile-elasticsearch4 guutara$ docker build -t guutara/elasticsearch:1.4.0 .
Sending build context to Docker daemon 29.46 MB
Sending build context to Docker daemon 
Step 0 : FROM dockerfile/java:oracle-java7
 ---> a643f54fef40
.
.
.
Successfully built cffbdbd80f9c
MBA20120331:dockerfile-elasticsearch4 guutara$ 

MBA20120331:dockerfile-elasticsearch4 guutara$ docker images
REPOSITORY              TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
guutara/elasticsearch   1.4.0               cffbdbd80f9c        About a minute ago   755.9 MB
guutara/kibana          4.0.0-BETA1         e3c7448aefd5        3 weeks ago          766.4 MB
guutara/elasticsearch   1.3.2               e1b42108cc83        3 weeks ago          755.5 MB
dockerfile/java         oracle-java7        a643f54fef40        4 weeks ago          722 MB
chung/centos7-up        latest              3a22ebba07c7        7 weeks ago          288.3 MB
centos                  centos7             70214e5d0a90        9 weeks ago          224 MB

これで、Imageは、作成完了。

Container の作成から起動確認

Docker -name で、名前をつけて作成して起動する。

MBA20120331:dockerfile-elasticsearch4 guutara$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
MBA20120331:dockerfile-elasticsearch4 guutara$ docker run -d -p 9200:9200 -p 9300:9300 -name el1.4-up guutara/elasticsearch:1.4.0
Warning: '-name' is deprecated, it will be replaced by '--name' soon. See usage.
3338665c2dfb431de5f51889436958e0954e5ed3f37277e5d1f19dadaa95a561
MBA20120331:dockerfile-elasticsearch4 guutara$ docker ps
CONTAINER ID        IMAGE                         COMMAND                CREATED             STATUS              PORTS                                            NAMES
3338665c2dfb        guutara/elasticsearch:1.4.0   "/elasticsearch/bin/   8 seconds ago       Up 7 seconds        0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp   el1.4-up            

DOCKER_HOSTのIPを、何度も叩くのは、面倒なので DOCKER_HOST_IP に設定しておく。

MBA20120331:data guutara$ export DOCKER_HOST_IP=192.168.59.103
MBA20120331:data guutara$ echo $DOCKER_HOST_IP
192.168.59.103


まずは、起動を確認。

MBA20120331:dockerfile-elasticsearch4 guutara$ curl http://$DOCKER_HOST_IP:9200/
{
  "status" : 200,
  "name" : "Kristoff Vernard",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "1.4.0",
    "build_hash" : "bc94bd81298f81c656893ab1ddddd30a99356066",
    "build_timestamp" : "2014-xx-xxxxxxxx",
    "build_snapshot" : false,
    "lucene_version" : "4.10.2"
  },
  "tagline" : "You Know, for Search"
}

Index作成と、データを検索してみる。使ったのは、kibanaで提供されているデータ。
curl -O http://www.elasticsearch.org/guide/en/kibana/current/snippets/shakespeare.jsonで、データを取得しておく。
先ほどの、gitのdata配下には、データと以下の、しょぼい、shがあるので、それを使っている。

MBA20120331:data guutara$ cat curl-shakespeare 
#/bin/sh
#

echo "Start Index Create" 
curl -XPUT http://$DOCKER_HOST_IP:9200/shakespeare -d '
{
 "mappings" : {
  "_default_" : {
   "properties" : {
    "speaker" : {"type": "string", "index" : "not_analyzed" },
    "play_name" : {"type": "string", "index" : "not_analyzed" },
    "line_id" : { "type" : "integer" },
    "speech_number" : { "type" : "integer" }
   }
  }
 }
}
';

echo

echo "Start Bulk Load"

curl -XPUT  http://$DOCKER_HOST_IP:9200/_bulk --data-binary @shakespeare.json

echo
echo "End "

MBA20120331:data guutara$ sh curl-shakespeare > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   305  100    21  100   284     18    255  0:00:01  0:00:01 --:--:--   255
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 33.6M  100 9791k  100 24.0M   408k  1027k  0:00:23  0:00:23 --:--:--     0

確認してみる。

Indexの確認。

MBA20120331:data guutara$ curl -XGET http://$DOCKER_HOST_IP:9200/_aliases?pretty
{
  "shakespeare" : { }
}


データの確認。

MBA20120331:data guutara$ curl -XGET http://$DOCKER_HOST_IP:9200/shakespeare/_search?
{"took":78,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":111396,"max_score":1.0,"hits":[{"_index":"shakespeare","_type":"line","_id":"46925","_score":1.0,"_source":{"line_id":46926,"play_name":"Julius Caesar","speech_number":13,"line_number":"","speaker":"BRUTUS","text_entry":"Exit LUCIUS"}},{"_index":"shakespeare","_type":"line","_id":"46932","_score":1.0,"_source":{"line_id":46933,"play_name":"Julius Caesar","speech_number":13,"line_number":"2.1.67","speaker":"BRUTUS","text_entry":"Are then in council; and the state of man,"}},{"_index":"shakespeare","_type":"line","_id":"46937","_score":1.0,"_source":{"line_id":46938,"play_name":"Julius Caesar","speech_number":14,"line_number":"2.1.71","speaker":"LUCIUS","text_entry":"Who doth desire to see you."}},{"_index":"shakespeare","_type":"line","_id":"46944","_score":1.0,"_source":{"line_id":46945,"play_name":"Julius Caesar","speech_number":18,"line_number":"2.1.78","speaker":"LUCIUS","text_entry":"By any mark of favour."}},{"_index":"shakespeare","_type":"line","_id":"46949","_score":1.0,"_source":{"line_id":46950,"play_name":"Julius Caesar","speech_number":19,"line_number":"2.1.82","speaker":"BRUTUS","text_entry":"When evils are most free? O, then by day"}},{"_index":"shakespeare","_type":"line","_id":"46951","_score":1.0,"_source":{"line_id":46952,"play_name":"Julius Caesar","speech_number":19,"line_number":"2.1.84","speaker":"BRUTUS","text_entry":"To mask thy monstrous visage? Seek none, conspiracy;"}},{"_index":"shakespeare","_type":"line","_id":"46956","_score":1.0,"_source":{"line_id":46957,"play_name":"Julius Caesar","speech_number":19,"line_number":"","speaker":"BRUTUS","text_entry":"Enter the conspirators, CASSIUS, CASCA, DECIUS BRUTUS, CINNA, METELLUS CIMBER, and TREBONIUS"}},{"_index":"shakespeare","_type":"line","_id":"46963","_score":1.0,"_source":{"line_id":46964,"play_name":"Julius Caesar","speech_number":22,"line_number":"2.1.95","speaker":"CASSIUS","text_entry":"You had but that opinion of yourself"}},{"_index":"shakespeare","_type":"line","_id":"46968","_score":1.0,"_source":{"line_id":46969,"play_name":"Julius Caesar","speech_number":25,"line_number":"2.1.100","speaker":"BRUTUS","text_entry":"He is welcome too."}},{"_index":"shakespeare","_type":"line","_id":"46970","_score":1.0,"_source":{"line_id":46971,"play_name":"Julius Caesar","speech_number":27,"line_number":"2.1.102","speaker":"BRUTUS","text_entry":"They are all welcome."}}]}}

まとめ

dockerfile/elasticsearch · GitHubを参考に、見よう見まねで、作ってみたけど、なんとか動いた。
boot2docker で、動かしてるけど、普通にDockerいれて物理で動かすならもっと違うやり方が出来ると思う。

ちなみに、データの永続化はしてないので、コンテナ消すと全てはなくなるのだよ。(笑)

あと、kibana4も、作ったのだけど、それは、また、気が向いた時にまとめるつもり。