Windows7にVagrantをインストールして動かしてみる

業務都合でWindows7Vagrantをインストールして動かしてみた。

バージョン

インストール

まず、最新版のWindows向けVagrantをサイトからダウンロード。

Download Vagrant - Vagrant

インストーラーを起動するとセットアップウィザードの準備が始まる。

f:id:heroween:20140604121821p:plain

f:id:heroween:20140604121828p:plain

そこそこ時間のかかる準備が終わるとインストールできるようになる。

f:id:heroween:20140604121838p:plain

ライセンスに同意し、

f:id:heroween:20140604121853p:plain

インストールディレクトリ(デフォルトはC:¥HashiCorp¥Vagrant)を決定し、

f:id:heroween:20140604121912p:plain

インストール開始。

f:id:heroween:20140604121924p:plain

f:id:heroween:20140604121931p:plain

3分くらいでインストールが完了。

f:id:heroween:20140604121946p:plain

完了したらWindowsの再起動をする。

f:id:heroween:20140604121953p:plain

再起動後にコマンドプロントを開いてvagrantコマンドが叩けるか確認。

f:id:heroween:20140604122001p:plain

バージョンが表示されて確認完了。
セットアップウィザード通りに進めれば簡単にインストールできる。

動かしてみる

それでは、Vagrantのインストールが完了したので、今度は仮想マシンを起動するところまで動かしてみる。
なお、プロバイダにはVirtualBox 4.3.12を、BoxにはBentoからダウンロードしてきたCentOS 6.5の64bit版を使用する。

まずは、Boxの追加から。

C:\Users\heroween>vagrant box add C:\HashiCorp\VagrantBox\opscode_centos-6.5_chef-provisionerless.box --name opscode-centos-6.5
==> box: Adding box 'opscode-centos-6.5' (v0) for provider:
    box: Downloading: file://C:/HashiCorp/VagrantBox/opscode_centos-6.5_chef-provisionerless.box
    box: Progress: 100% (Rate: 9870k/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'opscode-centos-6.5' (v0) for 'virtualbox'!

C:\Users\heroween>vagrant box list
opscode-centos-6.5 (virtualbox, 0)

次に適当な作業ディレクトリを作って仮想マシンを起動してみる。

C:\Users\heroween\VagrantProject\SampleProject>vagrant init opscode-centos-6.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.

C:\Users\heroween\VagrantProject\SampleProject>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'opscode-centos-6.5'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: SampleProject_default_1401933415261_2874
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => C:/Users/heroween/VagrantProject/SampleProject

C:\Users\heroween\VagrantProject\SampleProject>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`.

無事に起動したので仮想マシンに接続する。

C:\Users\heroween\VagrantProject\SampleProject>vagrant ssh
`ssh` executable not found in any directories in the %PATH% variable. Is an
SSH client installed? Try installing Cygwin, MinGW or Git, all of which
contain an SSH client. Or use your favorite SSH client with the following
authentication information shown below:

Host: 127.0.0.1
Port: 2222
Username: vagrant
Private key: C:/Users/heroween/.vagrant.d/insecure_private_key

と、Windowssshコマンドを入れてない場合はvagrant sshでは接続できない(2014/6/6追記:Windowsにsshコマンド入れる手順を書いた)
取り敢えず今回は既に入ってたPuttyを使ってポートフォワードされたローカルの2222に接続し、

f:id:heroween:20140605115817p:plain

ユーザーとパスワードを入力。

f:id:heroween:20140605120006p:plain

接続完了。Windows上でCentOSが使えるようになった。
接続確認も終えたので、仮想マシンをシャットダウン。

C:\Users\heroween\VagrantProject\SampleProject>vagrant halt
==> default: Attempting graceful shutdown of VM...

C:\Users\heroween\VagrantProject\SampleProject>vagrant status
Current machine states:

default                   poweroff (virtualbox)

The VM is powered off. To restart the VM, simply run `vagrant up`

そして、仮想マシンを破棄する。

C:\Users\heroween\VagrantProject\SampleProject>vagrant destroy
    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Destroying VM and associated drives...

C:\Users\heroween\VagrantProject\SampleProject>vagrant status
Current machine states:

default                   not created (virtualbox)

The environment has not yet been created. Run `vagrant up` to
create the environment. If a machine is not created, only the
default provider will be shown. So if a provider is not listed,
then the machine is not created for that environment.

以上、特に躓くような事も無く、インストールから簡単な動作確認までできた。

今度はプロビジョニングやPackerなども試してみようと思う。