---
title: 今日から使い始めるChef
tags:  #xtone #chef #opschef_ja  
author: [NAKAYAMA Masahiro](https://www.docswell.com/user/nekoruri)
site: [Docswell](https://www.docswell.com/)
thumbnail: https://bcdn.docswell.com/page/V8EDRYXYJG.jpg?width=480
description: 今日から使い始めるChef  2013-03-29 Xtone Ltd. ピザ会 (without pizza＞＜) Aki / @nekoruri
published: March 29, 13
canonical: https://www.docswell.com/s/nekoruri/ZWMRR5-20130329-chef
---
# Page. 1

![Page Image](https://bcdn.docswell.com/page/V8EDRYXYJG.jpg)

今日から使い始めるChef
2013-03-29
Xtone Ltd. ピザ会
(without pizza＞＜)
Aki / @nekoruri


# Page. 2

![Page Image](https://bcdn.docswell.com/page/4V7PW6P2J8.jpg)

いくつかの案件がChef管理に移行しました
必ず覚えて欲しいこと


# Page. 3

![Page Image](https://bcdn.docswell.com/page/L2JV832XJQ.jpg)

Chef
• サーバの構成管理ツール
– サーバの「完成状態」を書く
– 「現状」との違いを判断する
– その差分を埋める
•
•
•
•
•
•
パッケージのインストール
設定ファイルの設置
サービスの起動
DBのユーザ作成
ファイアウォール設定
etc.
ここ全部
Chefが
やってくれる


# Page. 4

![Page Image](https://bcdn.docswell.com/page/Z5EG53RREL.jpg)

禁止事項
• サーバ上でrootで設定ファイル変更


# Page. 5

![Page Image](https://bcdn.docswell.com/page/V4JQZ5VYJP.jpg)

禁止事項
• サーバ上でrootで設定ファイル変更
Chefで設定戻されて突然の障害


# Page. 6

![Page Image](https://bcdn.docswell.com/page/DK7439MZE1.jpg)

使ってみる
1. Chefのレポジトリを取ってくる
2. rootで入れるサーバを一台作る
3. 対象サーバにchef-soloを入れる
– knife solo prepare root@192.0.2.100
4. ノード設定を適当に書く
– vim nodes/192.0.2.100.json
{&quot;run_list&quot;: [&quot;role[base]&quot;]}
5. chef起動
– knife solo cook root@192.0.2.100


# Page. 7

![Page Image](https://bcdn.docswell.com/page/PLJ11K8DJG.jpg)

使ってみる
1. Chefのレポジトリを取ってくる
2. rootで入れるサーバを一台作る
3. 対象サーバにchef-soloを入れる
– knife solo prepare root@192.0.2.100
4. ノード設定を適当に書く
– vim nodes/192.0.2.100.json
{&quot;run_list&quot;: [&quot;role[base]&quot;]}
5. chef起動
– knife solo cook root@192.0.2.100
以降はここだけ


# Page. 8

![Page Image](https://bcdn.docswell.com/page/8GJW8DZ872.jpg)

ざっくりChefのしくみのおさらい
ざっくりCHEFの紹介


# Page. 9

![Page Image](https://bcdn.docswell.com/page/W4EZ8G19J3.jpg)

Chef用語
• Resource
– 設定の最小単位
– ファイルやパッケージ単位の設定
• Recipe
– 複数のResourceで構成されるひとまとまり
• Cookbook
– Recipeをグループにまとめたもの
– 一つのRecipeしか含まない場合もある
• レポジトリ（キッチン)
– Chefの動作に必要なファイル群
– 複数のCookbookや、ロール・ノードの設定を含む


# Page. 10

![Page Image](https://bcdn.docswell.com/page/KY76PYLDEV.jpg)

Chef用語
• ノード
– Chefで管理されるサーバ
• Role
– ノードの役割を定義
– 使うRecipeとか変数とかをここで設定する（抽象化）
• chef-solo
– サーバ無しでChefを利用するコマンド
– chef-soloを動かしたノード自身が操作対象となる
• knife-solo
– 対象ノードにレポジトリを転送してchef-soloを動かす


# Page. 11

![Page Image](https://bcdn.docswell.com/page/5G75KG18J4.jpg)

Resource
• 設定の最小単位
– ファイル
– パッケージ
– ファイアウォール
template &#039;/etc/apache2/sites-available/www.example.jp&#039; do
source &#039;www.example.jp.erb&#039;
ファイルを更新したら
mode &#039;0644&#039;
apache2サービスを
action :create
再起動
notifies :restart, &quot;service[apache2]&quot;
end
%w{zsh zip sysstat libcache-cache-perl}.each do |pkg|
package pkg do
action :install
完全にRuby
end
end
firewall_rule &quot;zabbix&quot; do
port 10050
source &quot;192.0.2.5&quot;
action :allow
notifies :enable, &quot;firewall[ufw]&quot;
end


# Page. 12

![Page Image](https://bcdn.docswell.com/page/V9J2WY1VER.jpg)

Recipe
• 複数のResourceで構成されるひとまとまり
munin_servers = [&#039; 192.0.2.6/32&#039;, &#039; 192.0.2.140 /32&#039;]
package &quot;munin-node&quot;
Rubyの内部DSLなので
変数とかも利用し放題
service_name = node[&#039;munin&#039;][&#039;service_name&#039;]
service service_name do
supports :restart =&gt; true
action :enable
end
サーバ毎の設定を利用
template &quot;#{node[&#039;munin&#039;][&#039;basedir&#039;]}/munin-node.conf&quot; do
source &quot;munin-node.conf.erb&quot;
mode 0644
variables :munin_servers =&gt; munin_servers
notifies :restart, &quot;service[#{service_name}]&quot;
end
変数をerbに渡す
サーバの情報を利用して分岐
case node[&#039;platform&#039;]
when &quot;arch&quot;, &quot;smartos&quot;
execute &quot;munin-node-configure --shell | sh&quot; do
not_if { Dir.entries(node[&#039;munin&#039;][&#039;plugins&#039;]).length &gt; 2 }
notifies :restart, &quot;service[#{service_name}]&quot;
end
end


# Page. 13

![Page Image](https://bcdn.docswell.com/page/5DEYLGZQJM.jpg)

Template
log_level 4
log_file /var/log/munin/munin-node.log
port 4949
pid_file /var/run/munin/munin-node.pid
background 1
setseid 1
# Which port to bind to;
host *
user root
group &lt;%= node[&#039;munin&#039;][&#039;root&#039;][&#039;group&#039;] %&gt;
setsid yes
サーバ毎の設定を利用
&lt;% end %&gt;
# A list of addresses that are allowed to connect. This must be a
# regular expression, due to brain damage in Net::Server, which
# doesn&#039;t understand CIDR-style network notation. You may repeat
# the allow line as many times as you&#039;d like
引き渡された変数を利用
allow ^127¥.0¥.0¥.1$
&lt;% @munin_servers.sort.each do |server| -%&gt;
cidr_allow &lt;%= server %&gt;
&lt;% end -%&gt;


# Page. 14

![Page Image](https://bcdn.docswell.com/page/2VJN4G32E8.jpg)

Cookbookの構成
cookbooks/munin
├── README.md
├── attributes
├── definitions
├── files
│ └── default
├── libraries
├── metadata.rb
├── providers
├── recipes
│ └── default.rb
│ └── server.rb
├── resources
└── templates
└── default
変数のデフォルト値
erbしない配布ファイル
デフォルトのRecipe
「munin」で利用可能
デフォルト以外のRecipe
「munin::server」で利用可能
配布ファイルの置き場
erb利用可能


# Page. 15

![Page Image](https://bcdn.docswell.com/page/GYE9QW9DE3.jpg)

新しいChefレポジトリを作って、
既存のCookbookを組み合わせて
自分用のRecipeを書いて
実際のノードに適用するまで
一からやってみる


# Page. 16

![Page Image](https://bcdn.docswell.com/page/5GE8GN95ED.jpg)

レポジトリを作る
• knife-solo gemを入れる
% gem install knife-solo
• knife soloでレポジトリ作成
% knife solo init demo
% cd demo
% git init
% git add .
% git commit -m &#039;new chef repo&#039;


# Page. 17

![Page Image](https://bcdn.docswell.com/page/MLELGPW37R.jpg)

opscode-cookbooks
• Chef開発元が公開しているCookbook集
– オープンソースとして開発
– Github上で公開
https://github.com/opscode-cookbooks/
– Ubuntuなら組み合わせるだけでそのまま使える
※ CentOSだとイマイチという意見多し（未確認）
– knifeコマンドで取ってこれる


# Page. 18

![Page Image](https://bcdn.docswell.com/page/54JMQ49MEW.jpg)

opscode-cookbooksのインポート
• knife cookbookでCookbookを取ってくる
% knife cookbook site install apt -o cookbooks
% knife cookbook site install users -o cookbooks
% knife cookbook site install sudo -o cookbooks
% knife cookbook site install mysql -o cookbooks
依存関係を見て引っ張って
くるツールがあるが、
今回は未使用
• knife-github-cookbooksというgemもある
– github上の任意のCookbookをインポートできる


# Page. 19

![Page Image](https://bcdn.docswell.com/page/ZPJR8WGLE9.jpg)

新しくCookbook/Recipeを作る
• knife cookbook createでCookbookを作成
– site-cookbooks以下に置くのが慣習
% knife cookbook create base -o site-cookbooks
• Recipeを書いていく
% vim site-cookbooks/base/recipes/default.rb
include_recipe &#039;apt&#039;
# ユーザ設定
include_recipe &#039;users::sysadmins&#039;
node.override[&#039;authorization&#039;][&#039;sudo&#039;][&#039;passwordless&#039;]
include_recipe &#039;sudo&#039;
= true


# Page. 20

![Page Image](https://bcdn.docswell.com/page/NPEX8PQ67X.jpg)

Roleに経由してRecipeを利用する
• ロールに対して利用するレシピを設定
% vim roles/base.rb
name &quot;base&quot;
description &quot;base packages&quot;
run_list(
&#039;recipe[base]&#039;,
&#039;recipe[mysql::server]&#039;
)
default_attributes({
&#039;mysql&#039; =&gt; {
&#039;server_root_password&#039; =&gt; &#039;rootpw&#039;,
&#039;server_repl_password&#039; =&gt; &#039;replpw&#039;,
&#039;server_debian_password&#039; =&gt; &#039;maintpw&#039;,
&#039;tunable&#039; =&gt; {
&#039;log_bin&#039; =&gt; &#039;mysqld&#039;
}
}
})


# Page. 21

![Page Image](https://bcdn.docswell.com/page/63EKKD9GED.jpg)

Roleに経由してRecipeを利用する
• ノードに対してロールを紐付ける
% nodes/192.0.2.100.json
{
&quot;run_list&quot;: [&quot;role[base]&quot;],
ノード別で異なる設定は
&quot;mysql&quot;: {
ここで定義できる
&quot;tunable&quot;: {
&quot;server_id&quot;: &quot;1&quot;,
&quot;innodb_buffer_pool_size&quot;: &quot;2048M&quot;,
&quot;innodb_log_file_size&quot;: &quot;128M&quot;
}
}
}


# Page. 22

![Page Image](https://bcdn.docswell.com/page/4L73ZRW575.jpg)

data_bags
• ユーザ情報などを置いておけるデータ置き場
– chef-solo環境だと、data_bagsディレクトリ内
% data_bags/users/hoge.json
{
&quot;id&quot;: &quot;hoge&quot;,
&quot;uid&quot;: &quot;10001&quot;,
&quot;comment&quot;: &quot;FOO Bar&quot;,
&quot;ssh_keys&quot;: [
&quot;ssh-rsa AAAAなんちゃら= hoge@example.jp&quot;
],
&quot;groups&quot;: [ &quot;sysadmin&quot; ],
&quot;shell&quot;: &quot;/usr/bin/zsh&quot;
}


# Page. 23

![Page Image](https://bcdn.docswell.com/page/V87DRYKYEG.jpg)

chef-solo実行
1. 対象サーバにchef-soloを入れる
– knife solo prepare root@192.0.2.100
2. chef起動
– knife solo cook root@192.0.2.100


# Page. 24

![Page Image](https://bcdn.docswell.com/page/4VJPW6K2E8.jpg)

ありがとうopscode-cookbooksありがとう
おすすめCOOKBOOK


# Page. 25

![Page Image](https://bcdn.docswell.com/page/L2EV83VXEQ.jpg)

apt
• aptレポジトリの更新
– https://github.com/opscode-cookbooks/apt
– 「include_recipe &#039;apt&#039;」でapt-getしてくれる
– 使わないと古いパッケージを取りに行き失敗


# Page. 26

![Page Image](https://bcdn.docswell.com/page/Z57G53LR7L.jpg)

users::sysadminsとsudo
• ユーザ管理（users::sysadmins）
– https://github.com/opscode-cookbooks/users
– data_bagsにsysadminグループのユーザ設定を入
れておくと勝手に作ってくれる
– ユーザ追加の度にRecipeを変更しなくてよい
• sudoers管理（sudo）
– 上記sysadminグループはデフォルトで入ってる


# Page. 27

![Page Image](https://bcdn.docswell.com/page/V4EQZ5YYEP.jpg)

firewall
• ファイアウォールのルール設定
– https://github.com/opscode-cookbooks/firewall
firewall &quot;ufw&quot; do
firewall Cookbookで定義された
action :enable
独自リソース
end
firewall_rule &quot;ssh&quot; do
port 22
action :allow
notifies :enable, &quot;firewall[ufw]&quot;
end
– Ubuntu標準ではpingが拒否されているので、
変更したbefore.ruleも配る


# Page. 28

![Page Image](https://bcdn.docswell.com/page/DKJ439WZ71.jpg)

apache2 / php
• 各ミドルウェアを入れてくれる
– https://github.com/opscode-cookbooks/apache2
node.default[&#039;apache&#039;][&#039;listen_ports&#039;] = [80, 443]
node.default[&#039;apache&#039;][&#039;serversignature&#039;] = &#039;Off&#039;
node.default[&#039;apache&#039;][&#039;traceenable&#039;] = &#039;Off&#039;
include_recipe &#039;apache2::mod_proxy_http&#039;
include_recipe &#039;apache2::mod_php5&#039;
include_recipe &#039;apache2::mod_rewrite&#039;
include_recipe &#039;apache2::mod_ssl&#039;
include_recipe &#039;apache2&#039;
include_recipe &#039;php::package&#039;
include_recipe &#039;php::module_apc&#039;
include_recipe &#039;php::module_mysql&#039;
include_recipe &#039;php::module_gd&#039;
node.default[&#039;php&#039;][&#039;directives&#039;] = { &#039;date.timezone&#039; =&gt; &#039;Asia/Tokyo&#039; }


# Page. 29

![Page Image](https://bcdn.docswell.com/page/PLE11KYDEG.jpg)

mysql / database
• データベースのユーザを作成する
– https://github.com/opscode-cookbooks/database
include_recipe &#039;database::mysql&#039;
mysql_connection_info = {
:host =&gt; &quot;localhost&quot;,
:username =&gt; &#039;root&#039;,
:password =&gt; node[&#039;mysql&#039;][&#039;server_root_password&#039;]
}
mysql_database &#039;myapp_production&#039; do
connection mysql_connection_info
action :create
end
mysql_database_user &#039;myapp_production&#039; do
connection mysql_connection_info
password &#039;passwordpassword&#039;
database_name &#039;myapp_production&#039;
host &#039;localhost&#039;
action :create
end


# Page. 30

![Page Image](https://bcdn.docswell.com/page/8GEW8DG8J2.jpg)

関連情報
• 伊藤直也さんの電子書籍がベスト
– 入門Chef Solo - Infrastructure as Code
http://www.amazon.co.jp/dp/B00BSPH158/
http://tatsu-zine.com/books/chef-solo
• opscode-cookbooksを読む
– 宝の山
– https://github.com/opscode-cookbooks


# Page. 31

![Page Image](https://bcdn.docswell.com/page/W47Z8GL9E3.jpg)

ぐぬぬ
悩んでいること


# Page. 32

![Page Image](https://bcdn.docswell.com/page/KYJ6PYWD7V.jpg)

Unicorn環境
• unicorn::default
– Unicorn gemいれる→普通Bundlerだし
– 設定ファイル→アプリと一緒にデプロイするし
• application_ruby::unicorn
– 似てるようで似てるだけのCapistranoデプロイと
いまいち相性が悪い！
– runitでUnicornのプロセス自動再起動！すごい！
– graceful restartさせると死ぬ！すごい！！１


# Page. 33

![Page Image](https://bcdn.docswell.com/page/5GJ5KGM8E4.jpg)

OpsWorks
• AWS + Chef
– http://aws.amazon.com/jp/opsworks/
– AWS提供のChef Cookbookを組み合わせる
– まだ遊んでない


# Page. 34

![Page Image](https://bcdn.docswell.com/page/4LE3ZRWGE5.jpg)

その他のアプローチ
• RightScale / Scalr
– もう一つ上のレイヤーでの構成管理したい派
– Xtoneでも某ゲーム案件で利用
• PaaS
– そもそもミドルウェアなんて面倒みたくない派
– Heroku、EngineYard、 AWS Elastic Beanstalk
ニフクラC4SA、IIJ Mogok


