サーバー構築は極力Chefを使うようにしている今日この頃。ElasticSearch & KibanaをChefで自動構築するレシピを書いたので自分用メモ。
インストールする内容は以下の通り。
- elasticsearch 1.3.2
- kibana 3.1.0
- Nginx 1.0.15
インストールするサーバーはCentOS6.4のminmal版。ssh接続できるところまで持っていく必要があり。その後は”knife cook”で全てインストール&設定が完了する。
- http://ftp.riken.jp/Linux/centos/6/isos/x86_64/CentOS-6.5-x86_64-minimal.iso
このレシピの注意点というか前提。
- 本来、レシピをいくつかに分けたり、Attributesに利用したりするべきだが、一つのレシビにまとめて書いている。
- ネットワークに制限があるサーバーへのインストールを想定しているため、パッケージ等はクックブックに内包し、なるべくネットワークを介さないようにしてある。(冪等性という観点からもそのほうが動作が安定するので・・)
- インストールするサーバーをCentOS-6.5-x86_64-minimalを想定。(ChefではOS間の依存を吸収する仕組みはあるが、業務上、その必要性はほぼないのでCentOSできめ決めつけで記述)
あとは、自分自身もまだまだChef見習い中なので、あくまで参考までに。
Cookbookのフォルダ構成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
cookbooks/elasticsearch/ |-- CHANGELOG.md |-- README.md |-- attributes |-- definitions |-- files | `-- default | |-- RPM-GPG-KEY-EPEL-6 | |-- elasticsearch-1.3.2.noarch.rpm | |-- epel-release-6-8.noarch.rpm | |-- htpasswd | |-- jdk-8u20-linux-x64.rpm | `-- kibana-3.1.0.zip |-- libraries |-- metadata.rb |-- providers |-- recipes | |-- default.rb | `-- setup.rb |-- resources `-- templates `-- default |-- elasticsearch.yml.erb |-- epel.repo.erb `-- kibana.conf.erb |
レシピとテンプレートファイル
cookbooks/elasticsearch/recipes/default.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# # Cookbook Name:: setup # Recipe:: default # # - Elasticsearchをインストール # - Kibanaをインストール # elasticsearch_file = 'elasticsearch-1.3.2' kibana_file = 'kibana-3.1.0' # パッケージインストール %w{ unzip vim glibc wget }.each do |name| package name do action :install not_if { ::File.exists?("/usr/bin/#{name}") } end end #################################################### # EPEL #################################################### directory '/root/sources' do action :create end # epelのrpmファイル cookbook_file "epel-release-6-8.noarch.rpm" do path "/root/sources/epel-release-6-8.noarch.rpm" mode "0644" end # epelリポジトリ追加 bash "Add epel repository" do code <<-EOH rpm -Uvh /root/sources/epel-release-6-8.noarch.rpm EOH not_if { ::File.exists?('/etc/yum.repos.d/epel.repo') } end # GPGキー設置 cookbook_file "RPM-GPG-KEY-EPEL-6" do path '/etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6' mode '0644' end # epel.repoファイル修正 template '/etc/yum.repos.d/epel.repo' do source 'epel.repo.erb' end #################################################### # JAVA #################################################### cookbook_file "/root/sources/jdk-8u20-linux-x64.rpm" do source "jdk-8u20-linux-x64.rpm" end bash "Install java" do code <<-EOH rpm -Uvh /root/sources/jdk-8u20-linux-x64.rpm EOH not_if { ::File.exists?("/usr/java/jdk1.8.0_20") } end #################################################### # Elasticsearch #################################################### # Elasticsearchのrpmファイル cookbook_file "elasticsearch-1.3.2.noarch.rpm" do path "/root/sources/elasticsearch-1.3.2.noarch.rpm" owner 'root' group 'root' mode "0644" end # Elasticsearch解凍 bash "rpm install elasticsearch" do code <<-EOH rpm -Uvh /root/sources/elasticsearch-1.3.2.noarch.rpm chkconfig elasticsearch on EOH not_if { ::File.exists?("/etc/init.d/elasticsearch") } end %w{ data plugins }.each do |value| directory "/var/www/elasticsearch/#{value}" do owner 'elasticsearch' group 'elasticsearch' action :create recursive true end end # Elasticsearch設定ファイル template "/etc/elasticsearch/elasticsearch.yml" do source 'elasticsearch.yml.erb' mode '0664' end # Elasticsearch起動 service "elasticsearch" do action :restart end #################################################### # Kibana #################################################### directory "/var/www/" do action :create end # kibanaのzipファイル cookbook_file "#{kibana_file}.zip" do path "/root/sources/#{kibana_file}.zip" mode "0644" end # Kibana解凍 bash "unzip kibana" do code <<-EOH unzip /root/sources/#{kibana_file}.zip -d /var/www/ EOH not_if { ::File.exists?("/var/www/#{kibana_file}") } end #################################################### # Eginx # - EPEL必須 #################################################### # パッケージインストール yum_package "nginx" do action :install not_if { ::File.exists?("/etc/init.d/nginx") } end # htpasswd設置 cookbook_file "htpasswd" do path "/etc/nginx/htpasswd" end # Kibana解凍 bash "unzip kibana" do code <<-EOH chown nginx:nginx -R /var/www/#{kibana_file} EOH end # デフォルトの設定ファイルはバックアップとしてファイル名変更 bash 'move default.conf' do code <<-EOH mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bk EOH only_if { ::File.exists?('/etc/nginx/conf.d/default.conf') } end # Nginxでkibana設定 template '/etc/nginx/conf.d/kibana.conf' do source 'kibana.conf.erb' mode '0644' end # nginx restart service 'nginx' do action :restart end |
cookbooks/elasticsearch/templates/default/elasticsearch.yml.erb
1 2 3 4 5 |
path.data: /var/www/elasticsearch/data path.logs: /var/log/elasticsearch/ path.plugins: /var/www/elasticsearch/plugins cluster.name: mysearch node.name: "Node 001" |
cookbooks/elasticsearch/templates/default/epel.repo.erb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
[epel] name=Extra Packages for Enterprise Linux 6 - $basearch baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch #mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch failovermethod=priority enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 [epel-debuginfo] name=Extra Packages for Enterprise Linux 6 - $basearch - Debug baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch/debug #mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-6&arch=$basearch failovermethod=priority enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 gpgcheck=1 [epel-source] name=Extra Packages for Enterprise Linux 6 - $basearch - Source baseurl=http://download.fedoraproject.org/pub/epel/6/SRPMS #mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-6&arch=$basearch failovermethod=priority enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 gpgcheck=1 |
cookbooks/elasticsearch/templates/default/kibana.conf.erb
1 2 3 4 5 6 7 8 9 10 11 12 13 |
server { listen 80 default; server_name localhost; access_log /var/log/nginx/kibana.access.log; error_log /var/log/nginx/kibana.error.log; location / { root /var/www/kibana-3.1.0/; index index.html index.htm; } } |
実行コマンド例
1 2 |
[]$ knife solo prepare root@your_elasticsearch_server []$ knife solo cook root@your_elasticsearch_server |