SolrCloud検証環境をVagrant+Ansibleで簡単自動構築

SolrCloudを簡単に試せるよう、VagrantとAnsibleで構築できるようにしました。

引用元:https://wiki.apache.org/solr/SolrCloud

SolrCloudの構成

以下のような構成で構築します。

f:id:yomon8:20161002200434p:plain

Solrサーバ

記事書いている時点ではSolrは 6.2.1 を使っています。

ホスト名 IP Solr Node Port
solr1 192.168.110.11 solr_node_8901 8901
- - solr_node_8902 8902
solr2 192.168.110.12 solr_node_8901 8901
- - solr_node_8902 8902

ZooKeeperサーバ

記事書いている時点ではZooKeeperは 3.4.9 を使っています。

ホスト名 IP Port
zk1 192.168.110.100 2181

インストール手順(Vagrant+Ansible)

以下に、Vagrantfileとansibleを準備してあるので簡単に環境準備できるはずです。

github.com

実際の手順

なお、手元の環境は以下になります。

  • OS X EI Capitan 10.11.6
  • ansible 2.0.2.0
  • Vagrant 1.8.1

構築自体は以下のコマンドを順番に実行するだけです。ネットワークの帯域次第ですが、30〜60分くらいで処理完了するかと思います。

$ git clone https://github.com/yomon8/solrcloud.git
$ cd solrcloud
$ vagrant up --no-provision && vagrant ssh-config > sshconfig && vagrant provision

終わったらブラウザからSolrCloudにアクセスしてみます。

http://192.168.110.11:8901/solr/#/~cloud

この時点ではCloudの中身見ても、何も登録されていない状態で、以下のような画面が表示されるはずです。

f:id:yomon8:20161002200505p:plain

コレクション作成

コレクション mycollection1 を作成してみます。

[solr@solr1 ~]$ /opt/solr_node_8901/bin/solr create_collection -c mycollection1 -shards 2 -replicationFactor 2

実行結果は以下のようになりました。ZooKeeper側にもアクセスしてShardが構成されているのが読み取れます。

Connecting to ZooKeeper at 192.168.110.100:2181 ...
Uploading /opt/solr_node_8901/server/solr/configsets/data_driven_schema_configs/conf for config mycollection1 to ZooKeeper at 192.168.110.100:2181

Creating new collection 'mycollection1' using command:
http://localhost:8902/solr/admin/collections?action=CREATE&name=mycollection1&numShards=2&replicationFactor=2&maxShardsPerNode=1&collection.configName=mycollection1

{
  "responseHeader":{
    "status":0,
    "QTime":20587},
  "success":{
    "192.168.110.11:8902_solr":{
      "responseHeader":{
        "status":0,
        "QTime":10449},
      "core":"mycollection1_shard2_replica2"},
    "192.168.110.11:8901_solr":{
      "responseHeader":{
        "status":0,
        "QTime":10453},
      "core":"mycollection1_shard1_replica1"},
    "192.168.110.12:8901_solr":{
      "responseHeader":{
        "status":0,
        "QTime":10413},
      "core":"mycollection1_shard2_replica1"},
    "192.168.110.12:8902_solr":{
      "responseHeader":{
        "status":0,
        "QTime":10779},
      "core":"mycollection1_shard1_replica2"}}}

この時点で mycollection1 が作成されてShardとNodeの状態はこのようになっています。

f:id:yomon8:20161002200520p:plain

同様に mycollection2 についても作成しておきます。

[solr@solr1 ~]$ /opt/solr_node_8901/bin/solr create_collection -c mycollection2 -shards 2 -replicationFactor 2

これで目標の環境は構築できました。

f:id:yomon8:20161002200529p:plain

ZooKeeperでSolrの情報確認

ZooKeeper側でどのように情報が格納されているか確認してみます。

ZooKeeperである zk1 サーバにSSHで入って、ZooKeeperクライアントを起動します。

[vagrant@zk1 ~]$ /opt/zookeeper/bin/zkCli.sh

Solrのノード情報や、コレクション情報が登録されていることが確認できます。

[zk: localhost:2181(CONNECTED) 1] ls /
[zookeeper, overseer, aliases.json, live_nodes, collections, overseer_elect, security.json, clusterstate.json]

[zk: localhost:2181(CONNECTED) 2] ls /live_nodes
[192.168.110.12:8901_solr, 192.168.110.11:8902_solr, 192.168.110.12:8902_solr, 192.168.110.11:8901_solr]

[zk: localhost:2181(CONNECTED) 3] ls /collections
[mycollection2, mycollection1]

テストデータ投入

mycollection1 側にSolr側で準備されているテストデータを投入してみます。

[solr@solr1 ~]$ /opt/solr_node_8901/bin/post -c mycollection1 -p 8901 /opt/solr_node_8901/example/exampledocs/*.xml

色々なノードに検索投げてみたり、

curl -s "http://192.168.110.11:8901/solr/mycollection1/select?indent=on&q=compName_s:Apple&wt=json"

curl -s "http://192.168.110.11:8902/solr/mycollection1/select?indent=on&q=compName_s:Apple&wt=json"

curl -s "http://192.168.110.12:8901/solr/mycollection1/select?indent=on&q=compName_s:Apple&wt=json"

curl -s "http://192.168.110.12:8902/solr/mycollection1/select?indent=on&q=compName_s:Apple&wt=json"

Shard変えながら検索してみたりして実機で動き確認できます。

curl -s "http://192.168.110.11:8901/solr/mycollection1/select?indent=on&q=compName_s:Apple&wt=json&shards=shard1,shard2"

curl -s "http://192.168.110.11:8901/solr/mycollection1/select?indent=on&q=compName_s:Apple&wt=json&shards=shard1"

curl -s "http://192.168.110.11:8901/solr/mycollection1/select?indent=on&q=compName_s:Apple&wt=json&shards=shard2"

参考URL

What is SolrCloud? (And how does it compare to master-slave?) – Lucidworks Support

Collections API - Apache Solr Reference Guide - Apache Software Foundation

SolrCloud - Apache Solr Reference Guide - Apache Software Foundation

Apache SolrCloudでクラスタ環境構築入門 (クラスタ構築〜分散インデシング〜フェールオーバーまで) - Qiita