Chef Severを用いたBoxのプロビジョニング(後編)~Chefクライアントを構築し、プロビジョニングを実行する

Chef Severを用いたBoxのプロビジョニング(後編)~Chefクライアントを構築し、プロビジョニングを実行する


Chef Severを用いたBoxのプロビジョニング(前篇)~Chefサーバーを構築するでは、Chefサーバーを構築しましたので、後編は、Chefクライアントの構築方法とプロビジョニング方法を解説します。



  1. クライアントVMの起動

    Base VMを指定して、ChefクライアントVMを起動します。


    $ mkdir -p ~/mybox/vagrant/chef-client-ubuntu-12.10-server-i386 && cd ~/mybox/vagrant/chef-client-ubuntu-12.10-server-i386
    $ vagrant init chef-client-ubuntu-12.10-server-i386
    $ vi Vagrantfile
    punio@PAPA ~
    $ mkdir -p ~/mybox/vagrant/chef-client-ubuntu-12.10-server-i386 && cd ~/mybox/vagrant/chef-client-ubuntu-12.10-server-i386
    punio@PAPA ~/mybox/vagrant/chef-client-ubuntu-12.10-server-i386
    $ vagrant init chef-client-ubuntu-12.10-server-i386
    A `Vagrantfile` has been placed in this directory. You are now
    ready to `vagrant up` your first virtual environment! Please read
    the comments in the Vagrantfile as well as documentation on
    `` for more information on using Vagrant.
    punio@PAPA ~/mybox/vagrant/chef-client-ubuntu-12.10-server-i386


  2. クライアントVMの設定



    vagrant@ubuntu-12:~$ sudo rm /etc/localtime
    vagrant@ubuntu-12:~$ sudo ln -s /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
    vagrant@ubuntu-12:~$ service --status-all 2>&1 | grep '+'
     [ + ]  ntp
     [ + ]  ssh
    vagrant@ubuntu-12:~$ sudo service ntp stop
     * Stopping NTP server ntpd                                              [ OK ]
    vagrant@ubuntu-12:~$ sudo vi /etc/ntp.conf
    vagrant@ubuntu-12:~$ sudo service ntp start
     * Starting NTP server ntpd                                              [ OK ]


    $ sudo vi /etc/hostname
    $ sudo hostname `cat /etc/hostname`



    $ echo -e "`ifconfig eth1 | grep 'inet addr' | awk '{print $2}' | sed  's/addr://'` `hostname` `hostname -s`" | sudo tee -a /etc/hosts
    $ echo -e " chef-server" | sudo tee -a /etc/hosts
    vagrant@ubuntu-12:~$ ifconfig eth1 | grep 'inet addr' | awk '{print $2}' | sed  's/addr://'
    vagrant@ubuntu-12:~$ echo -e " `hostname` `hostname -s`" | sudo tee -a /etc/hosts
    sudo: unable to resolve host client-ubuntu-12
    vagrant@ubuntu-12:~$ echo -e " chef-server" | sudo tee -a /etc/hosts chef-server
  3. chef-clientのインストール

    $ sudo apt-get -y install curl
    $ curl -L | sudo bash
    vagrant@client-ubuntu-12:~$ sudo apt-get -y install curl
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    The following extra packages will be installed:
    The following NEW packages will be installed:
      curl libcurl3
    0 upgraded, 2 newly installed, 0 to remove and 1 not upgraded.
    Need to get 400 kB of archives.
    After this operation, 964 kB of additional disk space will be used.
    Get:1 quantal-updates/main libcurl3 i386 7.27.0-1ubuntu1.4 [253 kB]
    Get:2 quantal-updates/main curl i386 7.27.0-1ubuntu1.4 [147 kB]
    Fetched 400 kB in 2s (160 kB/s)
    Selecting previously unselected package libcurl3:i386.
    (Reading database ... 58417 files and directories currently installed.)
    Unpacking libcurl3:i386 (from .../libcurl3_7.27.0-1ubuntu1.4_i386.deb) ...
    Selecting previously unselected package curl.
    Unpacking curl (from .../curl_7.27.0-1ubuntu1.4_i386.deb) ...
    Processing triggers for man-db ...
    Setting up libcurl3:i386 (7.27.0-1ubuntu1.4) ...
    Setting up curl (7.27.0-1ubuntu1.4) ...
    Processing triggers for libc-bin ...
    ldconfig deferred processing now taking place
    vagrant@client-ubuntu-12:~$ curl -L | sudo bash
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100  6790  100  6790    0     0   7062      0 --:--:-- --:--:-- --:--:--  9225
    Downloading Chef  for ubuntu...
    Installing Chef
    Selecting previously unselected package chef.
    (Reading database ... 58449 files and directories currently installed.)
    Unpacking chef (from .../tmp.RXvhy8JH/chef__i386.deb) ...
    Setting up chef (11.8.0-1.ubuntu.12.04) ...
    Thank you for installing Chef!
  4. ノード設定ファイルの設定


    $ sudo mkdir -p /etc/chef
    $ sudo cp /config/client.rb /etc/chef
    $ sudo cp /config/validation.pem /etc/chef
    $ sudo chmod 0400 /etc/chef/client.rb /etc/chef/validation.pem
    vagrant@client-ubuntu-12:~$ sudo mkdir -p /etc/chef
    vagrant@client-ubuntu-12:~$ sudo cp /config/client.rb /etc/chef
    vagrant@client-ubuntu-12:~$ sudo cp /config/validation.pem /etc/chef
    vagrant@client-ubuntu-12:~$ sudo chmod 0400 /etc/chef/client.rb /etc/chef/validation.pem

  5. WorkStation認証鍵の設定


    $ cat /config/ >> ~/.ssh/authorized_keys
    $ chmod 0600 ~/.ssh/authorized_keys
    $ chmod 0700 ~ ~/.ssh
    $ chown -R vagrant:vagrant ~
    vagrant@client-ubuntu-12:~$ cat /config/ >> ~/.ssh/authorized_keys
    vagrant@client-ubuntu-12:~$ chmod 0600 ~/.ssh/authorized_keys
    vagrant@client-ubuntu-12:~$ chmod 0700 ~ ~/.ssh
    vagrant@client-ubuntu-12:~$ chown -R vagrant:vagrant ~
  6. ノード登録


    $ sudo chef-client
    vagrant@client-ubuntu-12:~$ sudo chef-client
    [2013-11-28T16:13:46+09:00] INFO: Forking chef instance to converge...
    Starting Chef Client, version 11.8.0
    [2013-11-28T16:13:46+09:00] INFO: *** Chef 11.8.0 ***
    [2013-11-28T16:13:46+09:00] INFO: Chef-client pid: 2452
    Creating a new client identity for using the validator key.
    [2013-11-28T16:13:47+09:00] INFO: Client key /etc/chef/client.pem is not present - registering
    [2013-11-28T16:13:47+09:00] INFO: HTTP Request Returned 404 Object Not Found: error
    [2013-11-28T16:13:47+09:00] INFO: Run List is []
    [2013-11-28T16:13:47+09:00] INFO: Run List expands to []
    [2013-11-28T16:13:47+09:00] INFO: Starting Chef Run for
    [2013-11-28T16:13:47+09:00] INFO: Running start handlers
    [2013-11-28T16:13:47+09:00] INFO: Start handlers complete.
    [2013-11-28T16:13:47+09:00] INFO: HTTP Request Returned 404 Object Not Found:
    resolving cookbooks for run list: []
    [2013-11-28T16:13:47+09:00] INFO: Loading cookbooks []
    Synchronizing Cookbooks:
    Compiling Cookbooks...
    [2013-11-28T16:13:47+09:00] WARN: Node has an empty run list.
    Converging 0 resources
    [2013-11-28T16:13:47+09:00] INFO: Chef Run complete in 0.290557519 seconds
    [2013-11-28T16:13:47+09:00] INFO: Running report handlers
    [2013-11-28T16:13:47+09:00] INFO: Report handlers complete
    Chef Client finished, 0 resources updated
    [vagrant@chef-server ~]$ 

  7. ノード確認


    $ knife node list
    [vagrant@chef-server ~]$ knife node list
    [vagrant@chef-server ~]$ 



  1. Berkshelfのインストール

    $ sudo su -
    # echo 'export PATH="/opt/chef/embedded/bin:$PATH"' >> ~/.bashrc
    # source ~/.bashrc
    # gem i berkshelf --no-ri --no-rdoc
    [vagrant@chef-server ~]$ sudo su -
    [root@chef-server ~]# echo 'export PATH="/opt/chef/embedded/bin:$PATH"' >> ~/.bashrc
    [root@chef-server ~]# source .bashrc
    [root@chef-server ~]# gem i berkshelf --no-ri --no-rdoc
    Fetching: addressable-2.3.5.gem (100%)
    Fetching: buff-ruby_engine-0.1.0.gem (100%)
    Fetching: buff-shell_out-0.1.1.gem (100%)
    Fetching: chozo-0.6.1.gem (100%)
    Fetching: multipart-post-1.2.0.gem (100%)
    Fetching: faraday-0.8.8.gem (100%)
    Fetching: minitar-0.5.4.gem (100%)
    Fetching: retryable-1.3.3.gem (100%)
    Fetching: buff-extensions-0.5.0.gem (100%)
    Fetching: varia_model-0.2.0.gem (100%)
    Fetching: buff-config-0.4.0.gem (100%)
    Fetching: buff-ignore-1.1.1.gem (100%)
    Fetching: timers-1.1.0.gem (100%)
    Fetching: celluloid-0.14.1.gem (100%)
    Fetching: nio4r-0.5.0.gem (100%)
    Building native extensions.  This could take a while...
    Fetching: celluloid-io-0.14.1.gem (100%)
    Fetching: net-http-persistent-2.9.gem (100%)
    Fetching: solve-0.8.1.gem (100%)
    Fetching: ffi-1.9.3.gem (100%)
    Building native extensions.  This could take a while...
    Fetching: gssapi-1.0.3.gem (100%)
    Fetching: httpclient- (100%)
    Fetching: mini_portile-0.5.2.gem (100%)
    Fetching: nokogiri-1.6.0.gem (100%)
    Building native extensions.  This could take a while...
    Fetching: rubyntlm-0.1.1.gem (100%)
  2. chefリポジトリの作成

    $ sudo yum -y install git
    $ cd
    $ git clone git://
    [vagrant@chef-server ~]$ sudo yum -y install git
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base:
     * extras:
     * updates:
    Setting up Install Process
    Resolving Dependencies
    --> Running transaction check
    ---> Package git.x86_64 0:1.7.1-3.el6_4.1 will be installed
    --> Processing Dependency: perl-Git = 1.7.1-3.el6_4.1 for package: git-1.7.1-3.el6_4.1.x86_64
    --> Processing Dependency: rsync for package: git-1.7.1-3.el6_4.1.x86_64
    --> Processing Dependency: perl(Git) for package: git-1.7.1-3.el6_4.1.x86_64
    --> Processing Dependency: perl(Error) for package: git-1.7.1-3.el6_4.1.x86_64
    --> Running transaction check
    ---> Package perl-Error.noarch 1:0.17015-4.el6 will be installed
    ---> Package perl-Git.noarch 0:1.7.1-3.el6_4.1 will be installed
    ---> Package rsync.x86_64 0:3.0.6-9.el6_4.1 will be installed
    --> Finished Dependency Resolution
    [vagrant@chef-server ~]$ cd
    [vagrant@chef-server ~]$ git clone git://
    Initialized empty Git repository in /home/vagrant/chef-repo/.git/
    remote: Counting objects: 223, done.
    remote: Compressing objects: 100% (139/139), done.
    remote: Total 223 (delta 80), reused 178 (delta 49)
    Receiving objects: 100% (223/223), 38.14 KiB, done.
    Resolving deltas: 100% (80/80), done.
    [vagrant@chef-server ~]$
  3. Berkshelfの初期化

    $ cd chef-repo
    $ berks init
    [vagrant@chef-server ~]$ cd chef-repo/
    [vagrant@chef-server chef-repo]$ berks init
          create  Berksfile
          create  Thorfile
        conflict  .gitignore
    Overwrite /home/vagrant/chef-repo/.gitignore? (enter "h" for help) [Ynaqdh] y
           force  .gitignore
          create  Gemfile
          create  Vagrantfile
    Successfully initialized
    [vagrant@chef-server chef-repo]$ 
  4. ~/.chef/knife.rbの編集



    cookbook_path [


    knife[:ssh_user] = vagrant
    knife[:use_sudo] = true
    knife[:identity_file] = /home/vagrant/.ssh/workstation_rsa_key
  5. Cookbookのインストール


    $ echo "cookbook 'chef-client'" >> Berksfile
    $ berks install -p cookbooks
    $ echo "cookbook 'apache2'" >> Berksfile
    $ berks install -p cookbooks
    [vagrant@chef-server chef-repo]$ echo "cookbook 'chef-client'" >> Berksfile
    [vagrant@chef-server chef-repo]$ berks install -p cookbooks
    Installing chef-client (3.1.0) from site: ''
    Installing cron (1.2.8) from site: ''
    Installing logrotate (1.4.0) from site: ''
    [vagrant@chef-server chef-repo]$ echo "cookbook 'apache2'" >> Berksfile
    [vagrant@chef-server chef-repo]$ berks install -p cookbooks
    Using chef-client (3.1.0)
    Installing apache2 (1.8.4) from site: ''
    Using cron (1.2.8)
    Using logrotate (1.4.0)
    [vagrant@chef-server chef-repo]$ 
  6. Cookbookのアップロード


    $ knife cookbook upload -a
    [vagrant@chef-server chef-repo]$ knife cookbook upload -a
    Uploading apache2      [1.8.4]
    Uploading chef-client  [3.1.0]
    Uploading cron         [1.2.8]
    Uploading logrotate    [1.4.0]
    Uploaded all cookbooks.
    [vagrant@chef-server chef-repo]$
  7. ノード構築

    apache2 Cookbookをノードに適用します。

    $ knife bootstrap -r apache2
    [vagrant@chef-server chef-repo]$ knife bootstrap -r apache2
    Bootstrapping Chef on
    Failed to authenticate  - trying password auth
    Enter your password: Starting Chef Client, version 11.8.0 resolving cookbooks for run list: ["apache2"] Synchronizing Cookbooks:   - apache2 Compiling Cookbooks... [2013-11-28T16:54:18+09:00] WARN: Cloning resource attributes for service[apache2] from prior resource (CHEF-3694) [2013-11-28T16:54:18+09:00] WARN: Previous service[apache2]: /var/chef/cache/cookbooks/apache2/recipes/default.rb:24:in `from_file' [2013-11-28T16:54:18+09:00] WARN: Current  service[apache2]: /var/chef/cache/cookbooks/apache2/recipes/default.rb:210:in `from_file' Converging 34 resources Recipe: apache2::default   * package[apache2] action install     - install version 2.2.22-6ubuntu2.3 of package apache2   * service[apache2] action enable     - enable service service[apache2]         +  BrowserMatch "^WebDrive" redirect-carefully         +  BrowserMatch "^WebDAVFS/1.[012]" redirect-carefully         +  BrowserMatch "^gnome-vfs/1.0" redirect-carefully         +  BrowserMatch "^XML Spy" redirect-carefully         +  BrowserMatch "^Dreamweaver-WebDAV-SCM1" redirect-carefully        * execute[a2enmod setenvif] action run (skipped due to not_if) Recipe: apache2::default   * execute[a2dissite default] action run     - execute /usr/sbin/a2dissite default   * service[apache2] action start (up to date)   * service[apache2] action restart     - restart service service[apache2] Chef Client finished, 18 resources updated


ここまで、前編、後編(この記事)の2回に渡って、Chefサーバー、Chefクライアントの構築からクライアントのプロビジョニングまで解説しました。Chefサーバーを構築できれば、Chefクライアントの登録は比較的に簡単に行えますので、いろいろなChefクライアントを起動してプロビジョニングにチェレンジして下さい。また、Chefクライアントの構築は、定形作業になりますので、shell scriptをクライアントごとに作成し、Vagrantのshell provisioningで実行すると、より簡単にChefクライアントのセットアップができます。

Comments are closed.