VagrantをMacにインストールする

VagrantをMacに導入する

インストール

VAGRANT DOCS GETTING STARTED に従って、

VirtualBoxのインストール

デフォルトのプロバイダ(仮想化環境)はVirtualBoxなので、
https://www.virtualbox.org/ からダウンロードしてインストールする

Vagrantのインストール

https://www.vagrantup.com/ からダウンロードしてインストールする

仮想マシンの立ち上げと実行

Vagrant作業ディレクトリの作成
このディレクトリにVagrantfileを設置して仮想マシンを起動する
Vagrantfileの設置ディレクトリがプロジェクトのルートディレクトリとなる
このディレクトリは共有ディレクトリとしても利用する

$ mkdir ~/Vagrant
$ cd ~/Vagrant
$ mkdir -p hashicorp/precise32
$ cd hashicorp/precise32

Vagrantfileの設置
HashiCorpはVagrantの開発元
Boxは下記のサイトでも公開されている

  • https://vagrantcloud.com/ : HashiCorp, Inc.がメンテナンス、もちろんココがベストとのこと
  • https://github.com/opscode/bento : Chef Software, Inc.がメンテナンス
  • http://www.vagrantbox.es/
$ vagrant init hashicorp/precise32
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.

仮想マシンの立ち上げ
家のネットが遅いため30分程かかった
立ち上げが完了すると、VirtualBoxマネージャーで実行中を確認できる

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'hashicorp/precise32' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'hashicorp/precise32'
    default: URL: https://vagrantcloud.com/hashicorp/precise32
==> default: Adding box 'hashicorp/precise32' (v1.0.0) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/hashicorp/boxes/precise32/versions/1/providers/virtualbox.box
==> default: Successfully added box 'hashicorp/precise32' (v1.0.0) for 'virtualbox'!
==> default: Importing base box 'hashicorp/precise32'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'hashicorp/precise32' is up to date...
==> default: Setting the name of the VM: precise32_default_1413688989216_94903
==> 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: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default: 
    default: Guest Additions Version: 4.2.0
    default: VirtualBox Version: 4.3
==> default: Mounting shared folders...
    default: /vagrant => /Users/catrio/Vagrant/hashicorp/precise32

仮想マシンの状態確認
コマンドはVagrantfileを設置したディレクトリで実行する

$ 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`.

仮想マシンへの接続
vagrant upコマンドの出力を参考に仮想マシンにSSHで接続してみる
パスワードはvagrant

$ ssh -p 2222 vagrant@127.0.0.1vagrant@127.0.0.1's password: 
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)

 * Documentation:  https://help.ubuntu.com/
New release '14.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Welcome to your Vagrant-built virtual machine.
Last login: Sun Oct 19 03:32:47 2014 from 10.0.2.2
vagrant@precise32:~$ 

仮想マシンへの接続
vagrant sshコマンドで接続するとパスワードは不要

$ vagrant ssh
The program 'vagrant' is currently not installed.  You can install it by typing:
sudo apt-get install vagrant
vagrant@precise32:~$

仮想マシンのシャットダウン

$ vagrant halt
==> default: Attempting graceful shutdown of VM...
$ vagrant status
Current machine states:

default                   poweroff (virtualbox)

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

仮想マシンの破棄
vagrant destroyしてもダウンロードしたBox(イメージ)は~/.vagrant.d/boxesに残されている

$ vagrant destroy
    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Destroying VM and associated drives...

CentOS6.5 x64 の仮想マシンを用意する

Vagrant Cloudで公開されているBoxを利用してCentOS6.5 x64の仮想マシン環境を用意する
Chefが用意している https://vagrantcloud.com/chef/boxes/centos-6.5 を利用する

Vagrant作業ディレクトリの作成

$ cd ~/Vagrant
$ mkdir -p chef/centos-6.5
$ cd chef/centos-6.5

Vagrantfileの設置

$ vagrant init chef/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.

仮想マシンの立ち上げ
今回は10分程度で立ち上げが完了した

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'chef/centos-6.5' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'chef/centos-6.5'
    default: URL: https://vagrantcloud.com/chef/centos-6.5
==> default: Adding box 'chef/centos-6.5' (v1.0.0) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/chef/boxes/centos-6.5/versions/1/providers/virtualbox.box
==> default: Successfully added box 'chef/centos-6.5' (v1.0.0) for 'virtualbox'!
==> default: Importing base box 'chef/centos-6.5'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'chef/centos-6.5' is up to date...
==> default: Setting the name of the VM: centos-65_default_1413694128367_64694
==> 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: Warning: Connection timeout. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/catrio/Vagrant/chef/centos-6.5

CentOS7.0 x64 の仮想マシンを用意する

Vagrant Cloudで公開されているBoxを利用してCentOS7.0 x64の仮想マシン環境を用意する
Chefが用意している https://vagrantcloud.com/chef/boxes/centos-7.0 を利用する

Vagrant作業ディレクトリの作成

$ cd ~/Vagrant
$ mkdir -p chef/centos-7.0
$ cd chef/centos-7.0

Vagrantfileの設置

$ vagrant init chef/centos-7.0
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.

仮想マシンの立ち上げ
今回は15分程度で立ち上げが始まったが、ネットワークの問題で起動に失敗したが
centos−6.5、centos-7.0共にdestroyとupをそれぞれ繰り返したら同時起動に成功した

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'chef/centos-7.0' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'chef/centos-7.0'
    default: URL: https://vagrantcloud.com/chef/centos-7.0
==> default: Adding box 'chef/centos-7.0' (v1.0.0) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/chef/boxes/centos-7.0/versions/1/providers/virtualbox.box
==> default: Successfully added box 'chef/centos-7.0' (v1.0.0) for 'virtualbox'!
==> default: Importing base box 'chef/centos-7.0'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'chef/centos-7.0' is up to date...
==> default: Setting the name of the VM: centos-70_default_1413724087746_73069
==> 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: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.

If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.