[CentOS6][Chef] テスト環境の構築


Create: 2013/07/11
LastUpdate: 2013/08/04
[メニューに戻る]

ここでは、Chef11 を使用し、クライアント・サーバ構成でテスト環境を構築します。
以下のページを参考にしました。

最小構成でインストールしたCentOS6.4(64bit)を2台使用して、以下のように「Chefサーバ」、「ワークステーション」、「ノード」を導入します。

管理者は、管理サーバに chef ユーザでログインしてワークステーションを使用するか、WebGUIでChefサーバを操作することにします。
  • 管理サーバ
    192.168.1.67(chetos6g) ・・・ Chefサーバ + ワークステーション
  • 管理対象
    192.168.1.68(centos6h) ・・・ ノード

1.管理サーバにChefサーバをインストール


公式マニュアルのインストール手順は、以下のURLで参照できます。(英語です)

1.1.ChefサーバのRPMパッケージのダウンロード


ブラウザで以下のURLにアクセスして、Chefサーバのrpmパッケージをダウンロードします。
URLにアクセスしてページを表示したら、[Chef Server]タブをクリックします。すると、下図のようなページが表示されます。



インストール先の環境に合わせて、以下のように選択していきます。
  • [Select an Operating System] で “Enterprise Linux” を選択
  • [Select a Version] で “6” を選択
  • [Select an Architecture] で ”x86_64” を選択
  • [Chef Version] で 現時点で最新の“11.0.8" を選択
すると、Chefサーバの rpm パッケージが表示されるので、”chef-server-11.0.8-1.el6.x86_64.rpm” をダウンロードします。

1.2.管理サーバの /etc/hosts 設定


管理サーバのホスト名を /etc/hosts に設定します。(赤字部分)
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
#::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.67 centos6g

1.3.Chefサーバのインストール


管理サーバに root ユーザでログインして作業します。
上記1.1.で取得した rpm パッケージを管理サーバにアップロードして、以下のようにコマンドを実行します。
# rpm -ivh ./chef-server-11.0.8-1.el6.x86_64.rpm
以下のようにコマンドを実行して、Chefサーバの初期設定を行います。
実行結果の最後が以下の2行であれば成功です。
# chef-server-ctl reconfigure
~省略~
Chef Client finished, 155 resources updated
chef-server Reconfigured!
Chefサーバ のテストを実施します。実行結果が "0 failures" なら成功です。
# chef-server-ctl test
~省略~
Finished in 1 minute 1.99 seconds
70 examples, 0 failures

1.4.Chefサーバの状態確認


Chefサーバ関連のサービスの起動状態を確認します。以下のようにコマンドを実行して、"run" と表示されればサービスが起動中です。
# chef-server-ctl status
run: bookshelf: (pid 2882) 1507s; run: log: (pid 2881) 1507s
run: chef-expander: (pid 2743) 1513s; run: log: (pid 2742) 1513s
run: chef-server-webui: (pid 6478) 1177s; run: log: (pid 6477) 1177s
run: chef-solr: (pid 5996) 1204s; run: log: (pid 2655) 1519s
run: erchef: (pid 6677) 1154s; run: log: (pid 2967) 1501s
run: nginx: (pid 6658) 1156s; run: log: (pid 6657) 1156s
run: postgresql: (pid 2567) 1525s; run: log: (pid 2566) 1525s
run: rabbitmq: (pid 2290) 1541s; run: log: (pid 2289) 1541s
Chefサーバの起動・停止などは chef-server-ctl コマンドを使用します。以下のようにコマンドを実行すると操作方法をみることができます。
# chef-server-ctl --help
I don't know that command.
/opt/chef-server/embedded/bin/omnibus-ctl: command (subcommand)
cleanse
  Delete *all* private chef data, and start from scratch.
graceful-kill
  Attempt a graceful stop, then SIGKILL the entire process group.
help
  Print this help message.
hup
  Send the services a HUP.
int
  Send the services an INT.
kill
  Send the services a KILL.
once
  Start the services if they are down. Do not restart them if they stop.
reconfigure
  Reconfigure the application.
reindex
  Reindex all server data
restart
  Stop the services if they are running, then start them again.
service-list
  List all the services (enabled services appear with a *.)
show-config
  Show the configuration that would be generated by reconfigure.
start
  Start services if they are down, and restart them if they stop.
status
  Show the status of all the services.
stop
  Stop the services, and do not restart them.
tail
  Watch the service logs of all enabled services.
term
  Send the services a TERM.
test
  Run the API test suite against localhost.
uninstall
  Kill all processes and uninstall the process supervisor (data will be preserved).
初期状態では、サーバの起動・停止時にChefサーバが自動起動・停止するようには設定されていません。自分で設定する必要があります。

1.5.WebGUIの確認


ブラウザで以下のURLにアクセスします。IPアドレスは、管理サーバのものです。
  • https://192.168.1.67/
下図のような画面が表示されます。



初期ユーザと初期パスワードは、管理サーバで以下のようにgrepコマンドを実行して調べます。赤字部分がユーザとパスワードです。
# grep "web_ui_admin_" /etc/chef-server/chef-server-running.json
      "web_ui_admin_user_name": "admin",
      "web_ui_admin_default_password": "p@ssw0rd1"
ログインに成功すると下図の画面が表示されます。
この画面でパスワードを変更できますが、簡単なパスワードはエラーになります。アルファベットと数字は使用したほうが良さそうです。



2.ワークステーションの設定


公式マニュアルのインストール手順は、以下のURLで参照できます。(英語です)
今回は、管理サーバにワークステーションを設定します。ワークステーション用に chefユーザを作成し、管理者は、chefユーザで管理サーバにログインして、ワークステーションを利用(Chefサーバやクックブックを操作)することにします。

2.1.Chefクライアントのインストール


管理サーバに root ユーザでログインして作業します。
以下のようにコマンドを実行して Chefクライアントをインストールします。
# curl -L http://www.opscode.com/chef/install.sh | bash
Chefクライアントのバージョンを確認します。
# chef-client -v
Chef: 11.4.4

2.2.ワークステーション用のOSアカウント作成


Chefのワークステーション用にOSアカウントを作成します。
管理サーバに root ユーザでログインして、以下のようにコマンドを実行してchef ユーザを作成します。
# useradd -m -d /home/chef chef
# id chef
uid=500(chef) gid=500(chef) 所属グループ=500(chef)
# passwd chef
ユーザー chef のパスワードを変更。
新しいパスワード: ******
よくないパスワード: 短かすぎます
よくないパスワード: 簡単すぎます
新しいパスワードを再入力してください: ******
passwd: 全ての認証トークンが正しく更新できました。


2.3.パスの設定


Chefサーバの "/opt/chef/embedded/bin" には、以下のようなコマンドが格納されています。
$ ls /opt/chef/embedded/bin
autospec   erb       iconv      ncurses5-config   reset  testrb
bundle     fauxhai   infocmp    ncursesw5-config  ri     tic
c_rehash   gem       infotocap  openssl           rspec  toe
captoinfo  htmldiff  irb        rake              ruby   tput
clear      httparty  ldiff      rdoc              tabs   tset

このコマンドをPATHを省略して使えるようにしたいので、以下の1行を chef ユーザの "~/.bashrc" に設定します。
PATH=/opt/chef/embedded/bin:${PATH}

chef ユーザでログインしなおすか、以下のようにコマンドを実行して設定を有効にします。
$ . ~/.bashrc 

2.4.レポジトリ作成


レポジトリを作成します。
リポジトリとはChefの実行に必要なファイル一式を格納する大きな入れ物です。リポジトリにクックブックが入り、クックブックに得レシピが入る構成になります。
このレポジトリを Subversion や Git などで管理したりするようです。
opscode でレポジトリの雛形が公開されているので、それを利用します。
chef ユーザで以下のようにコマンドを実行します。
$ cd ~
$ wget -O xxx.tgz --no-check-certificate https://github.com/opscode/chef-repo/tarball/master
$ tar xvfz xxx.tgz
$ mv opscode-chef-repo-2c42c6a chef-repo
レポジトリの構成は以下のとおり。
$ tree /home/chef/chef-repo/
/home/chef/chef-repo/
├── LICENSE
├── README.md
├── Rakefile
├── certificates
│   └── README.md
├── chefignore
├── config
│   └── rake.rb
├── cookbooks
│   └── README.md
├── data_bags
│   └── README.md
├── environments
│   └── README.md
└── roles
    └── README.md


6 directories, 10 files
新規で Cookbook を作成する場合は、これでOKです。
ただし、上記手順でレポジトリを作成すると Opscode が Github で公開している Cookbook をknife コマンドで取得できませんでした。
GithubのCookbookを knifeコマンドでインストールしたい場合は、以下のようにレポジトリを作成したほうがいいみたいです。
$ cd ~
$ git clone https://github.com/opscode/chef-repo.git 

2.5.knife コマンドの初期設定


Chefのknifeコマンドで、Chefサーバに接続するためのユーザを新規作成します。
今回は、OSアカウントと同じユーザ名 ”chef” で作成します。
まず、knife コマンドでChefサーバに接続するために必要な鍵を用意します。
chef ユーザのホームディレクトリに ".chef" ディレクトリを作成して、そこに鍵を格納します。
root ユーザで以下のようにコマンドを実行します。
# cd /home/chef
# mkdir .chef
# cp /etc/chef-server/chef-validator.pem /home/chef/chef-repo/.chef/validation.pem
# cp /etc/chef-server/admin.pem /home/chef/chef-repo/.chef/.
# chown chef.chef /home/chef/chef-repo/.chef/*
knife コマンドの初期設定を行います。
chef ユーザで以下のようにコマンドを実行します。
$ knife configure -i
Please enter the chef server URL: [http://centos6g:4000] https://192.168.1.67
Please enter a name for the new user: [root] chef
Please enter the existing admin name: [admin]
Please enter the location of the existing admin's private key: [/etc/chef/admin.pem] /home/chef/.chef/admin.pem
Please enter the validation clientname: [chef-validator]
Please enter the location of the validation key: [/etc/chef/validation.pem] /home/chef/.chef/validation.pem
Please enter the path to a chef repository (or leave blank): /home/chef/chef-repo
Creating initial API user...
Please enter a password for the new user: ****** ※6桁以上
Created user[chef]
Configuration file written to /home/chef/.chef/knife.rb
knife コマンドが正常に使えるか確認します。
chef ユーザで、以下のようにコマンドを実行し、クライアントとユーザの一覧を表示してみます。
$ knife client list
chef-validator
chef-webui
$ knife user list
admin
chef
上記の初期設定で作成した chefユーザとパスワードでWebGUIにログインできるようになります。
ブラウザで確認すると、chef ユーザが追加されています。



3.ノードの登録


公式マニュアルのインストール手順は、以下のURLで参照できます。(英語です)
上記2で作成したワークステーションを利用して、管理対象のノードにChefクライアントをインストールして登録します。
管理サーバに chef ユーザでログインして、以下のようにコマンドを実行します。
この例では、管理対象(192.168.1.68)には、root ユーザで接続します。
$ knife bootstrap 192.168.1.68 -x root -P password
このとき、Opscode のサイトからインストーラをダウンロードするので、管理対象のノードは、インターネットへの接続環境と、DNSの設定が必要です。(このサイトが参考になります。)
これで、実行が完了すれば、管理対象へのChefクライアントのインストールと登録は完了です。
登録済みのノードは、以下のようにして確認します。
$ knife node list
centos6h
ノードの詳細は、以下のようにして確認できます。
$ knife node show centos6h
Node Name:   centos6h
Environment: _default
FQDN:
IP:          192.168.1.68
Run List:
Roles:
Recipes:
Platform:    centos 6.4
Tags:
ブラウザでは、下図のように確認できます。



ノードの Chefクライアントをデーモン化して、クックブックを自動的に反映したい場合は、別途作業が必要になります。
最後に、管理対象ノードの /etc/hosts に管理サーバの設定を追加します。
192.168.1.67 centos6g

4.テストツールのインストール


自分で作成したCookbook のテストをするために、以下の2つのテストツールをインストールします。使い方は、このサイトが参考になります。
テストツールはワークステーション(管理サーバのchefユーザ)で使用します。
  • Chefspec ・・・ ChefのCookbookのテストができるツール、目的のリソース定義がCookbookに記述されているかを確認する。
  • Foodcritic ・・・ CookbookレシピのSyntaxほか、全体的な書き方のチェックをする、矛盾なども指摘してくれる。
まず、Chefspec をインストールします。管理サーバに root ユーザデログインして以下のようにコマンド実行します。
この例ではドキュメントをインストールしないように指示しています。
# /opt/chef/embedded/bin/gem install chefspec --no-rdoc --no-ri --verbose
次に、Foodcritic をインストールしますが、必要なコンパイル環境やライブラリが不足しているので、まず、yumでそれらをインストールしてから gemコマンドを実行します。
# yum install gcc make libxml2-devel libxslt-devel
# /opt/chef/embedded/bin/gem install foodcritic --no-rdoc --no-ri --verbose