Vagrantで複数のVMを起動する

2018.04.30

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

コンニチハ、千葉です。

K8sの勉強環境として、インターネット接続なしでガチャガチャいじれる環境が欲しくなり、VirtualBox + Vagrantを利用しました。 で、複数VM起動できるの?ってちょっと調べたのでアウトプットしておきます。

やってみた

調べたところ、公式でMulti-Machineがサポートされていたのでやってみました。 Vagrantfileに複数のVMを指定するだけでいけそうです。

Vagrant.configure("2") do |config|
  config.vm.define "k8s_master" do |k8s_master|
    k8s_master.vm.box = "ubuntu/trusty64"
  end

  config.vm.define "k8s_node_1" do |k8s_node_1|
    k8s_node_1.vm.box = "ubuntu/trusty64"
  end
end

これだけです。起動してみます。

$ vagrant up
Bringing machine 'k8s_master' up with 'virtualbox' provider...
Bringing machine 'k8s_node_1' up with 'virtualbox' provider...
Bringing machine 'test' up with 'virtualbox' provider...
・・・省略

$ vagrant status
Current machine states:

k8s_master                running (virtualbox)
k8s_node_1                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`.

起動できてます!

sshで接続するにはsshの後ろにVMの名前を入れます。

$ vagrant ssh k8s_master
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-145-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Mon Apr 30 04:53:36 UTC 2018

  System load:  0.78              Processes:           82
  Usage of /:   3.6% of 39.34GB   Users logged in:     0
  Memory usage: 25%               IP address for eth0: 10.0.2.15
  Swap usage:   0%

  Graph this data and manage this system at:
    https://landscape.canonical.com/

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.

New release '16.04.4 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

接続できました!

最後に

Vagrantを使って複数VMを起動してみました。K8s用のローカル環境が用意できたので、ガチャガチャ作っては壊して試して見ます。

参考

https://www.vagrantup.com/docs/multi-machine/