Vagrantで複数の仮想サーバを作る

サーバ一台だけですむ場合は適当な仮想サーバを作って壊して遊べばいいんですが、複数のサーバ間の連携をテストしたいときにもVagantなら簡単にできちゃったりします。

そんなVagrantでマルチサーバのVagrantfileの作り方をメモっておきます。

前提

あらかじめvagrant box addで適当なboxが追加されているとします。
今回はsample-vmという名前でboxが追加されています。

同じsample-vmをつかって、sv1、sv2という名前でヴァーチャルマシンを作ります。

VM name:sv1
VM box:sample-vm
VM IP address:192.168.33.66

VM name:sv2
VM box:sample-vm
VM IP address:192.168.33.77

vagrant init

最初にvagrant init

$ vagrant init
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ができるので

config.vm.box = "sample-vm"

を、コメントアウトして以下のようなものを書き加えます。

$ vi Vagrantfile

# config.vm.box = "sample-vm"

# Multi VM configure
# "sv1" Virtual Machine
# VM name => sv1
# VM box  => sample-vm
# VM IP   => 192.168.33.66
  config.vm.define :sv1 do |sv1|
    sv1.vm.box = "sample-vm"
    sv1.vm.network :private_network, ip:"192.168.33.66"
  end

# "sv2" Virtual Machine
# VM name => sv2
# VM box  => sample-vm
# VM IP   => 192.168.33.77
  config.vm.define :sv2 do |sv2|
    sv2.vm.box = "sample-vm"
    sv2.vm.network :private_network, ip:"192.168.33.77"
  end

config.vm.defineからはじまる行で、それぞれの設定を行い、endで定義を終了します。

vagrant up

このあとに、

$ vagrant up

すると以下のように2つのVMが立ち上がります。

$ vagrant up
Bringing machine 'sv1' up with 'virtualbox' provider...
Bringing machine 'sv2' up with 'virtualbox' provider...
[sv1] Importing base box 'sample-vm'...
[sv1] Matching MAC address for NAT networking...
[sv1] Setting the name of the VM...
[sv1] Clearing any previously set forwarded ports...
[sv1] Creating shared folders metadata...
[sv1] Clearing any previously set network interfaces...
[sv1] Preparing network interfaces based on configuration...
[sv1] Forwarding ports...
[sv1] -- 22 => 2222 (adapter 1)
[sv1] Booting VM...
[sv1] Waiting for VM to boot. This can take a few minutes.
[sv1] VM booted and ready for use!
[sv1] Configuring and enabling network interfaces...
[sv1] Mounting shared folders...
[sv1] -- /vagrant
[sv2] Importing base box 'sample-vm'...
[sv2] Matching MAC address for NAT networking...
[sv2] Setting the name of the VM...
[sv2] Clearing any previously set forwarded ports...
[sv2] Fixed port collision for 22 => 2222. Now on port 2200.
[sv2] Creating shared folders metadata...
[sv2] Clearing any previously set network interfaces...
[sv2] Preparing network interfaces based on configuration...
[sv2] Forwarding ports...
[sv2] -- 22 => 2200 (adapter 1)
[sv2] Booting VM...
[sv2] Waiting for VM to boot. This can take a few minutes.
[sv2] VM booted and ready for use!
[sv2] Configuring and enabling network interfaces...
[sv2] Mounting shared folders...
[sv2] -- /vagrant

それぞれにSSHするには

$ vagrant ssh sv1

とかすると、つながります。

Multi-Machine - Vagrant Documentation vagrantで複数の仮想サーバを作る - 年中アイス

入門Chef Solo - Infrastructure as Code
伊藤直也 (2013-03-11)
売り上げランキング: 401

Rlated & Feedbacks

http://www.trapon.jp/cms/mt-tb.cgi/313