Chefを使ってCassandraを自動構築した作業メモ

cassandraの検証環境簡単に構築したいと思い誰かchefで公開していないかと思いググったところ、以下のレポジトリ見つけて、Vagrantfileも入っていたので早速使ってみました。

github.com

Vagrant Boxの準備

Atlas認証

Vagrantfileの中を見ると以下の記述があります。

config.vm.box = 'chef/centos-6.5'

Hashicorp AtlasのVagrant Boxを使うようなので、Vagrant BoxをダウンロードするためにAtlasアカウントでログオンします。

$ vagrant login
In a moment we will ask for your username and password to HashiCorp's
Atlas. After authenticating, we will store an access token locally on
disk. Your login details will be transmitted over a secure connection, and
are never stored on disk locally.

If you do not have an Atlas account, sign up at
https://atlas.hashicorp.com.

Atlas Username: your_user_name
Password (will be hidden): 
You are now logged in.

アカウントが無い場合は以下からアカウント登録しておきます。

https://atlas.hashicorp.com/account/new

Boxを探す

Repositoryの中のVagrantfileにはchef社のVagrant Boxが設定されていますが、chef社のVagrant Boxはbento organizationに移行しているようです。 http://blog.chopschips.net/blog/2015/08/28/chef-vagrant-box-migrate-to-bento/

ということで、今回はbentoの方から近いバージョンのCentOSのBoxを使います。 https://atlas.hashicorp.com/bento/boxes/centos-6.7

Vagrant Boxをダウンロードします。

$ vagrant box add bento/centos-6.7 --provider virtualbox

数分〜数十分でダウンロードが完了するはずです。

ダウンロードが完了したら、想定通りのBoxがダウンロードできているか確認してみます。

$ vagrant box list
bento/centos-6.7  (virtualbox, 2.2.3)   

Vagrant pluginインストール

vagrant-berkshelfのプラグインが必要なのでインストールしておきます。

$ vagrant plugin install vagrant-berkshelf

Vagrantfile調整

Vagrantfileを修正します。

ダウンロードしたVagrant Boxに合わせてVagrantfileを調整します。

#  config.vm.box = 'chef/centos-6.5'
  config.vm.box = 'chef/centos-6.5'

あまり小さいメモリだとcassandra上がってこないので、必要に応じてVMへのメモリの割り当て量を調整します。

   config.vm.provider :virtualbox do |vb|
          vb.memory = "1024" 
   end

必須のパラメータであるcluster_nameの設定をします。このパラメータはattributes/config.rbにて以下のように定義されています。

default['cassandra']['config']['cluster_name'] = nil

それに合わせてパラメータの記載部分を修正します。

config.vm.provision :chef_solo do |chef|
#   chef.json = {
#     cassandra: {
#       cluster_name: 'vagrant-test'
#     }
#   }
   chef.json = {
     cassandra: {
       config: {
         cluster_name: 'vagrant-test'
       }
     }
   }

VM起動・稼働確認

これで準備は整いました。起動してみます。

$ cd ./cassandra-chef-cookbook
$ vagrant up

SSHでログオンします。

$ vagrant ssh

cassandraは自動起動に設定されているので、この時点で起動されているはずです。

$ sudo service cassandra status
cassandra (pid  1216) is running...

起動確認してみます。

$ nodetool -h localhost status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address    Load       Tokens       Owns    Host ID                               Rack
UN  10.0.2.15  96.98 KB   256          ?       5ff11f90-1c0f-4a58-a3ee-f27f7f8596d3  rack1
$ cqlsh
Connected to vagrant-test at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 2.2.0 | CQL spec 3.3.0 | Native protocol v4]
Use HELP for help.
cqlsh> 

設定変更も可

attributesディレクトリ以下のconfig.rbやdefault.rb、datastax.rbの中身見ながら設定変えれば、色々な設定で立ち上げられそうです。

例えば、datastaxのリポジトリから存在するバージョン確認してパラメータ変更したら別のバージョンでも上げることができました。 http://rpm.datastax.com/community

config.rbの以下のパラメータと、

default['cassandra']['version'] = '2.1.10'

datastax.rbの以下のパラメータです。

default['cassandra']['package_name']  = 'dsc21'
default['cassandra']['release']       = '1'

これで指定したバージョンで立ち上がるはずです。