2
2

More than 5 years have passed since last update.

クックブックを作成してノードに適用する。- その2

Last updated at Posted at 2016-02-21

関連記事

クックブックの生成

  • デフォルトではcookbooksの配下に生成されるので、-o オプションを付けてsite-cookbooksの配下に生成する。
  • ここではyumをupdateするクックブックとmariadbを設置するクックブックを生成してノードに適用する。

yumをupdateする

  • 雛形生成
  % bin/knife cookbook create yum-update -o site-cookbooks
  • レシピを作成
    • execute:コマンドを実行する。
  % vi site-cookbooks/yum-update/recipes/default.rb

  execute 'yum-update' do
    user 'root'
    command 'yum -y update'
    action :run
  end
  • run_list(実行一覧)にレシピを追加する。
  % bin/knife node run_list add dev 'recipe[yum-update]' -z
  • ノードに適用
  % bin/knife solo cook dev

mariadbを設置する

  • 雛形生成
  % bin/knife cookbook create mariadb -o site-cookbooks
  • レシピを作成
    • cookbook_file:クックブック中のfilesディレクトリからノードの指定されたパスにファイルを配置する。
    • package:パッケージ(CentOSならyum)を管理する。
    • service:サービス(デーモン)を管理する。
  % vi site-cookbooks/mariadb/recipes/default.rb

  cookbook_file '/etc/yum.repos.d/MariaDB.repo' do
    owner 'root'
    group 'root'
    mode '0644'
  end

  %w(MariaDB-server MariaDB-client MariaDB-devel).each do |pkg|
    package pkg do
      action :install
    end
  end

  service 'mysql' do
    action [:enable, :start]
  end
  • mariadbのリポジトリ設定ファイルを作成する。
  % vi site-cookbooks/mariadb/files/default/MariaDB.repo

  # MariaDB 10.1 CentOS repository list - created 2016-02-21 00:19 UTC
  # http://mariadb.org/mariadb/repositories/
  [mariadb]
  name = MariaDB
  baseurl = http://yum.mariadb.org/10.1/centos6-amd64
  gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
  gpgcheck=1
  • run_list(実行一覧)にレシピを追加する。
  % bin/knife node run_list add dev 'recipe[mariadb]' -z

  dev:
    run_list:
      recipe[yum-epel]
      recipe[yum-update]
      recipe[mariadb]
  • ノードに適用
    • 設置手順は前回のsandboxモードでmariadbを設置したときと同様。
  % bin/knife solo cook dev

  Running Chef on dev...
  Checking Chef version...
  Installing Berkshelf cookbooks to 'cookbooks'...
  Resolving cookbook dependencies...
  Using yum (3.10.0)
  Using yum-epel (0.6.5)
  Vendoring yum (3.10.0) to cookbooks/yum
  Vendoring yum-epel (0.6.5) to cookbooks/yum-epel
  Uploading the kitchen...
  Generating solo config...
  Running Chef: sudo chef-solo -c ~/chef-solo/solo.rb -j ~/chef-solo/dna.json
  Starting Chef Client, version 12.7.2
  Compiling Cookbooks...
  Converging 5 resources
  Recipe: yum-epel::default
    * yum_repository[epel] action create
      * template[/etc/yum.repos.d/epel.repo] action create
        - update content in file /etc/yum.repos.d/epel.repo from 942dac to d02c1f
        --- /etc/yum.repos.d/epel.repo  2016-02-21 01:58:03.339879602 +0000
        +++ /etc/yum.repos.d/.epel.repo20160221-16395-1kk6smf   2016-02-21 03:56:30.298013142 +0000
        @@ -3,7 +3,7 @@

         [epel]
         name=Extra Packages for Enterprise Linux 6 - $basearch
        -enabled=0
        +enabled=1
         failovermethod=priority
         gpgcheck=1
         gpgkey=https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-6
        - restore selinux security context
      * execute[yum clean metadata epel] action run
        - execute yum clean metadata --disablerepo=* --enablerepo=epel


      * execute[yum-makecache-epel] action run
        - execute yum -q -y makecache --disablerepo=* --enablerepo=epel
      * ruby_block[yum-cache-reload-epel] action create
        - execute the ruby block yum-cache-reload-epel
      * execute[yum clean metadata epel] action nothing (skipped due to action :nothing)
      * execute[yum-makecache-epel] action nothing (skipped due to action :nothing)
      * ruby_block[yum-cache-reload-epel] action nothing (skipped due to action :nothing)

  Recipe: yum-update::default
    * execute[yum-update] action run
      - execute yum -y update
  Recipe: mariadb::default
    * cookbook_file[/etc/yum.repos.d/MariaDB.repo] action create
      - create new file /etc/yum.repos.d/MariaDB.repo
      - update content in file /etc/yum.repos.d/MariaDB.repo from none to 3e5392
      --- /etc/yum.repos.d/MariaDB.repo 2016-02-21 03:56:38.319013136 +0000
      +++ /etc/yum.repos.d/.MariaDB.repo20160221-16395-ih10g6   2016-02-21 03:56:38.319013136 +0000
      @@ -1 +1,8 @@
      +# MariaDB 10.1 CentOS repository list - created 2016-02-21 00:19 UTC
      +# http://mariadb.org/mariadb/repositories/
      +[mariadb]
      +name = MariaDB
      +baseurl = http://yum.mariadb.org/10.1/centos6-amd64
      +gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
      +gpgcheck=1
      - change mode from '' to '0644'
      - change owner from '' to 'root'
      - change group from '' to 'root'
      - restore selinux security context
    * yum_package[MariaDB-server] action install
      - install version 10.1.11-1.el6 of package MariaDB-server
    * yum_package[MariaDB-client] action install (up to date)
    * yum_package[MariaDB-devel] action install
      - install version 10.1.11-1.el6 of package MariaDB-devel

  Running handlers:
  Running handlers complete
  Chef Client finished, 8/12 resources updated in 01 minutes 59 seconds
  • ノードから確認
  % ssh dev

  $ sudo service mysql start
  Starting MySQL. SUCCESS!

  $ mysql -u root
  Welcome to the MariaDB monitor.  Commands end with ; or \g.
  Your MariaDB connection id is 3
  Server version: 10.1.11-MariaDB MariaDB Server

  Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  MariaDB [(none)]> exit
  Bye

Chefの作業手順について

chefで作業するときの手順を整理すると下記のようになる。

  1. クックブックを生成
  2. クックブックの中のレシピを修正
  3. レシピの中のリソースを記述
  4. run_list(実行一覧)にレシピを追加
  5. ノードに適用

参考

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2