ChefをインストールしてAristaを設定・操作する(前編)

f:id:komeiy:20140820204528p:plain

今回はChefをインストールしてAristaを操作してみたいと思います。
長くなりそうなので何回かに分けて書くつもりです。

(追記): ChefをインストールしてAristaを設定・操作する(後編) - ぽぽぽぽーんのネットワークとOSS

用語

Recipe:Rubyで書かれたプログラムコード。設定内容を記述したファイル。cookbookに入っている。
Cookbook:一つ以上のRecipeが含まれており、templateやAttiributeなども格納している。
Chef Server:CookbookやRecipeなどを集中管理し、Clientを管理するサーバ。
Chef Client:Chefの管理下に置かれるサーバ。
Chef Workstation:Knifeプラグインが置かれるサーバ。今回はサーバと同居している。

公式の説明もあるので参照すると良いと思います。
・Chef Serverとは
  http://docs.getchef.com/chef_overview_server.html
・Chef Clietnとは
  http://docs.getchef.com/essentials_chef_client.html

opscodeがgithubでcookbookを公開しています。
  https://github.com/opscode-cookbooks/

Chef Server インストール

触ってみないと全くイメージがつかないので、まずは環境を作るところから始めます。
最終的にはArista Networksのスイッチを追加する予定ですが、その前にサーバの準備からです。 サーバはCentOS 6を使用しています。検証環境であればViturl Boxで十分です。

最新バージョンは以下から自分の環境にあったものを選択してください。 http://www.getchef.com/chef/install/

# 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
warning: chef-server-11.0.8-1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 83ef826a: NOKEY
Preparing...                                                            (100%)#                                           (100%)##                                          (100%)###                                        
Thank you for installing Chef Server!

The next step in the install process is to run:

sudo chef-server-ctl reconfigure

Chef Serverの設定

続いてChef Serverの設定ですが、その前にサーバとなるIPの名前が解決できるようにする必要があります。
今回hostsを使用していますが、正規に立てる場合はDNSに登録してください。

# vi /etc/hosts
192.168.1.1 chef-server.test.local

# hostname chef-server.test.local

これで準備が整いました。ようやくChef Serverの設定です。

# chef-server-ctl reconfigure
Starting Chef Client, version 11.4.0
Compiling Cookbooks...

Chef Client failed. 24 resources updated

Chef Client failedが出ていますが、まだクライアントをインストールしていないので一旦無視します。

この時点でhttps://chef-server.test.localGUIが確認できるはずです。
デフォルトパスワードは右側に記載があります。

f:id:komeiy:20140805230126p:plain

Chef Workstation インストール

まずWorkstationをインストールします。Workstationはchefクライアントをインストールすると合わせてインストールされます。

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

証明書の設定

続いてサーバからWorkstationで使用する鍵をコピーします。
今回はサーバと同居しているのcpコマンドでコピーします。リモートの場合はSCP等でコピーしてください。

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

$ ls /etc/chef/
admin.pem  validation.pem

Chef Workstation/Clientの設定

knifeコマンドを使います。knifeコマンドはクライアント関連の操作で使います。
バージョンによって使用ポートが異なりますので注意してください。最新は443です。

$ su
パスワード:
# knife configure -i
Overwrite /home/user/.chef/knife.rb? (Y/N)Y
Please enter the chef server URL: [https://192.168.1.1:443]
Please enter a name for the new user: [user]
Please enter the existing admin name: [admin]
Please enter the location of the existing admin's private key: [/etc/chef-server/admin.pem] /etc/chef/admin.pem
Please enter the validation clientname: [chef-validator]
Please enter the location of the validation key: [/etc/chef-server/chef-validator.pem] /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[user]
Configuration file written to /home/user/.chef/knife.rb

前述の証明書が正しく配置できていない、違うものをコピーしたなどがあると以下のようなエラーになります。

$ knife configure -i
ERROR: Your private key could not be loaded from /etc/chef/admin.pem
Check your configuration file and ensure that your private key is readable

正しく設定がされていると以下のようにadmin以外のユーザが確認できます。
knifeコマンドが失敗する場合はそもそものインストールに問題がありますので見直してください。
この時点でclient.pemとknife.rbが生成されているはずです。

# knife user list
admin
user

問題がある場合は

admin.pemとvalidation.pemが正しく配置できているか。knife configure -iの設定値に問題がないかを確認してください。 設定を修正した場合は、サーバをreconfigureするなどしてください。ちなみにChef Client Failedが解消すると以下の出力になります。

# chef-server-ctl reconfigure
Starting Chef Client, version 11.4.0
Compiling Cookbooks...
Recipe: chef-server::default
  * directory[/etc/chef-server] action create (up to date)
  
  
Chef Client finished, 3 resources updated

証明書関連のエラーを一つ記載します。

# knife client list
ERROR: Your private key could not be loaded from /root/.chef/user.pem
Check your configuration file and ensure that your private key is readable

userを削除してやり直す場合は以下のコマンドを発行します。

# knife user delete user
Do you really want to delete user? (Y/N)Y
Deleted user[user]

サーバへクライアントを追加

chef-clientコマンドをクライアント側で発行します。 -cオプションで設定ファイル(.rb)の指定も可能です。
今回、自分自身をクライアントとしてChefサーバに登録します。

# chef-client
Starting Chef Client, version 11.12.8
Creating a new client identity for chef-server.test.local using the validator key.
resolving cookbooks for run list: []
Synchronizing Cookbooks:
Compiling Cookbooks...
[2014-07-15T11:43:08+09:00] WARN: Node chef-server.test.local has an empty run list.
Converging 0 resources

Running handlers:
Running handlers complete

Chef Client finished, 0/0 resources updated in 2.657475199 seconds

knife client listで見るとクライアントが追加されています。

# knife client list
chef-validator
chef-webui
chef-server.test.local

キャプチャを取り損ねたのでAristaのものを代用。以下の画像のようにGUIからもクライアントが確認できます。

f:id:komeiy:20140805230414p:plain

レシピ追加・適用

getting-startedというあらかじめ準備された簡単なレシピを適用します。cookbookはopscodeのレポジトリにありますのでこちらを使用します。

任意の場所で以下の通りgit cloneをしてください。cloneが成功すると「getting-started」フォルダができています。この操作はworkstationで実施します。

# git clone https://github.com/opscode-cookbooks/getting-started
Initialized empty Git repository in /root/chef/chef-repo/cookbooks/getting-started/.git/
remote: Reusing existing pack: 26, done.
remote: Total 26 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (26/26), done.
# ls
getting-started

getting-started/配下のrecipesフォルダに「default.rb」があるのを確認してください。

# cd getting-started/
# ls
README.md  attributes  metadata.rb  recipes  templates
# cd recipes/
# ls
default.rb

これが具体的な設定内容を記載したファイルでRubyで記述されています。
homeフォルダに権限0644でchef-getting-started.txtを作成しています。
テキストの中身はchef-getting-started.txt.erbから持ってきているようです。

# cat default.rb
#
# Cookbook Name:: getting-started
# Recipe:: default
#
# Copyright 2010, Opscode, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

template "#{ENV['HOME']}/chef-getting-started.txt" do
  source "chef-getting-started.txt.erb"
  mode "0644"
end

Templateの中のdefaultフォルダは以下に.erbファイルがありますので確認します。

# cat chef-getting-started.txt.erb
Welcome to Chef!

This is Chef version <%= node[:chef_packages][:chef][:version] %>.
Running on <%= node[:platform] %>.
Version <%= node[:platform_version] %>.

実際にレシピを適用して確認します。まずはcookbookをChef Serverへアップロードします。

# knife cookbook upload getting-started -o .
Uploading getting-started [0.4.0]
Uploaded 1 cookbook.

次にChef Serverにアップしたcookbookをclientに適用します。
ここまでWorkstationで操作します。

# knife node run_list add chef-server.test.local "recipe[getting-started]"
chef-server.test.local:
  run_list: recipe[getting-started]

最後にクライアントでchef-clientコマンドを実行します。最初と違いcookbookが同期されレシピが実行されているのが出力からもわかります。

# chef-client
Starting Chef Client, version 11.12.8
resolving cookbooks for run list: ["getting-started"]
Synchronizing Cookbooks:
  - getting-started
Compiling Cookbooks...
Converging 1 resources
Recipe: getting-started::default
  * template[/root/chef-getting-started.txt] action create
    - create new file /root/chef-getting-started.txt
    - update content in file /root/chef-getting-started.txt from none to 7f9d1b
        --- /root/chef-getting-started.txt      2014-07-15 16:39:36.588833647 +0900
        +++ /tmp/chef-rendered-template20140715-12755-1tx32mh   2014-07-15 16:39:36.589833688 +0900
        @@ -1 +1,6 @@
        +Welcome to Chef!
        +
        +This is Chef version 11.12.8.
        +Running on centos.
        +Version 6.5.
    - change mode from '' to '0644'


Running handlers:
Running handlers complete

結果確認

クライアントで見てみます。レシピ通りの内容でファイルが生成されているのがわかります。

# cat /root/chef-getting-started.txt
Welcome to Chef!

This is Chef version 11.12.8.
Running on centos.
Version 6.5.

今回、各種インストールからcookbookの適用・レシピ確認まで実施しました。
次回は、ネットワーク機器であるAristaをChefクライアントとして登録したいと思います。

続きはこちら: ChefをインストールしてAristaを設定・操作する(後編) - ぽぽぽぽーんのネットワークとOSS


シェアして頂けると嬉しいです。
参考になったという方がいれば是非お願いしますm(_ _ )m
モチベーション維持の観点で非常に励みになります。

このエントリーをはてなブックマークに追加

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

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

Chef活用ガイド コードではじめる構成管理

Chef活用ガイド コードではじめる構成管理