カタカタブログ

SIerで働くITエンジニアがカタカタした記録を残す技術ブログ。Java, Oracle Database, Linuxが中心です。たまに数学やデータ分析なども。

Mac(Yosemite)にChef(12.0.3)とVagrant(1.7.2)を入れてプロビジョニングしてみた

今さら感もあるものの、最近流行りのChef + Vagrantでのプロビジョニングをようやく試す時間ができたのでやってみた。とりあえず練習として、Mac OS XにChefとVagrantを入れ、vagrantbox.esから入手したCent OS7の仮想マシンをVirtual Boxで起動しApacheインストールをやってみた。Apacheは簡単なChefクックブックでレシピを書いて入れてみる。Chef-soloを使っているので、確認作業以外の全ての環境構築作業を仮想マシンにログインすることなく、Mac OS側で実行できているのがポイント。

(※2015/2/27追記: Windows7版もやってみました)
Windows7にChef(11.6.0)とVagrant(1.7.2)を入れてプロビジョニングしてみた - カタカタブログ

構成

構成としては、Chefはknife-soloを使ってMac側から仮想ノードにインストールする方針にした。
必要なツールのバージョンは以下の通り。基本的には2015年2月現時点での最新のものでそろえた。

  • ホスト環境: Mac OS X Yosemite 10.10.2
  • 仮想ノードOS: Cent OS 7
  • Vagrant 1.7.2
    • vagrant-vbguest (0.10.0): Virtual Box Guest Addition更新のためのプラグイン
    • vagrant-omnibus (1.4.1): ホスト側から仮想ノードにChefをインストールするためのプラグイン
  • Virtual Box 4.3.3
  • Ruby 2.1.5
  • Chef 12.0.3
    • knife-solo 0.4.2
  • berkshelf 3.2.3

Virtual BoxとVagrantのインストール

それぞれ公式サイトから最新版をダウンロードし、インストールする。すでにインストール済みでも普通にインストーラを実行すればアップデートされるようなので、最新化しておく。

Vagrantのプラグインインストール

vagrant-vbguestインストール

$ vagrant plugin install vagrant-vbguest
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Installed the plugin 'vagrant-vbguest (0.10.0)'!

chefプラグインインストール

$ vagrant plugin install vagrant-omnibus
Installing the 'vagrant-omnibus' plugin. This can take a few minutes...
Installed the plugin 'vagrant-omnibus (1.4.1)'!

Cent OS 7のBoxをvagrantbox.esからcentos7という名前で取得

$ mkdir centos7
$ vagrant box add centos7 https://f0fff3908f081cb6461b407be80daf97f07ac418.googledrive.com/host/0BwtuV7VyVTSkUG1PM3pCeDJ4dVE/centos7.box
==> box: Adding box 'centos7' (v0) for provider:
    box: Downloading: https://f0fff3908f081cb6461b407be80daf97f07ac418.googledrive.com/host/0BwtuV7VyVTSkUG1PM3pCeDJ4dVE/centos7.box
==> box: Successfully added box 'centos7' (v0) for 'virtualbox'!

Vagrantでcentos7仮想マシン作成

vagrant init

$ cd centos7
$ vagrant init centos7
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
`vagrantup.com` for more information on using Vagrant.

Vagrantfile作成

作ったVagrantfileを以下のように編集する。

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
  config.vm.box = "centos7"
  config.vm.network "private_network", ip: "192.168.33.11"
  config.vm.hostname = "vmcentos"
  config.vbguest.auto_update = false # Guest Additions自動更新無効
  config.vm.provider "virtualbox" do |vb|
    vb.name = "centos7_vagrant"
    vb.memory = "2048"
  end
end

** vagrant up
仮想マシンを起動してみる。
>|bash|
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
(・・・略)
Failed to mount folders in Linux guest. This is usually becausethe "vboxsf" file system is not available. Please verify thatthe guest additions are properly installed in the guest andcan work properly. The command attempted was:mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrantmount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrantThe error output from the last command was:/sbin/mount.vboxsf: mounting failed with the error: No such device

初回はGuest Additionsのバージョン不一致エラーが発生するので、ここでvagrant-vbguestプラグインを使ってGuest Additionsを最新化する。

Guest Additions更新

$ vagrant vbguest
GuestAdditions versions on your host (4.3.22) and guest (4.3.14) do not match.
(・・・略)
Installing Virtualbox Guest Additions 4.3.22 - guest version is 4.3.14
Verifying archive integrity... All good.
Uncompressing VirtualBox 4.3.22 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 4.3.14 of VirtualBox Guest Additions...
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox non-DKMS kernel modules[  OK  ]
Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module[  OK  ]
Building the shared folder support module[  OK  ]
Building the OpenGL support module[  OK  ]
Doing non-kernel setup of the Guest Additions[  OK  ]
Starting the VirtualBox Guest Additions [  OK  ]
Installing the Window System drivers
Could not find the X.Org or XFree86 Window System, skipping.An error occurred during installation of VirtualBox Guest Additions 4.3.22. Some functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.

vagrant ssh

$ vagrant ssh
Last login: Fri Feb 13 22:57:46 2015 from 10.0.2.2
[vagrant@vmcentos ~]$ df -h
ファイルシス            サイズ  使用  残り 使用% マウント位置
/dev/mapper/centos-root   6.7G  1.4G  5.4G   21% /
devtmpfs                  915M     0  915M    0% /dev
tmpfs                     921M     0  921M    0% /dev/shm
tmpfs                     921M  8.3M  913M    1% /run
tmpfs                     921M     0  921M    0% /sys/fs/cgroup
/dev/sda1                 497M  124M  373M   25% /boot
none                      466G  185G  281G   40% /vagrant

/vagrantがマウントされていることを確認。

Chefセットアップ

Ruby確認

Rubyのバージョンを確認。今回は最新化した2.1.5で実施。

$ ruby -v
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin14.0]

bundlerでchef関連インストール

Gemfileに以下の3つを追記。

source "https://rubygems.org"

gem "chef"
gem "knife-solo"
gem "berkshelf"

bundlerでインストール

$ bundle install
$ bundle list | grep chef
  * chef (12.0.3)
  * chef-zero (3.2.1)
$ bundle list | grep knife
  * knife-solo (0.4.2)
$ bundle list | grep berkshelf
  * berkshelf (3.2.3)
  * berkshelf-api-client (1.2.1)

hosts登録

ホスト側(Mac)の/etc/hostsに仮想マシンのIPアドレスに対応するホスト名を追加する
今回はとりあえずvmdevという名前で登録しておく。
/etc/hosts

192.168.33.10  vmdev

仮想マシンにChefインストール

knife solo bootstrapコマンドで仮想マシンにChefをインストールする。

$ bundle exec knife solo bootstrap vagrant@vmdev
Bootstrapping Chef...
vagrant@vmdev's password:
vagrant@vmdev's password:
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 18378  100 18378    0     0   2956      0  0:00:06  0:00:06 --:--:--  4031
Downloading Chef 12.0.3 for el...
downloading https://www.opscode.com/chef/metadata?v=12.0.3&prerelease=false&nightlies=false&p=el&pv=7&m=x86_64
  to file /tmp/install.sh.3846/metadata.txt
trying wget...
url https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-12.0.3-1.x86_64.rpm
md5 3634d1a3b6ae2e5977361075da0f44cc
sha256 0ec6162b9d0ca2b2016ff02781d84905f712d64c7a81d01b0df88f977832f310
downloaded metadata file looks valid...
downloading https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-12.0.3-1.x86_64.rpm
  to file /tmp/install.sh.3846/chef-12.0.3-1.x86_64.rpm
trying wget...
Comparing checksum with sha256sum...
Installing Chef 12.0.3
installing with rpm...
警告: /tmp/install.sh.3846/chef-12.0.3-1.x86_64.rpm: ヘッダー V4 DSA/SHA1 Signature、鍵 ID 83ef826a: NOKEY
準備しています...              ################################# [100%]
更新中 / インストール中...
   1:chef-12.0.3-1                    ################################# [100%]
Thank you for installing Chef!
Generating node config 'nodes/vmdev.json'...
Running Chef on vmdev...
Installing Berkshelf cookbooks to 'cookbooks'...
DEPRECATED: Your Berksfile contains a site location pointing to the Opscode Community Site (site :opscode). Site locations have been replaced by the source location. Change this to: 'source "https://supermarket.chef.io"' to remove this warning. For more information visit https://github.com/berkshelf/berkshelf/wiki/deprecated-locations
Resolving cookbook dependencies...
Uploading the kitchen...
vagrant@vmdev's password:
vagrant@vmdev's password:
vagrant@vmdev's password:WARNING:Local cookbook_path '/Users/user/vagrant_vms/centos7_vagrant/cookbooks' does not exist
vagrant@vmdev's password:
vagrant@vmdev's password:
vagrant@vmdev's password:
vagrant@vmdev's password:
vagrant@vmdev's password:
vagrant@vmdev's password:
Generating solo config...
vagrant@vmdev's password:
Running Chef...
Starting Chef Client, version 12.0.3
Compiling Cookbooks...
Converging 0 resources
Running handlers:
Running handlers complete
Chef Client finished, 0/0 resources updated in 6.080571409 seconds

なぜかvagrantユーザのログインパスワードを何度か聞かれてしまった。。ssh鍵でパスワードなしでログインできるはずだが。。あとで見直す。

仮想マシンにログインし、Chefがインストールされたことを確認する。

$ vagrant ssh
Last login: Sun Feb 22 08:02:43 2015 from 192.168.33.1
[vagrant@vmcentos ~]$ knife -v
Chef: 12.0.3

Chefクックブック作成

クックブックのひな形作成

$ bundle exec knife solo init .
WARNING: No knife configuration file found
Creating kitchen...
Creating knife.rb in kitchen...
Creating cupboards...
Setting up Berkshelf...
$ ls
Berksfile Vagrantfile data_bags environments provisioning site-cookbooks
README.md cookbooks disk nodes roles

apacheのクックブック作成

$ bundle exec knife cookbook create apache -o site-cookbooks
** Creating cookbook apache in /Users/user/vagrant_vms/centos7_vagrant/site-cookbooks
** Creating README for cookbook: apache
** Creating CHANGELOG for cookbook: apache
** Creating metadata for cookbook: apache

apacheレシピ作成

site-cookbooks/apache/recipes/default.rb を以下の内容で編集する。

package "httpd" do
  action :install
end
service "httpd" do
  action [ :enable, :start ]
end

ノード情報記載

nodes/vmdev.jsonにapacheレシピを追加

{
  "run_list": ["recipe[apache]"],
  "automatic": {
    "ipaddress": "vmdev"
  }
}

Chef-soloを実行しApacheをインストール

$ bundle exec knife solo cook vagrant@vmdev
Running Chef on vmdev...
Checking Chef version...
vagrant@vmdev's password:
vagrant@vmdev's password:
Installing Berkshelf cookbooks to 'cookbooks'...
DEPRECATED: Your Berksfile contains a site location pointing to the Opscode Community Site (site :opscode). Site locations have been replaced by the source location. Change this to: 'source "https://supermarket.chef.io"' to remove this warning. For more information visit https://github.com/berkshelf/berkshelf/wiki/deprecated-locations
Resolving cookbook dependencies...
Uploading the kitchen...
vagrant@vmdev's password:WARNING:Local cookbook_path '/Users/user/vagrant_vms/centos7_vagrant/cookbooks' does not exist
vagrant@vmdev's password:
vagrant@vmdev's password:
vagrant@vmdev's password:
vagrant@vmdev's password:
vagrant@vmdev's password:
vagrant@vmdev's password:
Generating solo config...
vagrant@vmdev's password:
Running Chef...
Starting Chef Client, version 12.0.3
Compiling Cookbooks...
Converging 2 resources
Recipe: apache::default
  * yum_package[httpd] action install- install version 2.4.6-19.el7.centos of package httpd
  * service[httpd] action enable- enable service service[httpd]
  * service[httpd] action start- start service service[httpd]
Running handlers:
Running handlers complete
Chef Client finished, 3/3 resources updated in 17.400807599 seconds

またしても対話式に。。ただインストールはできたはず。

仮想マシンにログインし、apacheがインストールされたことを確認

$ vagrant ssh
Last login: Sun Feb 22 08:08:05 2015 from 192.168.33.1
[vagrant@vmcentos ~]$ ps -ef | grep httpd
root      4782     1  0 08:08 ?        00:00:00 /usr/sbin/httpd-DFOREGROUND
apache    4783  4782  0 08:08 ?        00:00:00 /usr/sbin/httpd-DFOREGROUND
apache    4784  4782  0 08:08 ?        00:00:00 /usr/sbin/httpd-DFOREGROUND
apache    4785  4782  0 08:08 ?        00:00:00 /usr/sbin/httpd-DFOREGROUND
apache    4786  4782  0 08:08 ?        00:00:00 /usr/sbin/httpd-DFOREGROUND
apache    4787  4782  0 08:08 ?        00:00:00 /usr/sbin/httpd-DFOREGROUND
vagrant   4817  4794  0 08:08 pts/0    00:00:00 grep --color=auto httpd

apacheがインストールされ、起動もしている!

まとめ

今回、Mac OS XにVagrant、Chefのセットアップから行い、仮想マシンのセットアップ、Apacheインストールまで仮想マシンにログインすることなく構築が完了したことを確認した。一部パスワードを手入力してしまい完全自動化とはいかなかったが。。そのうち調査してみる。ともかく、これでVagrantfileやクックブックをgitなどのリポジトリにあげておけば、git cloneするだけで仮想OSを上げることができる。あとはVagrantfileやクックブックを改良していけば、より使いやすい環境が手に入る!

関連記事