Vagrantfileで複数VMを構築

Vagrantにおいて表題の通り。複数台構成の環境をVagrantで構築する。

■ 環境

  • Vagrant 1.7.4
  • Mac OSX (10.9.5)

■ Vagrantfile

Vagrantfileは下記のように準備した。

Vagrant.configure(2) do |config|
  config.vm.box = "chef/centos-6.6"

  config.vm.define "zoo1"
  config.vm.define "zoo2"
  config.vm.define "zoo3"
end

いずれZooKeeperを3台構成で稼動させる予定である。それぞれ個別の設定はまだしていない。

■ 起動

上記のように記載したVagrantfileを使って起動する。

$ vagrant up
Bringing machine 'zoo1' up with 'virtualbox' provider...
Bringing machine 'zoo2' up with 'virtualbox' provider...
Bringing machine 'zoo3' up with 'virtualbox' provider...
  :

3台を一気に起動しているのでローカルマシンに負荷が掛かるのは仕方がない。起動すると下記のように確認できる。

$ vagrant status
Current machine states:

zoo1                      running (virtualbox)
zoo2                      running (virtualbox)
zoo3                      running (virtualbox)

This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.
$

個別の状態も確認することができる。

$ vagrant status zoo1

■ SSH接続

名前を指定すれば、1台での時と同じようにSSHでログイン可能だ。

$ vagrant ssh zoo1

ちなみに名前を指定しない場合は下記のようにエラーとなる。

$ vagrant ssh
This command requires a specific VM name to target in a multi-VM environment.
$

他のコマンドも基本的に全て名前をつければ動作するであろう。

■ 状態変更

状態を変更してみる。

$ vagrant halt zoo2
  :
$ vagrant suspend zoo3
  :

この場合の”status”コマンドの結果は下記の通りだ。

$ vagrant status
Current machine states:

zoo1                      running (virtualbox)
zoo2                      poweroff (virtualbox)
zoo3                      saved (virtualbox)
  :

■ 破棄

全てを破棄する。

$ vagrant destroy
    zoo3: Are you sure you want to destroy the 'zoo3' VM? [y/N] y
==> zoo3: Discarding saved state of VM...
==> zoo3: Destroying VM and associated drives...
==> zoo3: Removing hosts
    zoo2: Are you sure you want to destroy the 'zoo2' VM? [y/N] y
==> zoo2: Destroying VM and associated drives...
==> zoo2: Removing hosts
    zoo1: Are you sure you want to destroy the 'zoo1' VM? [y/N] y
==> zoo1: Forcing shutdown of VM...
==> zoo1: Destroying VM and associated drives...
==> zoo1: Removing hosts
$

複数台構成の環境も容易に扱えて便利だ。

以上。

■ 関連

コメントを残す

メールアドレスが公開されることはありません。

CAPTCHA


このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください