Chefのお勉強2 - recipeの登録、利用

Workstation(今回はChefサーバ)で実行

利用するレシピはOPSCODEのgetting-started。レシピをChef Serverにアップロードする。
getting-startedはホームディレクトリに、chef-getting-started.txt を作成するレシピ。

# git clone https://github.com/opscode-cookbooks/getting-started.git
# knife cookbook upload getting-started -o .

アップロードが出来たか確認する。

# knife recipe list
getting-started

レシピをNodeで利用出来るようにする。

# knife node list
<Node名が表示される>
# knife node run_list add <Node名> "recipe[getting-started]"
<Node名>
  run_list: recipe[getting-started]
# knife node show <Node名>
Node Name:   <Node名>
Environment: _default
FQDN:        <Node名(FQDN) >
IP:         < IPアドレス>
Run List:    recipe[getting-started]
Roles:       
Recipes:     
Platform:    centos 6.4
Tags:    
実行環境(Node)で実行
# chef-client
Starting Chef Client, version 11.4.4
[2013-05-18T20:14:26+09:00] INFO: *** Chef 11.4.4 ***
[2013-05-18T20:14:26+09:00] INFO: [inet6] no default interface, picking the first ipaddress
[2013-05-18T20:14:26+09:00] INFO: Run List is [recipe[getting-started]]
[2013-05-18T20:14:26+09:00] INFO: Run List expands to [getting-started]
[2013-05-18T20:14:26+09:00] INFO: Starting Chef Run for client2.localdomain
[2013-05-18T20:14:26+09:00] INFO: Running start handlers
[2013-05-18T20:14:26+09:00] INFO: Start handlers complete.
resolving cookbooks for run list: ["getting-started"]
[2013-05-18T20:14:26+09:00] INFO: Loading cookbooks [getting-started]
Synchronizing Cookbooks:
[2013-05-18T20:14:27+09:00] INFO: Storing updated cookbooks/getting-started/recipes/default.rb in the cache.
[2013-05-18T20:14:27+09:00] INFO: Storing updated cookbooks/getting-started/attributes/default.rb in the cache.
[2013-05-18T20:14:27+09:00] INFO: Storing updated cookbooks/getting-started/README.md in the cache.
[2013-05-18T20:14:27+09:00] INFO: Storing updated cookbooks/getting-started/metadata.rb in the cache.
  - getting-started
Compiling Cookbooks...
Converging 1 resources
Recipe: getting-started::default
  * template[/root/chef-getting-started.txt] action create[2013-05-18T20:14:27+09:00] INFO: Processing template[/root/chef-getting-started.txt] action create (getting-started::default line 20)
[2013-05-18T20:14:27+09:00] INFO: template[/root/chef-getting-started.txt] updated content
[2013-05-18T20:14:27+09:00] INFO: template[/root/chef-getting-started.txt] mode changed to 644

    - create template[/root/chef-getting-started.txt]
        --- /tmp/chef-tempfile20130518-11703-1d23vid    2013-05-18 20:14:27.092977873 +0900
        +++ /tmp/chef-rendered-template20130518-11703-1oxhmkm   2013-05-18 20:14:27.092977873 +0900
        @@ -0,0 +1,5 @@
        +Welcome to Chef!
        +
        +This is Chef version 11.4.4.
        +Running on centos.
        +Version 6.4.

[2013-05-18T20:14:27+09:00] INFO: Chef Run complete in 0.324367186 seconds
[2013-05-18T20:14:27+09:00] INFO: Running report handlers
[2013-05-18T20:14:27+09:00] INFO: Report handlers complete
Chef Client finished, 1 resources updated

レシピを実行した結果ファイルが出来たかを確認する。

# ls /root/chef-getting-started.txt
/root/chef-getting-started.txt

Chefのお勉強1 - CentOSでのChef Server環境の構築

今回はChefサーバ(Chef Server、兼Workstation(Chef Client))と、管理対象ノード(Nodes)の2台構成(CentOS 6.4)で環境を作ります。

事前準備
  • ホスト名を解決出来るようにしておく。

 DNSを使わない場合は、/etc/hosts、/etc/sysconfig/networkを修正して、さらにhostnameコマンドでホスト名を設定しておく。
 このときに設定するホスト名はFQDNにしておく。

  • EDITOR変数を定義しておく。
export EDITOR=/usr/bin/vim
  • 使用するサーバの時刻を合わせておく。

Chefサーバの設定

Chef Serverの設定

Chefサーバのインストール方法は automation - CentOSにChef Serverを最速でインストールする - Qiita を参照しようとしたら、インストールされるバージョンが0.10系だったので、Chef Client | Chef Downloads | Chefから最新版(11.0.8)を取ってきてインストール。

# wget https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-server-11.0.8-1.el6.x86_64.rpm
# rpm -ivh chef-server-11.0.8-1.el6.x86_64.rpm

正常に終わったら、sudo chef-server-ctl reconfigureを行う。

# chef-server-ctl reconfigure
Chef Client finished, 268 resources updated
chef-server Reconfigured!

chef-server Reconfigured!が出れば、サーバの設定が完了。
これでweb-uiにアクセス出来るようになる。
adminユーザのid/passwordは/etc/chef-server/chef-server-running.jsonのweb_ui_admin_default_passwordを参照。
adminユーザでログインすると、パスワード変更が求められる。

Workstationの設定

今回はworkstationをChefサーバのサーバにするので、引き続きchefクライアントのインストール。

# curl -L https://www.opscode.com/chef/install.sh | bash

Workstationが使用する鍵ファイルをコピー 。

# cp /etc/chef-server/admin.pem /etc/chef/
# cp /etc/chef-server/chef-validator.pem /etc/chef/validation.pem

knifeの初期設定を行う。knifeの初期設定を行うことで、client、user、nodeが追加できるようになる。
Chef 11系では、httpsでポート番号は443な点に注意が必要。

# knife configure -i
Please enter the chef server URL: [http://<FQDNのホスト名>:4000] https://<FQDNのホスト名>:443
Please enter a name for the new user: [root] 
Please enter the existing admin name: [admin] 
Please enter the location of the existing admin's private key: [/etc/chef/admin.pem] 
Please enter the validation clientname: [chef-validator] 
Please enter the location of the validation key: [/etc/chef/validation.pem] 
Please enter the path to a chef repository (or leave blank): 
Creating initial API user...
Please enter a password for the new user: 
Created user[root]
Configuration file written to /root/.chef/knife.rb

作成したユーザの確認を行う。

# knife user list
admin
root

作成されてknife.rbはこんな感じ。

# cat /root/.chef/knife.rb 
log_level                :info
log_location             STDOUT
node_name                'root'
client_key               '/root/.chef/root.pem'
validation_client_name   'chef-validator'
validation_key           '/etc/chef/validation.pem'
chef_server_url          'https://<FQDNのホスト名>:443'
syntax_check_cache_path  '/root/.chef/syntax_check_cache'



管理対象ノードの設定

chef clientのインストールを行う。

Chefサーバ側で

管理対象ノードが利用する設定ファイルを作成する。

# knife configure client <設定ファイルを一時的に保存するディレクトリ>
# ls <設定ファイルを一時的に保存するディレクトリ>
client.rb
validation.pem
管理対象ノード側で

Chefクライアントのインストールを行う。

# sudo true && curl -L https://www.opscode.com/chef/install.sh | sudo bash
準備中...                ########################################### [100%]
   1:chef                   ########################################### [100%]
Thank you for installing Chef!

Chefサーバ側にある 設定ファイルをコピーする。

# scp root@chefserver:/<設定ファイルを一時的に保存するディレクトリ>/client.rb /etc/chef/
# scp root@chefserver:/<設定ファイルを一時的に保存するディレクトリ>/validation.pem /etc/chef/

Chef Serverに管理対象ノードを追加。

# chef-client
[2013-05-18T17:54:56+09:00] WARN: Node chefclient1.localdomain has an empty run list.
Converging 0 resources
[2013-05-18T17:54:56+09:00] INFO: Chef Run complete in 0.127357451 seconds
[2013-05-18T17:54:56+09:00] INFO: Running report handlers
[2013-05-18T17:54:56+09:00] INFO: Report handlers complete
Chef Client finished, 0 resources updated
Chefサーバ側で

管理対象ノードが登録されてたか確認する。

# knife node list
<管理対象ノードホスト名>

管理対象ノードホスト名が登録されればOK。

地味にはまったところ

サーバと管理対象ノードの時刻がずれていると発生するので、時刻を事前に合わせておく。

================================================================================
Chef encountered an error attempting to create the client "chefclient1.localdomain"
================================================================================


Authentication Error:
---------------------
Failed to authenticate to the chef server (http 401).
The request failed because your clock has drifted by more than 15 minutes.
Syncing your clock to an NTP Time source should resolve the issue.

ホスト名の解決エラーが出たら、サーバと管理対象ノードの両方で名前解決が出来るか確認する。
hostsで解決する場合には、ホスト名の記述順序に誤りが無いか確認する。

Errno::EHOSTUNREACH: No route to host - connect(2)

管理対象側の認証エラー系は、validation.pemをサーバから取り直してみる。
それでもダメなら、validation.pemとclient.rbとvalidation.pemの作り直し。
それでもダメなら、chef-server-ctl reconfigure と knife configure -i を再実行して、それ以降の作業をやり直す。