Vagrantで複数の仮想マシンを一度に起動させる
過去にVagrantで複数のOracle VM VirtualBoxを制御することを試したので記載します。
今回は以下のようなネットワーク構成を想定しました。
centos01にはIPフォワーディングとIPマスカレードの設定をします。
Vagrantfileの設定は以下のように書きました。
作業環境
OS: Windows7 Home Premium 64bit
VirtualBox: 5.0.2
Vagrant: 1.7.4
VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "centos67" config.vm.box_url = "https://github.com/CommanderK5/packer-centos-template/releases/download/0.6.7/vagrant-centos-6.7.box" config.vm.define "centos01" do |centos01| centos01.vm.hostname = "centos01" centos01.vm.network "private_network", ip:"172.16.1.1", virtualbox__intnet: "intnet" centos01.vm.provider "virtualbox" do |vb| vb.customize ["modifyvm", :id ,"--memory", "512"] end centos01.vm.provision "shell", inline: "sysctl -w net.ipv4.ip_forward=1" centos01.vm.provision "shell", inline: "iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE" end config.vm.define "centos02" do |centos02| centos02.vm.hostname = "centos02" centos02.vm.network "private_network", ip:"172.16.1.2", virtualbox__intnet: "intnet" centos02.vm.provider "virtualbox" do |vb| vb.customize ["modifyvm", :id ,"--memory", "512"] end end config.vm.define "centos03" do |centos03| centos03.vm.hostname = "centos03" centos03.vm.network "private_network", ip:"172.16.1.3", virtualbox__intnet: "intnet" centos03.vm.provider "virtualbox" do |vb| vb.customize ["modifyvm", :id ,"--memory", "512"] end end end
• Vagrantfile解説
- config.vm.network
VirtualBoxのネットワーク設定など。
config.vm.network "private_network", ip: "ゲストOS側に割り当てたいIPアドレス", virtualbox__intnet: "内部ネットワーク名"
VirtualBoxのゲストOS同士でネットワークを繋げたい場合は、
virtualbox__intnet "内部ネットワーク名"
の記入が必要です。"内部ネットワーク名"は共通にします。
- config.vm.provider
Provider (Virtualbox,VMware等)の基本設定など。メモリの設定は、
config.vm.provider "virtualbox" do |vb| vb.customize ["modifyvm", :id, "--memory", "割り当てたいメモリ量(Mbyte単位)"] end
のように書きます。
- config.vm.provision
VirutualBoxの起動後に実行させる処理の設定など。
config.vm.provision "shell", inline: "実行させたいコマンド"
• 確認
- vagrant up
$ vagrant up Bringing machine 'centos01' up with 'virtualbox' provider... Bringing machine 'centos02' up with 'virtualbox' provider... Bringing machine 'centos03' up with 'virtualbox' provider... ==> centos01: You assigned a static IP ending in ".1" to this machine. ==> centos01: This is very often used by the router and can cause the ==> centos01: network to not work properly. If the network doesn't work ==> centos01: properly, try changing this IP. ==> centos01: You assigned a static IP ending in ".1" to this machine. ==> centos01: This is very often used by the router and can cause the ==> centos01: network to not work properly. If the network doesn't work ==> centos01: properly, try changing this IP. ==> centos01: Clearing any previously set forwarded ports... ==> centos01: Clearing any previously set network interfaces... ==> centos01: Preparing network interfaces based on configuration... centos01: Adapter 1: nat centos01: Adapter 2: intnet ==> centos01: Forwarding ports... centos01: 22 => 2222 (adapter 1)
- centos01への接続
$ vagrant ssh centos01 Last login: Sat Jul 18 18:59:53 2015 from X.X.X.X [vagrant@centos01 ~]$
- centos01からcentos02(172.16.1.2)へping
[vagrant@centos01 ~]$ ping -c 4 172.16.1.2 PING 172.16.1.2 (172.16.1.2) 56(84) bytes of data. 64 bytes from 172.16.1.2: icmp_seq=1 ttl=64 time=3.40 ms 64 bytes from 172.16.1.2: icmp_seq=2 ttl=64 time=0.838 ms 64 bytes from 172.16.1.2: icmp_seq=3 ttl=64 time=1.07 ms 64 bytes from 172.16.1.2: icmp_seq=4 ttl=64 time=1.11 ms --- 172.16.1.2 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3614ms rtt min/avg/max/mdev = 0.838/1.607/3.404/1.043 ms
- centos01からcentos03(172.16.1.3)へping
[vagrant@centos01 ~]$ ping -c 4 172.16.1.3 PING 172.16.1.3 (172.16.1.3) 56(84) bytes of data. 64 bytes from 172.16.1.3: icmp_seq=1 ttl=64 time=9.60 ms 64 bytes from 172.16.1.3: icmp_seq=2 ttl=64 time=0.942 ms 64 bytes from 172.16.1.3: icmp_seq=3 ttl=64 time=0.977 ms 64 bytes from 172.16.1.3: icmp_seq=4 ttl=64 time=0.985 ms --- 172.16.1.3 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3505ms rtt min/avg/max/mdev = 0.942/3.127/9.606/3.740 ms
- centos01から外部(yahoo.co.jp)へのping
[vagrant@centos01 ~]$ ping -c 4 yahoo.co.jp PING yahoo.co.jp (182.22.59.229) 56(84) bytes of data. 64 bytes from f1.top.vip.ssk.yahoo.co.jp (182.22.59.229): icmp_seq=1 ttl=63 time=16.1 ms 64 bytes from f1.top.vip.ssk.yahoo.co.jp (182.22.59.229): icmp_seq=2 ttl=63 time=50.4 ms 64 bytes from f1.top.vip.ssk.yahoo.co.jp (182.22.59.229): icmp_seq=3 ttl=63 time=11.1 ms 64 bytes from f1.top.vip.ssk.yahoo.co.jp (182.22.59.229): icmp_seq=4 ttl=63 time=10.0 ms --- yahoo.co.jp ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3413ms rtt min/avg/max/mdev = 10.081/21.941/50.473/16.629 ms
- centos02への接続
$ vagrant ssh centos02 Last login: Sat Jul 18 18:22:55 2015 from X.X.X.X [vagrant@centos02 ~]$
- centos02からcentos01(172.16.1.1)へのping
[vagrant@centos02 ~]$ ping -c 4 172.16.1.1 PING 172.16.1.1 (172.16.1.1) 56(84) bytes of data. 64 bytes from 172.16.1.1: icmp_seq=1 ttl=64 time=1.07 ms 64 bytes from 172.16.1.1: icmp_seq=2 ttl=64 time=0.898 ms 64 bytes from 172.16.1.1: icmp_seq=3 ttl=64 time=0.907 ms 64 bytes from 172.16.1.1: icmp_seq=4 ttl=64 time=0.949 ms --- 172.16.1.1 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3630ms rtt min/avg/max/mdev = 0.898/0.957/1.076/0.077 ms
- centos02からcentos03(172.16.1.3)へのping
[vagrant@centos02 ~]$ ping -c 4 172.16.1.3 PING 172.16.1.3 (172.16.1.3) 56(84) bytes of data. 64 bytes from 172.16.1.3: icmp_seq=1 ttl=64 time=2.25 ms 64 bytes from 172.16.1.3: icmp_seq=2 ttl=64 time=0.879 ms 64 bytes from 172.16.1.3: icmp_seq=3 ttl=64 time=0.861 ms 64 bytes from 172.16.1.3: icmp_seq=4 ttl=64 time=0.977 ms --- 172.16.1.3 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3441ms rtt min/avg/max/mdev = 0.861/1.242/2.254/0.587 ms
- centos02から外部(yahoo.co.jp)へのping
[vagrant@centos02 ~]$ ping -c 4 yahoo.co.jp PING yahoo.co.jp (183.79.135.206) 56(84) bytes of data. 64 bytes from f1.top.vip.kks.yahoo.co.jp (183.79.135.206): icmp_seq=1 ttl=63 time=19.0 ms 64 bytes from f1.top.vip.kks.yahoo.co.jp (183.79.135.206): icmp_seq=2 ttl=63 time=21.0 ms 64 bytes from f1.top.vip.kks.yahoo.co.jp (183.79.135.206): icmp_seq=3 ttl=63 time=19.0 ms 64 bytes from f1.top.vip.kks.yahoo.co.jp (183.79.135.206): icmp_seq=4 ttl=63 time=19.6 ms --- yahoo.co.jp ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3524ms rtt min/avg/max/mdev = 19.015/19.679/21.034/0.821 ms
- centos03への接続
$ vagrant ssh centos03 Last login: Sat May 16 06:35:58 2015 from X.X.X.X [vagrant@centos03 ~]$
- centos03からcentos01(172.16.1.1)へのping
[vagrant@centos03 ~]$ ping -c 4 172.16.1.1 PING 172.16.1.1 (172.16.1.1) 56(84) bytes of data. 64 bytes from 172.16.1.1: icmp_seq=1 ttl=64 time=0.713 ms 64 bytes from 172.16.1.1: icmp_seq=2 ttl=64 time=0.947 ms 64 bytes from 172.16.1.1: icmp_seq=3 ttl=64 time=0.983 ms 64 bytes from 172.16.1.1: icmp_seq=4 ttl=64 time=0.905 ms --- 172.16.1.1 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3405ms rtt min/avg/max/mdev = 0.713/0.887/0.983/0.104 ms
- centos03からcentos02(172.16.1.2)へのping
[vagrant@centos03 ~]$ ping -c 4 172.16.1.2 PING 172.16.1.2 (172.16.1.2) 56(84) bytes of data. 64 bytes from 172.16.1.2: icmp_seq=1 ttl=64 time=1.08 ms 64 bytes from 172.16.1.2: icmp_seq=2 ttl=64 time=1.01 ms 64 bytes from 172.16.1.2: icmp_seq=3 ttl=64 time=1.83 ms 64 bytes from 172.16.1.2: icmp_seq=4 ttl=64 time=1.11 ms --- 172.16.1.2 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3432ms rtt min/avg/max/mdev = 1.015/1.262/1.833/0.331 ms
- centos03から外部(yahoo.co.jp)へのping
[vagrant@centos03 ~]$ ping -c 4 yahoo.co.jp PING yahoo.co.jp (183.79.135.206) 56(84) bytes of data. 64 bytes from f1.top.vip.kks.yahoo.co.jp (183.79.135.206): icmp_seq=1 ttl=63 time=67.7 ms 64 bytes from f1.top.vip.kks.yahoo.co.jp (183.79.135.206): icmp_seq=2 ttl=63 time=19.6 ms 64 bytes from f1.top.vip.kks.yahoo.co.jp (183.79.135.206): icmp_seq=3 ttl=63 time=19.3 ms 64 bytes from f1.top.vip.kks.yahoo.co.jp (183.79.135.206): icmp_seq=4 ttl=63 time=69.8 ms --- yahoo.co.jp ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3677ms rtt min/avg/max/mdev = 19.386/44.122/69.801/24.641 ms
大丈夫そうですね。
参考にして頂ければと思います。
以上になります。
参考:
vagrantのネットワークについて - Qiita
Vagrantのネットワーク周りのあれこれ - Septeni Engineer's Blog