Chef 11 での client/server/knife のセットアップ手順(+α)

by dløuie



遅れながら、Chefの新しい11系のバージョンを触ってみました。
つまづいた途中経過を含めて、セットアップのログや動きで気付いた事を簡単に残しておきます。

尚、今回使ってみた実行環境は、CentOSの6系です(Linux)。


結論から申し上げますと、Chefの新しいバージョンは、サーバのセットアップが物凄く楽でした。
旧来のバージョンでもUbuntuはそこそこ楽でしたが、CentOSの面倒くささと言ったら、んもう。

インストールパッケージの入手先

最近は、下記のChef本家となるOpscodeのサイトから、client/serverともインストーラ(各OSでのパッケージ)をダウンロードできるようになっています。

Clientのインストール

上記サイトからパッケージをダウンロードしてインストールしてもよいのですが、Linuxであれば

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

このようなワンライナーを実行するだけで、最新版のChef(client)がインストールされます。
もし、インストールするChefのバージョンを指定する場合は、上記サイトから該当するバージョンを選択して、パッケージをダウンロードしてインストールする感じです。

# curl -L https://www.opscode.com/chef/install.sh | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  6510  100  6510    0     0   8945      0 --:--:-- --:--:-- --:--:-- 39695
Downloading Chef  for el...
Installing Chef
warning: /tmp/tmp.7Qob70VM/chef-.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 83ef826a: NOKEY
Preparing...                ########################################### [100%]
   1:chef                   ########################################### [100%]
Thank you for installing Chef!

この通り、あっさりインストールされました。

# chef-client -v
Chef: 11.4.4

現時点での最新版が動いております。

# ll /opt/chef/bin/
total 36
-rwxr-xr-x 1 root root 466 May  1 09:37 chef-apply
-rwxr-xr-x 1 root root 467 May  1 09:37 chef-client
-rwxr-xr-x 1 root root 466 May  1 09:37 chef-shell
-rwxr-xr-x 1 root root 465 May  1 09:37 chef-solo
-rwxr-xr-x 1 root root 468 May  1 09:37 erubis
-rwxr-xr-x 1 root root 461 May  1 09:37 knife
-rwxr-xr-x 1 root root 460 May  1 09:37 ohai
-rwxr-xr-x 1 root root 487 May  1 09:37 restclient
-rwxr-xr-x 1 root root 460 May  1 09:37 shef

このあたりが実行可能となります。

Serverのインストール

上記のOpscodeのサイトから"Chef Server"タブを選択し、OSの種類やバージョン、アーキテクチャ、そしてChef Serverのバージョンを順に選択していくと、パッケージバイナリのダウンロードリンクが表示されます。
それをコピーして、下記のような感じでインストールしてみましょう。(面倒くさいので以降rootで作業しておりますw)

# rpm -Uvh https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-server-11.0.8-1.el6.x86_64.rpm
Retrieving https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-server-11.0.8-1.el6.x86_64.rpm
warning: /var/tmp/rpm-tmp.eWnS91: Header V4 DSA/SHA1 Signature, key ID 83ef826a: NOKEY
Preparing...                ########################################### [100%]
   1:chef-server            ########################################### [100%]
Thank you for installing Chef Server!

The next step in the install process is to run:

sudo chef-server-ctl reconfigure

この通りインストールされました。ちなみに、このバージョン11.0.8のRHEL(CentOS)向けのrpmパッケージは170MBほどありましたので、ダウンロードに時間がかかると思うので、気長にお待ちくださいませ。


次のステップは "sudo chef-server-ctl reconfigure" の実行と書いてあるので、やってみましょう。

# chef-server-ctl reconfigure
Starting Chef Client, version 11.4.0
Compiling Cookbooks...
Recipe: chef-server::default
  * directory[/etc/chef-server] action create
    - create new directory /etc/chef-server
    - change mode from '' to '0775'
    - change owner from '' to 'root'
    - change group from '' to 'root'

[2013-05-24T16:03:33+09:00] WARN: Cloning resource attributes for directory[/var/opt/chef-server/chef-server-webui/etc] from prior resource (CHEF-3694)
[2013-05-24T16:03:33+09:00] WARN: Previous directory[/var/opt/chef-server/chef-server-webui/etc]: /opt/chef-server/embedded/cookbooks/chef-server/recipes/chef-server-webui.rb:31:in `block in from_file'
[2013-05-24T16:03:33+09:00] WARN: Current  directory[/var/opt/chef-server/chef-server-webui/etc]: /opt/chef-server/embedded/cookbooks/chef-server/definitions/unicorn_config.rb:21:in `block in from_file'
Generating RSA private key, 2048 bit long modulus
..............................................................+++


・・・・・省略・・・・・


[2013-05-24T16:22:28+09:00] FATAL: Stacktrace dumped to /opt/chef-server/embedded/cookbooks/cache/chef-stacktrace.out
[2013-05-24T16:22:28+09:00] FATAL: Mixlib::ShellOut::ShellCommandFailed: execute[verify-system-status] (chef-server::bootstrap line 24) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '22'
---- Begin output of curl -sf http://127.0.0.1:8000/_status ----
STDOUT:
STDERR:
---- End output of curl -sf http://127.0.0.1:8000/_status ----
Ran curl -sf http://127.0.0.1:8000/_status returned 22

あら。途中でエラーが出てこけてしまいました。

# curl -sf http://127.0.0.1:8000/_status
# echo $?
22

確かにexit codeは"22"が返っていますね。"0"じゃないといけないとのこと。
終了コード22って何じゃらほい。

# curl --fail http://127.0.0.1:8000/_status
curl: (22) The requested URL returned error: 500

ほう、500が返ってます。

CURLE_HTTP_RETURNED_ERROR (22)

This is returned if CURLOPT_FAILONERROR is set TRUE and the HTTP server returns an error code that is >= 400.

とのこと。

# curl http://127.0.0.1:8000/_status
{"status":"fail","upstreams":{"chef_solr":"fail","chef_sql":"pong"}}

普通に叩いてみると、ステータスがレスポンスとして返ってきて、"chef_solr"がfailってなっていますね。
というわけで、ログを見てみましょう。

# tail -n 1 /var/log/chef-server/chef-solr/current

2013-05-24_07:22:56.89065 Error: Exception thrown by the agent : java.net.MalformedURLException: Local host name unknown: java.net.UnknownHostException: chef-test01: chef-test01: Name or service not known

どうも、chef-solrで自分のホスト名の名前解決が出来ていないらしいです。というわけで、

127.0.0.1   chef-test01 localhost localhost.localdomain localhost4 localhost4.localdomain4

"/etc/hosts"に自分のhostname(今回の例だと、↑の"chef-test01")を追加。

# curl http://127.0.0.1:8000/_status
{"status":"pong","upstreams":{"chef_solr":"pong","chef_sql":"pong"}}

その後、もう一度cURLで叩いてみると、OKになりました。
ということで、気を取り直して、もう1度"chef-server-ctl reconfigure"を実行します。

# chef-server-ctl reconfigure

・・・・・省略・・・・・

Chef Client finished, 154 resources updated
chef-server Reconfigured!

今度は成功しました。
実行すると、何となくわかると思うのですが、Chef Serverのセットアップ自体は、バックグラウンドでChefのエンジンそのもので実行されています。
chef-server自体のCookbookについては、以下に展開されています。

  • /opt/chef-server/embedded/cookbooks/chef-server/


ここのrecipesの中身を見れば、インストール時に何をやっているかは把握できますし、chef-server自体の設定パラメータを変更したい場合は、以下パスのattributeで定義されているパラメータを変えてやればいいわけです。(実際に修正するのは以下パスのファイルではないですが。これはまた別のエントリで。)

  • /opt/chef-server/embedded/cookbooks/chef-server/attributes/default.rb


chef-server自体もcookbookで管理・状態定義されていて、ユーザからもそれが見える点は美しいですね。


次に、サーバが正しく動いているかのテストスイートを流します。

# chef-server-ctl test
Configuring logging...
Creating platform...
Starting Pedant Run: 2013-05-24 07:33:32 UTC
setting up rspec config for #<Pedant::OpenSourcePlatform:0x000000028d5958>
Configuring RSpec for Open-Source Tests
 _______  _______  _______  _______  _______  ______   _______
|       ||       ||       ||       ||       ||      | |       |
|   _   ||    _  ||  _____||       ||   _   ||  _    ||    ___|
|  | |  ||   |_| || |_____ |       ||  | |  || | |   ||   |___
|  |_|  ||    ___||_____  ||      _||  |_|  || |_|   ||    ___|
|       ||   |     _____| ||     |_ |       ||       ||   |___
|_______||___|    |_______||_______||_______||______| |_______|

     _______  _______  ______   _______  __    _  _______
    |       ||       ||      | |   _   ||  |  | ||       |
    |    _  ||    ___||  _    ||  |_|  ||   |_| ||_     _|
    |   |_| ||   |___ | | |   ||       ||       |  |   |
    |    ___||    ___|| |_|   ||       ||  _    |  |   |
    |   |    |   |___ |       ||   _   || | |   |  |   |
    |___|    |_______||______| |__| |__||_|  |__|  |___|

                    "Accuracy Over Tact"

                  === Testing Environment ===
                 Config File: /var/opt/chef-server/chef-pedant/etc/pedant_config.rb
       HTTP Traffic Log File: /var/log/chef-server/chef-pedant/http-traffic.log

Running tests from the following directories:
/opt/chef-server/embedded/service/chef-pedant/spec/api
Ruby?  Erlang? true
Run options:
  include {:focus=>true, :smoke=>true}
  exclude {:platform=>:multitenant, :cleanup=>true}
Creating client pedant_admin_client...


・・・・・省略・・・・・


Finished in 50.21 seconds
70 examples, 0 failures

問題なく、全てパスしました。OKそうです。

chef-clientの実行

今回は、Serverと同じマシンでClientを実行してみます。

まずは、10系のときと同じように、"/etc/chef/client.rb"に以下のような感じで、サーバのURLを設定します。HTTPではなくHTTPSを指定します。
(厳密には、HTTPでもいいのですが、おそらく多くのインターナルな環境ではnginxの設定を変更しないとHTTPではうまく動かないので、この話はまた別の機会に書きます。)

# cat /etc/chef/client.rb
chef_server_url "https://127.0.0.1"


次に、これも10系同様ですが、chef-validatorの秘密鍵を、"/etc/chef/validation.pem"として配置します。

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

これで設定は終わりです。chef-clientを実行してみましょう。

# chef-client
Starting Chef Client, version 11.4.4
Creating a new client identity for chef-test01 using the validator key.
resolving cookbooks for run list: []
Synchronizing Cookbooks:
Compiling Cookbooks...
[2013-05-24T17:35:28+09:00] WARN: Node chef-test01 has an empty run list.
Converging 0 resources
Chef Client finished, 0 resources updated

無事、サーバと通信できました。

knifeの実行

まずは、adminユーザ用の鍵(admin.pem)を、"/etc/chef"へコピーします。

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


次に、knifeの設定コマンドを実行します。
(今回は、rootユーザで実行していますが、適切なユーザで実行してください。)

# knife configure -i
WARNING: No knife configuration file found
Where should I put the config file? [/root/.chef/knife.rb]
Please enter the chef server URL: [http://chef-test01:4000] https://127.0.0.1
Please enter a name for the new user: [nami] knife
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[knife]
Configuration file written to /root/.chef/knife.rb

基本的にデフォルトのままでいいですが、chef server URLのところだけ、環境に合わせて設定してください。
(前述の通りですが、忘れずにHTTPSで設定してと...)

# knife client list
chef-test01
chef-validator
chef-webui

この通り、問題なくChef Serverと通信できました。

最後に

と、こんな感じで、以前のバージョンでのCentOSでのセットアップと比べて、随分簡単になっていると思います。
Chef serverが1つのパッケージ内に、フルセットでおさまっていることと、実際のセットアップ処理が、ChefのCookbookという単位で管理されていて、処理内容(Recipe)や設定パラメータ(Attribute)が見える状態になっている(簡単に変更できる)ことが素晴らしいですね。


次は、Chef Serverの中身とか設定について、気付いた事を書いていこうかなと思っております。
それでは! =͟͟͞͞(๑•̀=͟͟͞͞(๑•̀д•́=͟͟͞͞(๑•̀д•́๑)=͟͟͞͞(๑•̀д•́

追記

続きを書きました。


入門Chef Solo - Infrastructure as Code

入門Chef Solo - Infrastructure as Code