使用Vagrant和Chef进行elasticsearch的环境建设
环境搭建笔记
使用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 – Java的食谱
https://www.vagrantup.com/docs/provisioning/chef_solo.html – Chef Solo的虚拟机配置文档