Vagrantで簡単仮想マシン構築
VagrantはOracle VirtualBoxを利用した仮想マシンをコマンドラインから作成してくれるソフトウェアだ。 設定ファイルをRubyで書くことができ、Chef等とも連携できるので、開発環境をコマンドライン一発で作成することができる。更にはCapistranoと組み合わせてアプリケーションのデプロイも一括で行うことで完全自動でいつでもテスト環境をつくれたりもする。 仮想マシンを捨ててしまってもいつでも再構築できること、誰のところにでもすぐ同じ状態に展開できることは開発を進める上で非常にメリットがある。
以下ではまずはVagrantを利用した簡単な仮想マシン構築の手順を説明する(本当に説明したい内容はもっと違う話なのだが追って別のエントリで書いていくことにする)
Oracle VirtualBoxのインストール
https://www.virtualbox.org/にアクセスし左ナビのDownloadsからVirtualBox本体をダウンロードしてインストールする。
Vagrantのインストール
簡単に終わる
sudo gem install vagrant
雛形となるボックスの準備
Vagrantでは予め用意された仮想マシンの雛形を利用して仮想マシンを構築する。 下記はQuick startでよく使われるUbuntuの仮想マシンの雛形だが、他にも、以下のサイトに大量のboxが用意されている。 http://www.vagrantbox.es/ 例えばCentOSやScientific LinuxやGentooやDebian等だ。sudo vagrant box add lucid32 http://files.vagrantup.com/lucid32.box
これによって
[vagrant] Creating home directory since it doesn't exist: /Users/ryuzee/.vagrant.d
[vagrant] Creating home directory since it doesn't exist: /Users/ryuzee/.vagrant.d/tmp
[vagrant] Creating home directory since it doesn't exist: /Users/ryuzee/.vagrant.d/boxes
[vagrant] Creating home directory since it doesn't exist: /Users/ryuzee/.vagrant.d/logs
[vagrant] Downloading with Vagrant::Downloaders::HTTP...
[vagrant] Downloading box: http://files.vagrantup.com/lucid32.box
[vagrant] Extracting box...
[vagrant] Verifying box...
[vagrant] Cleaning up downloaded box…
となればOKだ。
なおCentOSの場合は
sudo vagrant box add centos_56_32 http://yum.mnxsolutions.com/vagrant/centos_56_32.box
とすればOK。
仮想マシン作成
仮想マシンの作成は、仮想マシンの設定ファイルを作成するディレクトリ作成、初期化、設定ファイル編集、起動の順に行う。 適当なディレクトリを作成して移動しsudo vagrant init
とすると
create Vagrant file
と表示される。これによってそのディレクトリにVagrantfileという設定ファイルが作成される。このファイルはRubyで記述する。 今回試したのは以下のような設定ファイルだ(余計なコメント行を除いてある)
Vagrant::Config.run do |config|
# 元となるboxの名前を指定
config.vm.box = "lucid32"
# 仮想マシンをGUIで起動する
config.vm.boot_mode = :gui
# ホストオンリーネットワークで以下のIPアドレスを割り当てる
config.vm.network "192.168.50.2"
# Chef-soloの設定
config.vm.provision :chef_solo do |chef|
# 今回は標準のレシピを使う
chef.recipe_url = "http://files.vagrantup.com/getting_started/cookbooks.tar.gz"
chef.add_recipe "vagrant_main"
end
# 仮想マシン自体の設定
config.vm.customize do |vm|
# メモリを1Gにする。
vm.memory_size = 1024
# 仮想マシンの名前(Virtualboxのメニューに出る)
vm.name = "vagrant_ubuntu"
end
end
起動
起動は以下のコマンドでOK。OSがなければ作成し起動。既に存在すればそのまま起動する。
sudo vagrant up
こんな感じに表示され、仮想マシンが起動すれば成功だ。
[default] VM already created. Booting if its not already running...
[default] Preparing host only network...
[default] Clearing any previously set forwarded ports...
[default] Forwarding ports...
[default] -- ssh: 22 => 2222 (adapter 1)
[default] Cleaning previously set shared folders...
[default] Creating shared folders metadata...
[default] Running any VM customizations...
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Enabling host only network...
[default] Mounting shared folders...
[default] -- v-root: /vagrant
[default] -- v-csc-1: /tmp/vagrant-chef-1/chef-solo-1
[default] Running provisioner: Vagrant::Provisioners::ChefSolo...
[default] Generating chef JSON and uploading...
[default] Running chef-solo...
[default] stdin: is not a tty
: stderr
[default] [Thu, 27 Oct 2011 09:31:13 -0700] INFO: *** Chef 0.10.2 ***
: stdout
[default] [Thu, 27 Oct 2011 09:31:14 -0700] INFO: Setting the run_list to ["recipe[base_packages]", "recipe[apt]"] from JSON
: stdout
(略)
MacOS Xの場合の注意
lucid32では起動直後にネットワークが動作しない問題があるため以下の手順でネットワークの設定を行う。- 起動したOSにvagrant / vagrant でログインする
- /etc/network/interfaces の設定において以下のようにコメントアウトする。
#pre-up sleep 2
- ネットワークの自動設定を行う。
sudo apt-get install sysv-rc-conf sysv-rc-conf networking on /etc/init.d/networking restart
sudo vagrant reload
で再起動する。以上で完了だ。
その他のコマンド
母艦からsshでログインするsudo vagrant ssh
ゲストOSを終了する
sudo vagrant halt
仮想ホストを削除する
sudo vagrant destroy
アジャイルコーチングやトレーニングを提供しています
株式会社アトラクタでは、アジャイル開発に取り組むチーム向けのコーチングや、認定スクラムマスター研修などの各種トレーニングを提供しています。ぜひお気軽にご相談ください。
詳細はこちら