2014年7月2日水曜日

Vagrant 素振り

Vagrantを初めてさわってみた記録です。
印象は、Vagrant( ・∀・)イイ!! でした。

Vagrantとは何か。

ググりました。
主に以下のページを参照しました。
「Vagrant」って何ぞ?(・o・)
仮想環境構築ツール「Vagrant」で開発環境を仮想マシン上に自動作成する
Vagrant で作ったり壊したりできる Windows 環境を手に入れるまでの手順

使用した環境

Windows 7 Professional SP1 64bit(ホストPC)
VirtualBox 4.3.12
Vagrant 1.6.3
Git 1.9.4

やったこと

Vagrantをインストールする

Vagrantの公式サイトからWindows用のインストーラをダウンロードして、インストールした。
インストーラによるインストールが終わったあと、Windowsのパスに「C:\HashiCorp\Vagrant\bin」を追加した。このパスを通しておかないと、あとでGit Bashのターミナルからたどれない(はず)。

VirtualBoxをインストールする

VirtualBoxの公式サイトからWindows用のインストーラをダウンロードして、インストールした。

Gitをインストールする

Gitの公式サイトからWindows用のインストーラをダウンロードして、インストールした。
実際、gitバイナリは今必要ではなく、必要なのはsshバイナリ。GitのWindowsインストーラはMinGWの(おそらく主要な)コマンド一式をインストールしてくれて、そこにsshも入っているため今回インストールしている。
cygwinがすでにインストールされているなどしてsshが使えるなら、GitのWindowsインストーラによるインストールは不要。

Vagrantにさわる

Varant公式のGetting Startedを見ながら、コマンドを実行していきました。

まず、先にインストールしたGit Bashを起動して、そのターミナルから次のコマンドを実行する。vagrant up すると、インターネットから仮想マシンをダウンロードするので、それなりに時間かかります。
$ mkdir temp
$ cd temp
$ vagrant init hashicorp/precise32
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> 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: temp_default_1404227001641_88575
==> 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: 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 => C:/Users/yasu_kei/temp 
vagrant upの実行が完了して、以上のようにエラーが出ていないようであれば、仮想マシンはすでに起動しています。vagrant ssh を実行すると、仮想マシンにsshでログインできます。
$ vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)
 * Documentation:  https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:22:31 2012 from 10.0.2.2
vagrant@precise32:~$
これで仮想マシンにログインできているので、あとはやりたい放題できます。
現状で、Vagrantがデフォルトで設定してくれる設定があります。
その設定は、NATの設定、ポートフォワーディングの設定、共有フォルダの設定、です。

NATの設定

すでに仮想マシンからインターネットにアクセスできます。wgetやpingなど通ります。
vagrant@precise32:~$ wget http://www.google.co.jp
--2014-07-01 15:17:07--  http://www.google.co.jp/
Resolving www.google.co.jp (www.google.co.jp)... 173.194.117.223, 173.194.117.216, 173.194.117.207, ...
Connecting to www.google.co.jp (www.google.co.jp)|173.194.117.223|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: `index.html'
    [ <=>                                   ] 11,272      --.-K/s   in 0s
2014-07-01 15:17:08 (47.7 MB/s) - `index.html' saved [11272]
vagrant@precise32:~$

ポートフォワーディングの設定

先にvagrant up したときのログに出ていますが、ホストマシンの2222ポートが仮想マシンの22ポートにポートフォワーディングされています。なので、ホストマシンからsshクライアントで仮想マシンにログインできます。ログインアカウントは、「vagrant」、パスワードも、「vagrant」です。
$ ssh vagrant@localhost -p 2222
vagrant@localhost's password:
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)
 * Documentation:  https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Tue Jul  1 15:23:29 2014 from 10.0.2.2
vagrant@precise32:~$

共有フォルダの設定

仮想マシンの/vagrant ディレクトリがホストマシンとの共有フォルダとして設定されています。
vagrant@precise32:~$ cd /vagrant/
vagrant@precise32:/vagrant$ ls
Vagrantfile
vagrant@precise32:/vagrant$ touch aaa
vagrant@precise32:/vagrant$ ls
aaa  Vagrantfile
vagrant@precise32:/vagrant$

ホストマシンのvagrant up を実行したフォルダに、「aaa」ファイルが作成されているはずです。

以上デフォルトの仮想マシンの設定ですが、ここから自身で変更していくには、Vagrantfileファイルを編集します。Vagrantfileファイルは、Vagrant up を実行するフォルダに配置されています。Vagrantfileファイルの書式については、Vagrant公式サイトやインターネット上の情報を参照してください。

仮想マシンの止め方

止め方が3つあります。
vagrant suspend
 Windowsでいうスリープです。次回、vagrant up すると、suspend状態から仮想マシンが復帰します。
vagrant halt
 Windowsでいうシャットダウンです。次回、vagrant up すると、仮想マシンを起動します。
vagrant destroy
 haltしたあと、仮想マシンそのものを削除します。次回、vagrant up すると、仮想マシンを一から作成し直して、仮想マシンを起動します。(ただしインターネットからのダウンロードはし直しません)

vagrant up を実行したフォルダで、同じくコマンドを実行します。
$ vagrant halt
==> default: Attempting graceful shutdown of VM...

以上で、仮想マシンをVagrantで簡単に起動、停止できるようになりました。

次の話題

続く話題は、仮想マシンのプロビジョニング(初期設定)です。
ググったところによると、プロビジョニングには、Puppet、Chef、Ansible、Saltといったプロビジョニングに特化したツールがあるようで、それらツールをVagrantとともに用いるのが一般的なようです。

0 件のコメント:

コメントを投稿