vagrantを使って自宅の検証環境を整える

f:id:komeiy:20140920161500j:plain

最近、自宅の検証環境はもっぱらvagrantを使用しています。もともとvirtualboxguiで十分でしたが使ってみると、ところどころ便利で手放せなくなります。

別になくても致命的ではないけど、あるとストレス軽減という感覚です。

  • 使い捨て仮想マシンが簡単に実現できる
  • sshとか毎回必要な設定を勝手に準備してくれます
  • knife-soloとの組み合わせが秀逸
  • sandboxがいい感じ

まぁ、1つのOSしか使わない場合は綺麗なOSのリンククローン作成しておけばいいのではとか思ったり。

あとはgoogleで試してみましたがクラウドのdevも使い勝手がいいです。GUIでポチポチするのが面倒なので。自宅PCのスペック不足を感じるこの頃ですが、いいスペックのマシンを買うよりもGCE+vagrant+knife-soloで使い捨てマシンを検証に使えばいいような気もしてきました。

これからVagrant始めようという方向けにサラッとOS起動までの流れです。環境はMAC OSを想定してます。

vagrantインストール

これは以下からダウンロードして次へ次へでインストールできます。

https://www.vagrantup.com/downloads.html

virtalboxがインストールされてない場合はも合わせてインストールしておいてください。

https://www.virtualbox.org/wiki/Downloads

vagrant用box追加

まず、今回以下のバージョンを使用しています。

komei:~ komei$ vagrant --version
Vagrant 1.6.3

boxの追加にはvagrant box addコマンドを使用します。 'centos'と書かれている部分がローカルでのbox名です。

boxのURLはhttp://www.vagrantbox.esから取得してください。一覧の'copy'を押してコピペです。

komei:~ komei$ vagrant box add centos64 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box
==> box: Adding box 'centos64' (v0) for provider: 
    box: Downloading: https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box
==> box: Successfully added box 'centos64' (v0) for 'virtualbox'!

さっそく使ってみる

vagrantはフォルダ単位でvagrantfileが作成され、その中の環境設定に従いboxを起動します。gitみたいなイメージというとわかりやすいかもしれません。

これから何個かマシンを立ち上げるのでVagrantフォルダを親にして配下にcentos64という名前でフォルダを作成してみました。

komei:~ komei$ mkdir -p ~/Vagrant/CentOS64
komei:~ komei$ cd ~/Vagrant/CentOS64/
komei:CentOS64 komei$ ls
komei:CentOS64 komei$ vagrant init centos64
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.
komei:CentOS64 komei$ ls
Vagrantfile

vagrant init で初期設定を行います。gitみたいです。 vagrant upして起動しろと記述がありますので早速実施してみます。

komei:CentOS64 komei$ 
komei:CentOS64 komei$ 
komei:CentOS64 komei$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: CentOS64_default_1408706002124_96211
==> 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: Remote connection disconnect. 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.3.6
    default: VirtualBox Version: 4.2
==> default: Mounting shared folders...
    default: /vagrant => /Users/komei/Vagrant/CentOS64
komei:CentOS64 komei$ 

コンソールの出力でわかるとおりネットワーク周りやsshの設定も実施しVMを立ち上げてくれました。statusを見るとrunningになっています。

komei:CentOS64 komei$ 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 sshを使用します。マルチVMを立ち上げた場合はマシン名を指定する必要があります。SSHをしてネットワーク周りの設定を確認してみます。

komei:CentOS64 komei$ vagrant ssh
[vagrant@vagrant-centos65 ~]$ 
[vagrant@vagrant-centos65 ~]$ ls
[vagrant@vagrant-centos65 ~]$ ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:4F:B8:06  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe4f:b806/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:396 errors:0 dropped:0 overruns:0 frame:0
          TX packets:301 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:41288 (40.3 KiB)  TX bytes:37065 (36.1 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

[vagrant@vagrant-centos65 ~]$ exit
logout
Connection to 127.0.0.1 closed.

使用可能なコマンドは以下の通りです。

komei:CentOS64 komei$ vagrant ?
Usage: vagrant [options] <command> [<args>]

    -v, --version                    Print the version and exit.
    -h, --help                       Print this help.

Common commands:
     box             manages boxes: installation, removal, etc.
     connect         connect to a remotely shared Vagrant environment
     destroy         stops and deletes all traces of the vagrant machine
     global-status   outputs status Vagrant environments for this user
     halt            stops the vagrant machine
     help            shows the help for a subcommand
     init            initializes a new Vagrant environment by creating a Vagrantfile
     login           log in to Vagrant Cloud
     package         packages a running vagrant environment into a box
     plugin          manages plugins: install, uninstall, update, etc.
     provision       provisions the vagrant machine
     rdp             connects to machine via RDP
     reload          restarts vagrant machine, loads new Vagrantfile configuration
     resume          resume a suspended vagrant machine
     share           share your Vagrant environment with anyone in the world
     ssh             connects to machine via SSH
     ssh-config      outputs OpenSSH valid configuration to connect to the machine
     status          outputs status of the vagrant machine
     suspend         suspends the machine
     up              starts and provisions the vagrant environment
     version         prints current and latest Vagrant version

For help on any individual command run `vagrant COMMAND -h`

Additional subcommands are available, but are either more advanced
or not commonly used. To see all subcommands, run the command
`vagrant list-commands`.

komei:CentOS64 komei$ vagrant suspend
==> default: Saving VM state and suspending execution...

vagrantはknife-soloやkitchenを使ったりする際に効果を発揮すると思います。これからchef覚えるって人は眠ってたMACを引っ張りだしてみるのもいいかと思います。


シェアして頂けると嬉しいです。
参考になったという方がいれば是非お願いしますm(_ _ )m
モチベーション維持の観点で非常に励みになります。

このエントリーをはてなブックマークに追加

Vagrant入門ガイド

Vagrant入門ガイド

入門Chef Solo - Infrastructure as Code

入門Chef Solo - Infrastructure as Code