edo1z blog

プログラミングなどに関するブログです

Vagrant・chefの設定

$ vagrant -v

Vagrant 1.4.3

$ vagrant box list

centos6.5 (virtualbox)

$ vagrant init centos6.5

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の中身

# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "centos6.5"
end
$ vagrant up

色々表示されて、最後にエラーが表示される。

Failed to mount folders in Linux guest. This is usually beacuse the "vboxsf" file system is not available. Please verify that the guest additions are properly installed in the guest and can work properly. The command attempted was: mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` /vagrant /vagrant mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` /vagrant /vagrant
$ vagrant status

Current machine states:

default running (virtualbox)

The VM is running. To stop this VM, you can run vagrant halt to shut it down forcefully, or you can run vagrant suspend to simply suspend the virtual machine. In either case, to restart it again, simply run vagrant up.

Saharaプラグインを使う

スナップショット機能が使えるやつ。

$ vagrant plugin install sahara
Installing the 'sahara' plugin. This can take a few minutes... Installed the plugin 'sahara (0.0.17)'!
$ vagrant plugin list

sahara (0.0.17) vagrant-aws (0.4.1) vagrant-berkshelf (1.3.7) vagrant-omnibus (1.3.0) vagrant-vbguest (0.10.0)

Saharaをインストールすると、sanboxコマンドがつかえる。 vagrant sandbox onで、sandboxモードが有効になる。vagrant sandbox statusで状態確認できる。

$ vagrant sandbox status

[default] Sandbox mode is off

$ vagrant sandbox on

[default] Starting sandbox mode... 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

vagrant sandbox rollbackでサーバの状態を元に戻せる

vagrant ssh
sudo -i
yum install php
php -v
バージョン表示される
exit
exit

vagrant sandbox rollback
戻る

vagrant ssh
php -v
コマンドないといわれる
$ vagrant ssh-config --host hoge >> ~/.ssh/config

これで、ssh hogeでアクセスできるようになる。

chefでPHP、MySQL、Apacheをインストール・設定する

chefをインストールする 参考:初心者にお勧めするChefの正しいインストール方法 https://downloads.chef.io/chef-dk/ここからダウンロードして、インストールする。

knife-soloをインストールする。

$ sudo chef gem install knife-solo

Successfully installed knife-solo-0.4.2

Knifeの初回設定

$ knife configure

全部エンター押すと、~/.chef/knife.rbにknifeの設定ファイルが保存される。

vagrant hogeにchefをインストールする

$ knife solo prepare hoge

chefのリポジトリをつくる

$ knife solo init chef-repo

Creating kitchen... Creating knife.rb in kitchen... Creating cupboards... Setting up Berkshelf...

cookbookをつくる

$ knife cookbook create lamp -o site-cookbooks

recipeをつくる

$ vim site-cookbooks/lamp/recipes/default.rb

内容は、一旦下記を参考にする。 http://qiita.com/skinoshita/items/57ac059ff8b1008f5e1d#3-12

# stop iptables
service "iptables" do
  action [:stop, :disable]
end

# install apache
%w{httpd httpd-devel}.each do |pkg|
  package pkg do
    action :install
  end
end

# install php
%w{php php-common php-devel php-mbstring php-xml php-pear php-mysql}.each do |pkg|
  package pkg do
    action :install
  end
end

# install mysql
%w{mysql-server mysql-devel}.each do |pkg|
  package pkg do
    action :install
  end
end

# service configuration

service "httpd" do
  supports :status => true, :restart => true, :reload => true
  action [:enable, :start]
end

service "mysqld" do
  action [:enable, :start]
end

JSONファイルを編集する

$ vim ../nodes/hoge.json
{
  "run_list": [
        "lamp"
  ],
  "automatic": {
    "ipaddress": "hoge"
  }
}

Vagrantfileを編集する

# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "centos6.5"

  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = "chef-repo/site-cookbooks"
    chef.add_recipe "lamp"
  end

end

$ vagrant reload これで、chefが動作するが、reload中に下記のようなエラーが出る場合は、ここに記載の対応が必要。

Failed to mount folders in Linux guest. This is usually beacuse the “vboxsf” file system is not available. Please verify that the guest additions are properly installed in the guest and can work properly. The command attempted was: mount -t vboxsf -o uid=id -u vagrant,gid=getent group vagrant | cut -d: -f3 /vagrant /vagrant mount -t vboxsf -o uid=id -u vagrant,gid=id -g vagrant /vagrant /vagrant


vagrant reloadの結果、下記のようにchefが動いた。

[2015-04-24T06:40:25+00:00] INFO: Forking chef instance to converge... [2015-04-24T06:40:25+00:00] INFO: *** Chef 12.0.3 *** [2015-04-24T06:40:25+00:00] INFO: Chef-client pid: 4900 [2015-04-24T06:40:32+00:00] INFO: Setting the run_list to ["recipe[lamp]"] from CLI options [2015-04-24T06:40:32+00:00] INFO: Run List is [recipe[lamp]] [2015-04-24T06:40:32+00:00] INFO: Run List expands to [lamp] [2015-04-24T06:40:32+00:00] INFO: Starting Chef Run for vagrant-centos65.vagrantup.com [2015-04-24T06:40:32+00:00] INFO: Running start handlers [2015-04-24T06:40:32+00:00] INFO: Start handlers complete. [2015-04-24T06:40:32+00:00] INFO: service[iptables] disabled [2015-04-24T06:40:37+00:00] INFO: yum_package[httpd] installing httpd-2.2.15-39.el6.centos from base repository [2015-04-24T06:40:47+00:00] INFO: yum_package[httpd-devel] installing httpd-devel-2.2.15-39.el6.centos from base repository [2015-04-24T06:41:11+00:00] INFO: yum_package[php] installing php-5.3.3-40.el6_6 from updates repository [2015-04-24T06:41:23+00:00] INFO: yum_package[php-devel] installing php-devel-5.3.3-40.el6_6 from updates repository [2015-04-24T06:41:30+00:00] INFO: yum_package[php-mbstring] installing php-mbstring-5.3.3-40.el6_6 from updates repository [2015-04-24T06:41:41+00:00] INFO: yum_package[php-xml] installing php-xml-5.3.3-40.el6_6 from updates repository [2015-04-24T06:41:54+00:00] INFO: yum_package[php-pear] installing php-pear-1.9.4-4.el6 from base repository [2015-04-24T06:42:01+00:00] INFO: yum_package[php-mysql] installing php-mysql-5.3.3-40.el6_6 from updates repository [2015-04-24T06:42:09+00:00] INFO: yum_package[mysql-server] installing mysql-server-5.1.73-3.el6_5 from base repository [2015-04-24T06:42:29+00:00] INFO: yum_package[mysql-devel] installing mysql-devel-5.1.73-3.el6_5 from base repository [2015-04-24T06:42:52+00:00] INFO: service[httpd] enabled [2015-04-24T06:42:57+00:00] INFO: service[httpd] started [2015-04-24T06:42:57+00:00] INFO: service[mysqld] enabled [2015-04-24T06:42:58+00:00] INFO: service[mysqld] started [2015-04-24T06:42:58+00:00] INFO: Chef Run complete in 146.414070168 seconds [2015-04-24T06:42:58+00:00] INFO: Skipping removal of unused files from the cache [2015-04-24T06:42:58+00:00] INFO: Running report handlers [2015-04-24T06:42:58+00:00] INFO: Report handlers complete