Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Vagrant+chefでelasticsearchの環境構築

More than 3 years have passed since last update.

環境構築メモ

Vagrant+chefでelasticsearchの環境構築をしたときのメモ.
まずはelasticsearchをセットアップするcookbookの用意.

$chef generate repo chef-repo
$cd chef-repo

#cookbook作成
$knife cookbook create elastic

#berkshelfを使う為に初期化
$cd cookbooks/elastic && berks init

#javaとelasticsearchを追加
$vi Berksfile
$cat Berksfile
source "https://supermarket.chef.io"

metadata

cookbook 'elasticsearch', '~> 2.3.2'
cookbook 'java', '~> 1.39.0'

#defaultのjdkバージョンだとelasticsearchが動かなかったのでバージョン指定
$vi attributes/default.rb
$cat attributes/default.rb
default['java']['jdk_version'] = '7'

#Elasticsearchをインストールして起動するレシピ
$vi elastic/recipes/default.rb
$cat elastic/recipes/default.rb
elasticsearch_user 'elasticsearch'
elasticsearch_install 'elasticsearch'
elasticsearch_configure 'elasticsearch'

elasticsearch_service 'elasticsearch' do
  service_actions [:enable, :start]
end

elasticsearch_plugin 'head' do
  url 'mobz/elasticsearch-head'
  notifies :restart, 'elasticsearch_service[elasticsearch]', :delayed
end

#chef provisionで使用するためcommunity cookbookをローカルに配置
$berks install
$berks vendor

なとなくできたはずなので、Vagrant側の準備.

$vagrant init

#config.vm.provisionの項目を追加
$vi Vagrantfile
$cat Vagrantfile
  ...省略...

  config.vm.provision "chef_solo" do |chef|
    chef.cookbooks_path = ["chef-repo/cookbooks", "chef-repo/cookbooks/elastic/berks-cookbooks"]
    chef.nodes_path = "chef-repo/nodes"
    chef.add_recipe "java"
    chef.add_recipe "elastic"
  end

最終的にこんなディレクトリ構成になるはず。

$tree -L 4 .
.
├── Vagrantfile
└── chef-repo
    ├── LICENSE
    ├── README.md
    ├── chefignore
    ├── cookbooks
    │   ├── README.md
    │   ├── elastic
    │   │   ├── Berksfile
    │   │   ├── Berksfile.lock
    │   │   ├── CHANGELOG.md
    │   │   ├── Gemfile
    │   │   ├── README.md
    │   │   ├── Thorfile
    │   │   ├── Vagrantfile
    │   │   ├── attributes
    │   │   ├── berks-cookbooks
    │   │   ├── chefignore
    │   │   ├── definitions
    │   │   ├── files
    │   │   ├── libraries
    │   │   ├── metadata.rb
    │   │   ├── providers
    │   │   ├── recipes
    │   │   ├── resources
    │   │   ├── templates
    │   │   └── test
    │   └── example
    │       ├── README.md
    │       ├── attributes
    │       ├── metadata.rb
    │       └── recipes
    ├── data_bags
    │   ├── README.md
    │   └── example
    │       └── example_item.json
    ├── environments
    │   ├── README.md
    │   └── example.json
    ├── nodes
    └── roles
        ├── README.md
        └── example.json

vagrantでインスタンスを作成しつつchefで環境を一気に構築.

#インスタンス作成からprovisioningまで
$vagrant up

#なんか足りてなかったら再読み込みしてchef再実行
$vagrant reload --provision

感想

  • レシピを試しに動かしながらデバックして作るのにvagrantはすごくあう

  • vagrantもchefも中途半端な知識しか無い割には比較的さくっとできた

  • Dockerでelasticsearch入ったコンテナ落としてきて動かした方が絶対早いとは思う

参考

https://github.com/elastic/cookbook-elasticsearch
https://github.com/agileorbit-cookbooks/java
https://www.vagrantup.com/docs/provisioning/chef_solo.html

nyamage
Docker, Ruby, Swiftは趣味で触ってるだけです。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away