本日も乙

ただの自己満足な備忘録。

今更ながらMacOSXにVagrant+VirtualBoxをインストールして使ってみた

巷でVagrantが流行っているようです。Vagrant(ベイグラント)は仮想環境をコマンドで簡単に扱えるようにできるツールで、仮想環境なのでVirtualBoxVMwareの他にAmazon EC2や1時間1円で使えるSSDクラウドDigitalOceanでも使えます。

特に開発環境においては、VirtualBoxで作ることが多いのですが、コマンドで仮想マシンを建てたり壊したりできたり、複数人で同じ環境を使えるのでVagrantは重宝すると思います。

また、最近はインフラを自動で構築できるChefと組み合わせてより簡単に環境構築ができるようになってきています。 僕も仕事でChef(Chef-solo)を使うことになるのでVagrant仮想マシンを建てて、レシピを作って試していたりしています。

今回は、Vagrantをインストールして仮想マシンを起動してみます。

サーバ環境

Virtualboxをインストール

今回の記事では紹介しないため、省略します。

Vagrantをインストール

Download Vagrant からdmgファイルをダウンロードします。 インストールすると、vagrantコマンドが使えます。

$ vagrant --version
Vagrant 1.5.1 

基本的な使い方

Boxの登録

Boxとは仮想環境のベースファイルのことで、Boxファイルから仮想環境が作成されます。

Boxファイルのダウンロード

BoxはVagrantbox.esからダウンロードします。今回は「CentOS 6.5 x86_64」を選択します。

Box追加

$ vagrant box add centos65 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box

==> box: Adding box 'centos65' (v0) for provider:
    box: Downloading: https://github.com/2creatives/vagrant-centos/releases/download/v6.5.1/centos65-x86_64-20131205.box
==> box: Successfully added box 'centos65' (v0) for 'virtual box’!

Box名をcentos65としました。

Boxの確認

登録できているか確認します。

$ vagrant box list
centos65 (virtualbox)

Vagrantfileの作成

Vagrantfileとは、仮想環境を構築するための設定ファイルのことです。

まず、適当なディレクトリを作成します。

$ mkdir centos

Vagrantfileのひな形を作成します。 Box名は先ほどの「centos65」を使用します。

$ cd centos
$ vagrant init centos65
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/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "centos65"
end

Vagrantfileを編集します。

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

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "centos65"
  # ホスト名
  config.vm.hostname = "centos"
  # ホストオンリーネットワーク
  config.vm.network "private_network", ip: "192.168.56.70"

  config.vm.provider :virtualbox do |vb|
    # GUI mode off
    vb.gui = false
    # Memory 1024MB, CPU 2core
    vb.customize ["modifyvm", :id, "--memory", "1024", "--cpus", "2", "--ioapic", "on"]
    # ネットワークが遅くなるのを防ぐ対策
    vb.customize ["modifyvm", :id, "--natdnsproxy1", "off"]
    vb.customize ["modifyvm", :id, "--natdnshostresolver1", "off"]
  end
end

仮想マシンの起動

$ vagrant up

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos65'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: centos_default_1396257171124_49352
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> 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: Error: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => /vagrant/centos

仮想マシンにログイン

$ vagrant ssh

[vagrant@centos ~]$

ユーザ名、パスワードはともに「vagrant」です。

仮想マシンの停止、廃棄

仮想マシンを停止します。

$ vagrant halt
==> default: Attempting graceful shutdown of VM...

仮想マシンを廃棄します。廃棄すると元に戻せないので注意してください。

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

プラグイン

Vagrantには様々なプラグインがあります。 vagrantの便利に使えるプラグイン6選 - Qiitaで紹介されているすべてのプラグインを入れています。

  • sahara
    • すぐにロールバックできるのでChefのレシピを作っているときに便利
  • vagrant-cachier
  • vagrant-global-status
  • vagrant-vbguest
    • VirtualBoxの場合に自動でGuestAdditionを入れてくれるので便利
  • vagrant-vbox-snapshot
  • vagrant-omnibus
    • ChefのCookbookとVagrantfileを渡せば社内ですぐに同じ環境が作れそう

最後に

Vagrantのインストールと使い方、プラグインを紹介しました。 今後もVagrantの便利な使い方があったら紹介していきたいと思います。