Cluster機能を使う – AWSで始めるElasticSearch(2)

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

出張ブロガーの@smokeymonkeyです。今回はElasticSearchのCluster機能を使ってみました。

Cluster設定

ElasticSearchの設定は/etc/elasticsearch/elasticsearch.ymlで行いますが、このファイルの冒頭には以下のように記載されています。

Most of the time, these defaults are just fine for running a production cluster.

ということで細かい設定せずにこのままいっちゃいます。

まず、ElasticSearchをセットアップした2台のEC2を用意し、セキュリティグループで9200/tcp、9300/tcpのInboundをオープンします。

ElasticSearchはAPI over HTTPで9200-9300/tcpを、クラスタ間の内部通信を行うtransportモジュールで9300-9400/tcpを使用します)

初期状態のノードAのクラスタの状態は以下の通りです。

$ curl localhost:9200/_cluster/nodes/_local
{
    "array": {
        "ok": true,
        "cluster_name": "elasticsearch",
        "nodes": {
            "-saA5swFSpWxPlnGV9kGcQ": {
                "name": "Shocker",
                "transport_address": "inet[/172.31.2.129:9300]",
                "hostname": "ip-172-31-2-129",
                "version": "0.90.5",
                "http_address": "inet[/172.31.2.129:9200]"
            }
        }
    }
}

$ sudo cat /var/log/elasticsearch.log
[2013-10-29 00:53:20,943][INFO ][node                     ] [Shocker] version[0.90.5], pid[4321], build[c8714e8/2013-09-17T13:09:46Z]
[2013-10-29 00:53:20,944][INFO ][node                     ] [Shocker] initializing ...
[2013-10-29 00:53:20,952][INFO ][plugins                  ] [Shocker] loaded [], sites []
[2013-10-29 00:53:24,308][INFO ][node                     ] [Shocker] initialized
[2013-10-29 00:53:24,309][INFO ][node                     ] [Shocker] starting ...
[2013-10-29 00:53:24,520][INFO ][transport                ] [Shocker] bound_address {inet[/0:0:0:0:0:0:0:0:9300]}, publish_address {inet[/172.31.2.129:9300]}
[2013-10-29 00:53:27,564][INFO ][cluster.service          ] [Shocker] new_master [Shocker][-saA5swFSpWxPlnGV9kGcQ][inet[/172.31.2.129:9300]], reason: zen-disco-join (elected_as_master)
[2013-10-29 00:53:27,614][INFO ][discovery                ] [Shocker] elasticsearch/-saA5swFSpWxPlnGV9kGcQ
[2013-10-29 00:53:27,642][INFO ][http                     ] [Shocker] bound_address {inet[/0:0:0:0:0:0:0:0:9200]}, publish_address {inet[/172.31.2.129:9200]}
[2013-10-29 00:53:27,642][INFO ][node                     ] [Shocker] started
[2013-10-29 00:53:27,668][INFO ][gateway                  ] [Shocker] recovered [0] indices into cluster_state

同じく初期状態のノードBのクラスタの状態は以下の通りです。

$ curl localhost:9200/_cluster/nodes/_local
{
    "array": {
        "ok": true,
        "cluster_name": "elasticsearch",
        "nodes": {
            "sHxbUNgxRieV2z9Pyt9ZVA": {
                "name": "Mister Machine",
                "transport_address": "inet[/172.31.8.224:9300]",
                "hostname": "ip-172-31-8-224",
                "version": "0.90.5",
                "http_address": "inet[/172.31.8.224:9200]"
            }
        }
    }
}
$ sudo cat /var/log/elasticsearch.log
[2013-10-29 02:20:24,468][INFO ][node                     ] [Mister Machine] version[0.90.5], pid[1346], build[c8714e8/2013-09-17T13:09:46Z]
[2013-10-29 02:20:24,469][INFO ][node                     ] [Mister Machine] initializing ...
[2013-10-29 02:20:24,476][INFO ][plugins                  ] [Mister Machine] loaded [], sites []
[2013-10-29 02:20:28,881][INFO ][node                     ] [Mister Machine] initialized
[2013-10-29 02:20:28,882][INFO ][node                     ] [Mister Machine] starting ...
[2013-10-29 02:20:29,161][INFO ][transport                ] [Mister Machine] bound_address {inet[/0:0:0:0:0:0:0:0:9300]}, publish_address {inet[/172.31.8.224:9300]}
[2013-10-29 02:20:32,240][INFO ][cluster.service          ] [Mister Machine] new_master [Mister Machine][sHxbUNgxRieV2z9Pyt9ZVA][inet[/172.31.8.224:9300]], reason: zen-disco-join (elected_as_master)
[2013-10-29 02:20:32,292][INFO ][discovery                ] [Mister Machine] elasticsearch/sHxbUNgxRieV2z9Pyt9ZVA
[2013-10-29 02:20:32,321][INFO ][http                     ] [Mister Machine] bound_address {inet[/0:0:0:0:0:0:0:0:9200]}, publish_address {inet[/172.31.8.224:9200]}
[2013-10-29 02:20:32,322][INFO ][node                     ] [Mister Machine] started
[2013-10-29 02:20:32,356][INFO ][gateway                  ] [Mister Machine] recovered [0] indices into cluster_state

同一サブネットで、同一クラスタ名("elasticsearch")なのに、それぞれが別のクラスタのマスタになっています。何故でしょう?

そこでdiscoveryの挙動について調べてみます。elasticsearch.ymlに以下の記載がありました。

# Discovery infrastructure ensures nodes can be found within a cluster
# and master node is elected. Multicast discovery is the default.

Discoveryはデフォルトではマルチキャストを使ってmaster nodeを検索しに行くようです。

しかしAmazon Virtual Private Cloud FAQに

Q: Amazon VPC は、マルチキャストまたはブロードキャストをサポートしますか?
いいえ。

と記載されています...Amazon VPCではmulticast discoveryは使えません。

それではunicast discoveryを使った構成で構築してみます

ノードA、ノードB共に、elasticsearch.yamlを修正し、multicast discoveryを無効化し、unicast対象のmaster nodeとなるホストを記載します。ここではノードAとノードBのIPアドレスを記載しました。

$ sudo vi /etc/elasticsearch/elasticsearch.yml
discovery.zen.ping.multicast.enabled: false
discovery.zen.ping.unicast.hosts: ["172.31.2.129","172.31.8.224"]

ではノードAでサービスを起動してみます。なお[***.log]の***の部分はcluster nameになります。

$ sudo service elasticsearch start
$ cat /var/log/elasticsearch/elasticsearch.log
[2013-10-29 05:11:24,797][INFO ][node                     ] [Stranger] version[0.90.5], pid[21630], build[c8714e8/2013-09-17T13:09:46Z]
[2013-10-29 05:11:24,797][INFO ][node                     ] [Stranger] initializing ...
[2013-10-29 05:11:24,815][INFO ][plugins                  ] [Stranger] loaded [cloud-aws], sites []
[2013-10-29 05:11:28,382][INFO ][node                     ] [Stranger] initialized
[2013-10-29 05:11:28,382][INFO ][node                     ] [Stranger] starting ...
[2013-10-29 05:11:28,566][INFO ][transport                ] [Stranger] bound_address {inet[/0:0:0:0:0:0:0:0:9300]}, publish_address {inet[/172.31.2.129:9300]}
[2013-10-29 05:11:31,615][INFO ][cluster.service          ] [Stranger] new_master [Stranger][LajRpjtATZma4sBlgz96NQ][inet[/172.31.2.129:9300]], reason: zen-disco-join (elected_as_master)
[2013-10-29 05:11:31,631][INFO ][discovery                ] [Stranger] elasticsearch/LajRpjtATZma4sBlgz96NQ
[2013-10-29 05:11:31,661][INFO ][http                     ] [Stranger] bound_address {inet[/0:0:0:0:0:0:0:0:9200]}, publish_address {inet[/172.31.2.129:9200]}
[2013-10-29 05:11:31,661][INFO ][node                     ] [Stranger] started
[2013-10-29 05:11:32,643][INFO ][gateway                  ] [Stranger] recovered [1] indices into cluster_state

ノードAがmasterとして起動しました。

次にノードBのサービスを再起動します。

$ sudo service elasticsearch start
$ cat /var/log/elasticsearch/elasticsearch.log
[2013-10-29 05:11:59,093][INFO ][node                     ] [Smuggler I] version[0.90.5], pid[18229], build[c8714e8/2013-09-17T13:09:46Z]
[2013-10-29 05:11:59,094][INFO ][node                     ] [Smuggler I] initializing ...
[2013-10-29 05:11:59,111][INFO ][plugins                  ] [Smuggler I] loaded [cloud-aws], sites []
[2013-10-29 05:12:02,621][INFO ][node                     ] [Smuggler I] initialized
[2013-10-29 05:12:02,621][INFO ][node                     ] [Smuggler I] starting ...
[2013-10-29 05:12:02,796][INFO ][transport                ] [Smuggler I] bound_address {inet[/0:0:0:0:0:0:0:0:9300]}, publish_address {inet[/172.31.8.224:9300]}
[2013-10-29 05:12:05,886][INFO ][cluster.service          ] [Smuggler I] detected_master [Stranger][LajRpjtATZma4sBlgz96NQ][inet[/172.31.2.129:9300]], added {[Stranger][LajRpjtATZma4sBlgz96NQ][inet[/172.31.2.129:9300]],}, reason: zen-disco-receive(from master [[Stranger][LajRpjtATZma4sBlgz96NQ][inet[/172.31.2.129:9300]]])
[2013-10-29 05:12:05,918][INFO ][discovery                ] [Smuggler I] elasticsearch/S6ZtXTxAS_2c-1q9NuGOQQ
[2013-10-29 05:12:05,929][INFO ][http                     ] [Smuggler I] bound_address {inet[/0:0:0:0:0:0:0:0:9200]}, publish_address {inet[/172.31.8.224:9200]}
[2013-10-29 05:12:05,930][INFO ][node                     ] [Smuggler I] started

ノードA(Stranger)をMasterとして発見しました。

ノードAのログを見ると、ノードB(Smuggler I)がクラスタに追加されたことがわかります。

[2013-10-29 05:12:05,868][INFO ][cluster.service ] [Stranger] added {[Smuggler I][S6ZtXTxAS_2c-1q9NuGOQQ][inet[/172.31.8.224:9300]],}, reason: zen-disco-receive(join from node[[Smuggler I][S6ZtXTxAS_2c-1q9NuGOQQ][inet[/172.31.8.224:9300]]])

動作確認

ノードAでPUTしてデータを登録します。

$ curl -XPUT http://localhost:9200/cltest/test/1 -d '
>  {
>    "title" : "test",
>    "text"  : "node A"
>  }'
{"ok":true,"_index":"cltest","_type":"test","_id":"1","_version":1}

次にノードBでGETしてデータを抽出してみます。

$ curl -XGET http://localhost:9200/cltest/test/_search -d '
>  {
>    "query":
>    { "match":{"title":"test"}}
>  }'
{
    "array": {
        "took": 86,
        "timed_out": false,
        "_shards": {
            "total": 5,
            "successful": 5,
            "failed": 0
        },
        "hits": {
            "total": 1,
            "max_score": 0.30685282,
            "hits": [
                {
                    "_index": "cltest",
                    "_type": "test",
                    "_id": "1",
                    "_score": 0.30685282,
                    "_source": {
                        "title": "test",
                        "text": "node A"
                    }
                }
            ]
        }
    }
}

ちゃんと分散されていることが確認できました!

まとめ

Cluster機能を使って高可用性を確保できることが分かりました。複数のAZにmaster nodeを分散して配置しフロントにELBを置けばAZが停止する事態にも対応できます。