VagrantでマルチVM
vegrantで複数のVMを立ち上げてお互いに通信させる方法をメモしておきます。
環境(前提条件)
◆以下の手順に沿って単体のVMは構築済みです。今回はこちらで構築したCentOS6.7のVM(ゲストOS)を複数立ち上げます。
saosao-0706.hatenablog.com
◆Vagrantfileの格納されているディレクトリ(ホストOS)の場所は「/root/vagrant/centos67」です。
Vagrantfile変更
# vi /root/vagrant/centos67/Vagrantfile ・ ・ config.vm.define :node1 do |node1| node1.vm.network "private_network", ip: "192.168.33.10" end config.vm.define :node2 do |node2| node2.vm.network "private_network", ip: "192.168.33.11" end
上記の4行を追加してnode単位に内部ネットワーク用のIPアドレスを付与するようにします。
一旦構築したVMを削除
# cd /root/vagrant/centos67/
# vagrant destroy
VM(ゲストOS)の起動
# cd /root/vagrant/centos67/ # vagrant up Bringing machine 'node1' up with 'virtualbox' provider... Bringing machine 'node2' up with 'virtualbox' provider... ==> node1: Importing base box 'centos67'... ==> node1: Matching MAC address for NAT networking... ==> node1: Setting the name of the VM: centos67_node1_1462177540423_22245 ==> node1: Fixed port collision for 22 => 2222. Now on port 2202. ==> node1: Clearing any previously set network interfaces... ==> node1: Preparing network interfaces based on configuration... node1: Adapter 1: nat node1: Adapter 2: hostonly ==> node1: Forwarding ports... node1: 22 (guest) => 2202 (host) (adapter 1) ==> node1: Booting VM... ==> node1: Waiting for machine to boot. This may take a few minutes... node1: SSH address: 127.0.0.1:2202 node1: SSH username: vagrant node1: SSH auth method: private key node1: Warning: Remote connection disconnect. Retrying... node1: node1: Vagrant insecure key detected. Vagrant will automatically replace node1: this with a newly generated keypair for better security. node1: node1: Inserting generated public key within guest... node1: Removing insecure key from the guest if it's present... node1: Key inserted! Disconnecting and reconnecting using new SSH key... ==> node1: Machine booted and ready! ==> node1: Checking for guest additions in VM... node1: The guest additions on this VM do not match the installed version of node1: VirtualBox! In most cases this is fine, but in rare cases it can node1: prevent things such as shared folders from working properly. If you see node1: shared folder errors, please make sure the guest additions within the node1: virtual machine match the version of VirtualBox you have installed on node1: your host and reload your VM. node1: node1: Guest Additions Version: 4.3.30 node1: VirtualBox Version: 5.0 ==> node1: Configuring and enabling network interfaces... ==> node1: Mounting shared folders... node1: /vagrant => /root/vagrant/centos67 ==> node2: Importing base box 'centos67'... ==> node2: Matching MAC address for NAT networking... ==> node2: Setting the name of the VM: centos67_node2_1462177592736_21927 ==> node2: Fixed port collision for 22 => 2222. Now on port 2203. ==> node2: Clearing any previously set network interfaces... ==> node2: Preparing network interfaces based on configuration... node2: Adapter 1: nat node2: Adapter 2: hostonly ==> node2: Forwarding ports... node2: 22 (guest) => 2203 (host) (adapter 1) ==> node2: Booting VM... ==> node2: Waiting for machine to boot. This may take a few minutes... node2: SSH address: 127.0.0.1:2203 node2: SSH username: vagrant node2: SSH auth method: private key node2: Warning: Remote connection disconnect. Retrying... node2: node2: Vagrant insecure key detected. Vagrant will automatically replace node2: this with a newly generated keypair for better security. node2: node2: Inserting generated public key within guest... node2: Removing insecure key from the guest if it's present... node2: Key inserted! Disconnecting and reconnecting using new SSH key... ==> node2: Machine booted and ready! ==> node2: Checking for guest additions in VM... node2: The guest additions on this VM do not match the installed version of node2: VirtualBox! In most cases this is fine, but in rare cases it can node2: prevent things such as shared folders from working properly. If you see node2: shared folder errors, please make sure the guest additions within the node2: virtual machine match the version of VirtualBox you have installed on node2: your host and reload your VM. node2: node2: Guest Additions Version: 4.3.30 node2: VirtualBox Version: 5.0 ==> node2: Configuring and enabling network interfaces... ==> node2: Mounting shared folders... node2: /vagrant => /root/vagrant/centos67
2つのVMが起動しています。
ホストOSからPINGを打ってみる
# ping 192.168.33.10 PING 192.168.33.10 (192.168.33.10) 56(84) bytes of data. 64 bytes from 192.168.33.10: icmp_seq=1 ttl=64 time=8.46 ms 64 bytes from 192.168.33.10: icmp_seq=2 ttl=64 time=0.184 ms 64 bytes from 192.168.33.10: icmp_seq=3 ttl=64 time=0.158 ms 64 bytes from 192.168.33.10: icmp_seq=4 ttl=64 time=0.298 ms ^C --- 192.168.33.10 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3312ms rtt min/avg/max/mdev = 0.158/2.277/8.469/3.575 ms # ping 192.168.33.11 PING 192.168.33.11 (192.168.33.11) 56(84) bytes of data. 64 bytes from 192.168.33.11: icmp_seq=1 ttl=64 time=0.673 ms 64 bytes from 192.168.33.11: icmp_seq=2 ttl=64 time=0.197 ms 64 bytes from 192.168.33.11: icmp_seq=3 ttl=64 time=0.235 ms 64 bytes from 192.168.33.11: icmp_seq=4 ttl=64 time=0.231 ms ^C --- 192.168.33.11 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3981ms rtt min/avg/max/mdev = 0.197/0.334/0.673/0.196 ms
問題なさそうです。
SSHの設定
ホストOSからゲストOSにSSHログインする際にオプションを付けずにログインできるようにしておきます。
# cd /root/vagrant/centos67/ # vagrant ssh-config --host 192.168.33.10 node1 >> ~/.ssh/config # vagrant ssh-config --host 192.168.33.11 node2 >> ~/.ssh/config # cat ~/.ssh/config Host 192.168.33.10 HostName 127.0.0.1 User vagrant Port 2202 UserKnownHostsFile /dev/null StrictHostKeyChecking no PasswordAuthentication no IdentityFile "/root/vagrant/centos67/.vagrant/machines/node1/virtualbox/private_key" IdentitiesOnly yes LogLevel FATAL Host 192.168.33.11 HostName 127.0.0.1 User vagrant Port 2203 UserKnownHostsFile /dev/null StrictHostKeyChecking no PasswordAuthentication no IdentityFile "/root/vagrant/centos67/.vagrant/machines/node2/virtualbox/private_key" IdentitiesOnly yes LogLevel FATAL
ちょっと分かりずらいのですが、
# vagrant ssh --host [IPアドレス] [ノード名]
で各ノードのSSH情報をHostをIPアドレス名に変換した形で標準出力に出力し、
その出力結果をrootユーザの「.ssh/config」に書き込んでいます。
SSHログイン確認
# ssh 192.168.33.10 Last login: Mon May 2 09:50:35 2016 from 10.0.2.2 [vagrant@localhost ~]$ exit logout Connection to 127.0.0.1 closed. # ssh 192.168.33.11 Last login: Mon May 2 09:50:39 2016 from 10.0.2.2
問題なくSSHログインできます。
ゲストOS同士のPING確認
# ssh 192.168.33.10 Last login: Mon May 2 09:53:02 2016 from 10.0.2.2 [vagrant@localhost ~]$ ping 192.168.33.11 ※node1⇒node2へのPING PING 192.168.33.11 (192.168.33.11) 56(84) bytes of data. 64 bytes from 192.168.33.11: icmp_seq=1 ttl=64 time=0.411 ms 64 bytes from 192.168.33.11: icmp_seq=2 ttl=64 time=0.331 ms 64 bytes from 192.168.33.11: icmp_seq=3 ttl=64 time=0.371 ms 64 bytes from 192.168.33.11: icmp_seq=4 ttl=64 time=0.421 ms e64 bytes from 192.168.33.11: icmp_seq=5 ttl=64 time=0.378 ms ^C --- 192.168.33.11 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4458ms rtt min/avg/max/mdev = 0.331/0.382/0.421/0.036 ms [vagrant@localhost ~]$ exit logout Connection to 127.0.0.1 closed. # ssh 192.168.33.11 Last login: Mon May 2 09:53:07 2016 from 10.0.2.2 [vagrant@localhost ~]$ ping 192.168.33.10 ※node2⇒node1へのPING PING 192.168.33.10 (192.168.33.10) 56(84) bytes of data. 64 bytes from 192.168.33.10: icmp_seq=1 ttl=64 time=0.291 ms 64 bytes from 192.168.33.10: icmp_seq=2 ttl=64 time=0.316 ms 64 bytes from 192.168.33.10: icmp_seq=3 ttl=64 time=0.237 ms 64 bytes from 192.168.33.10: icmp_seq=4 ttl=64 time=0.313 ms ^C --- 192.168.33.10 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3246ms rtt min/avg/max/mdev = 0.237/0.289/0.316/0.033 ms
問題なさそうです。