読者です 読者をやめる 読者になる 読者になる

こしごぇ(B)

旧:http://d.hatena.ne.jp/koshigoeb/

Chef 実践入門(1) クックブック

Chef実践入門 ~コードによるインフラ構成の自動化 (WEB+DB PRESS plus)

Chef実践入門 ~コードによるインフラ構成の自動化 (WEB+DB PRESS plus)

今回も写経。

コミュニティクックブック

クックブックを検索する

$ knife cookbook site search apache2
apache:
  cookbook:             http://cookbooks.opscode.com/api/v1/cookbooks/apache
  cookbook_description: various apache server related resource provides (LWRP)
  cookbook_maintainer:  melezhik
  cookbook_name:        apache
apache2:
  cookbook:             http://cookbooks.opscode.com/api/v1/cookbooks/apache2
  cookbook_description: Installs and configures all aspects of apache2 using Debian style symlinks with helper definitions
  cookbook_maintainer:  onehealth
  cookbook_name:        apache2
apache2-windows:
  cookbook:             http://cookbooks.opscode.com/api/v1/cookbooks/apache2-windows
  cookbook_description: Installs and configures Apache on Microsoft Windows platforms.
  cookbook_maintainer:  dlrobinson
  cookbook_name:        apache2-windows
  (略)

クックブックの詳細を見る

$ knife cookbook site show apache2 | head -20
average_rating:
category:       Other
created_at:     2009-10-25T23:47:55.000Z
deprecated:     false
description:    Installs and configures all aspects of apache2 using Debian style symlinks with helper definitions
external_url:   http://github.com/opscode-cookbooks/apache2
latest_version: http://cookbooks.opscode.com/api/v1/cookbooks/apache2/versions/2.0.0
maintainer:     onehealth
metrics:
  downloads:
    total:    11815036
    versions:
      0.10.0: 227842
      0.10.1: 227941
      0.11.0: 227831
      0.12.0: 227835
      0.12.1: 227865
      0.12.2: 227868
      0.12.3: 228022
      0.9.1:  227976

クックブックの一覧

$ knife cookbook site list

Berkshelf

$ cat Berksfile
site :opscode

cookbook 'yum-epel'
cookbook 'apache2'
cookbook 'mysql'
$ berks
DEPRECATED: Your Berksfile contains a site location pointing to the Opscode Community Site (site :opscode). Site locations have been replaced by the source location. Change this to: 'source "https://supermarket.getchef.com"' to remove this warning. For more information visit https://github.com/berkshelf/berkshelf/wiki/deprecated-locations
Resolving cookbook dependencies...
Fetching cookbook index from https://supermarket.getchef.com...
Installing apache2 (2.0.0)
Installing logrotate (1.6.0)
Installing iptables (0.13.2)
Installing mysql (5.3.6)
Installing pacman (1.1.1)
Installing yum (3.2.2)
Installing yum-epel (0.4.0)
Installing yum-mysql-community (0.1.10)
$ vi Berksfile
$ cat Berksfile
source "https://supermarket.getchef.com"

cookbook 'yum-epel'
cookbook 'apache2'
cookbook 'mysql'

default.rb 以外のレシピ

recipe[apache2::mod_deflate]

default.rb の代わりに mod_deflate.rb が使われるルール。

Attribute の上書き

Node オブジェクトで apache2 の Attribute を上書き。

$ cat nodes/webdb.json
{
    "apache": {
        "listen_ports": [ 8080],
        "keepalive": "Off",
        "docroot_dir": "/home/vagrant/htdocs"
    },
    "run_list": [
        "recipe[dstat]",
        "recipe[apache2]",
        "recipe[mysql]"
    ],
    "automatic": {
        "ipaddress": "webdb"
    }
}

Chef Solo で複数ノードを管理する

Vagrant マルチ VM 機能

diff --git a/Vagrantfile b/Vagrantfile
index 0f53f00..660dfe6 100644
--- a/Vagrantfile
+++ b/Vagrantfile
@@ -12,6 +12,9 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
   # Every Vagrant virtual environment requires a box to build off of.
   config.vm.box = "opscode-centos-6.5"

+  config.vm.define :node01
+  config.vm.define :node02
+
   # Disable automatic box update checking. If you disable this, then
   # boxes will only be checked for updates when the user runs
   # `vagrant box outdated`. This is not recommended.

ロール

diff --git a/nodes/node01.json b/nodes/node01.json
new file mode 100644
index 0000000..4bcece1
--- /dev/null
+++ b/nodes/node01.json
@@ -0,0 +1,5 @@
+{
+    "run_list": [
+        "role[web]"
+    ]
+}
diff --git a/nodes/node02.json b/nodes/node02.json
new file mode 100644
index 0000000..4bcece1
--- /dev/null
+++ b/nodes/node02.json
@@ -0,0 +1,5 @@
+{
+    "run_list": [
+        "role[web]"
+    ]
+}
diff --git a/roles/web.json b/roles/web.json
new file mode 100644
index 0000000..609467f
--- /dev/null
+++ b/roles/web.json
@@ -0,0 +1,9 @@
+{
+    "name": "web",
+    "chef_type": "role",
+    "json_class": "Chef::Role",
+    "run_list": [
+        "recipe[git]",
+        "recipe[apache2]"
+    ]
+}

Environments

diff --git a/environments/development.json b/environments/development.json
new file mode 100644
index 0000000..489416e
--- /dev/null
+++ b/environments/development.json
@@ -0,0 +1,13 @@
+{
+    "name": "development",
+    "description": "Development environment",
+    "chef_type": "environment",
+    "json_class": "Chef::Environment",
+    "default_attributes": {
+        "apache": {
+            "max_children": "10"
+        }
+    },
+    "override_attributes": {
+    }
+}

Attribute の優先度

  1. Node オブジェクト
  2. ロール
  3. Environments
  4. レシピの中で定義された Attribute
  5. クックブック内の Attribute ファイル

複数ノードへ Chef Solo を実行

$ vagrant ssh-config --host node01 --host node02 >> ~/.ssh/config
$ echo node01 node02 | xargs -n 1 knife solo bootstrap
$ echo node01 node02 | xargs -n 1 knife solo cook