複数の仮想マシンの操作¶
Vagrantfile に複数の仮想マシンを定義できます。これにより、一度に複数の仮想マシンを作成するなどの操作が可能になります。仮想マシンごとの操作も可能です。
仮想マシンの定義¶
config.vm.defineを使用して、仮想マシンごとに定義します。config.vm.defineの後に定義した名前( websv, dbsv )が Vagrant 内の仮想マシンの識別名です。
config.vm.define :websv do |web|
仮想マシンごとの定義
end
Web サーバーとデータベースサーバーの 2 台の仮想マシンを定義した Vagrantfile です。
$scriptweb = <<-'SCRIPT'
echo hello, Web world
SCRIPT
$scriptdb = <<-'SCRIPT'
echo hello, db world
SCRIPT
Vagrant.configure("2") do |config|
config.vm.define :websv do |web|
web.vm.box = "centos/7"
web.vm.network "public_network", mac: "080027000001"
web.vm.hostname = "websv"
web.vm.provider "virtualbox" do |vb|
vb.gui = true
vb.name = "webserver"
vb.memory = "4096"
vb.cpus = 2
vb.customize [
"modifyvm", :id,
"--ioapic", "on",
"--graphicscontroller", "vmsvga"
]
end
web.vm.provision "shell", inline: $scriptweb
end
config.vm.define :dbsv do |db|
db.vm.box = "centos/7"
db.vm.network "public_network", mac: "080027000002"
db.vm.hostname = "dbsv"
db.vm.provider "virtualbox" do |vb|
vb.gui = true
vb.name = "dbserver"
vb.memory = "4096"
vb.cpus = 2
vb.customize [
"modifyvm", :id,
"--ioapic", "on",
"--graphicscontroller", "vmsvga"
]
end
db.vm.provision "shell", inline: $scriptdb
end
end
実行結果です。ログの各行の先頭に Vagrant 内の仮想マシンの識別名が表示されます。一部のコマンドを除いてvagrant upやvagrant haltなど仮想マシンに対するコマンドはすべての仮想マシンに適用されます。
PS C:\vagrant\multi> vagrant up
Bringing machine 'websv' up with 'virtualbox' provider...
Bringing machine 'dbsv' up with 'virtualbox' provider...
==> websv: Importing base box 'centos/7'...
==> websv: Matching MAC address for NAT networking...
==> websv: Checking if box 'centos/7' version '1905.1' is up to date...
==> websv: Setting the name of the VM: webserver
==> websv: Clearing any previously set network interfaces...
==> websv: Preparing network interfaces based on configuration...
websv: Adapter 1: nat
websv: Adapter 2: bridged
==> websv: Forwarding ports...
websv: 22 (guest) => 2222 (host) (adapter 1)
==> websv: Running 'pre-boot' VM customizations...
==> websv: Booting VM...
==> websv: Waiting for machine to boot. This may take a few minutes...
websv: SSH address: 127.0.0.1:2222
websv: SSH username: vagrant
websv: SSH auth method: private key
websv:
websv: Vagrant insecure key detected. Vagrant will automatically replace
websv: this with a newly generated keypair for better security.
websv:
websv: Inserting generated public key within guest...
websv: Removing insecure key from the guest if it's present...
websv: Key inserted! Disconnecting and reconnecting using new SSH key...
==> websv: Machine booted and ready!
==> websv: Checking for guest additions in VM...
websv: No guest additions were detected on the base box for this VM! Guest
websv: additions are required for forwarded ports, shared folders, host only
websv: networking, and more. If SSH fails on this machine, please install
websv: the guest additions and repackage the box to continue.
websv:
websv: This is not an error message; everything may continue to work properly,
websv: in which case you may ignore this message.
==> websv: Setting hostname...
==> websv: Configuring and enabling network interfaces...
==> websv: Rsyncing folder: /cygdrive/c/vagrant/multi/ => /vagrant
==> websv: Running provisioner: shell...
websv: Running: inline script
websv: hello, Web world
==> dbsv: Importing base box 'centos/7'...
==> dbsv: Matching MAC address for NAT networking...
==> dbsv: Checking if box 'centos/7' version '1905.1' is up to date...
==> dbsv: Setting the name of the VM: dbserver
==> dbsv: Fixed port collision for 22 => 2222. Now on port 2200.
==> dbsv: Clearing any previously set network interfaces...
==> dbsv: Preparing network interfaces based on configuration...
dbsv: Adapter 1: nat
dbsv: Adapter 2: bridged
==> dbsv: Forwarding ports...
dbsv: 22 (guest) => 2200 (host) (adapter 1)
==> dbsv: Running 'pre-boot' VM customizations...
==> dbsv: Booting VM...
==> dbsv: Waiting for machine to boot. This may take a few minutes...
dbsv: SSH address: 127.0.0.1:2200
dbsv: SSH username: vagrant
dbsv: SSH auth method: private key
dbsv:
dbsv: Vagrant insecure key detected. Vagrant will automatically replace
dbsv: this with a newly generated keypair for better security.
dbsv:
dbsv: Inserting generated public key within guest...
dbsv: Removing insecure key from the guest if it's present...
dbsv: Key inserted! Disconnecting and reconnecting using new SSH key...
==> dbsv: Machine booted and ready!
==> dbsv: Checking for guest additions in VM...
dbsv: No guest additions were detected on the base box for this VM! Guest
dbsv: additions are required for forwarded ports, shared folders, host only
dbsv: networking, and more. If SSH fails on this machine, please install
dbsv: the guest additions and repackage the box to continue.
dbsv:
dbsv: This is not an error message; everything may continue to work properly,
dbsv: in which case you may ignore this message.
==> dbsv: Setting hostname...
==> dbsv: Configuring and enabling network interfaces...
==> dbsv: Rsyncing folder: /cygdrive/c/vagrant/multi/ => /vagrant
==> dbsv: Running provisioner: shell...
dbsv: Running: inline script
dbsv: hello, db world
PS C:\vagrant\multi>
PS C:\vagrant\multi> vagrant halt
==> dbsv: Attempting graceful shutdown of VM...
==> websv: Attempting graceful shutdown of VM...
PS C:\vagrant\multi>
共通した定義の取り扱い¶
「仮想マシンの定義」で使用した Vagrantfile です。
$scriptweb = <<-'SCRIPT'
echo hello, Web world
SCRIPT
$scriptdb = <<-'SCRIPT'
echo hello, db world
SCRIPT
Vagrant.configure("2") do |config|
config.vm.define :websv do |web|
web.vm.box = "centos/7"
web.vm.network "public_network", mac: "080027000001"
web.vm.hostname = "websv"
web.vm.provider "virtualbox" do |vb|
vb.gui = true
vb.name = "webserver"
vb.memory = "4096"
vb.cpus = 2
vb.customize [
"modifyvm", :id,
"--ioapic", "on",
"--graphicscontroller", "vmsvga"
]
end
web.vm.provision "shell", inline: $scriptweb
end
config.vm.define :dbsv do |db|
db.vm.box = "centos/7"
db.vm.network "public_network", mac: "080027000002"
db.vm.hostname = "dbsv"
db.vm.provider "virtualbox" do |vb|
vb.gui = true
vb.name = "dbserver"
vb.memory = "4096"
vb.cpus = 2
vb.customize [
"modifyvm", :id,
"--ioapic", "on",
"--graphicscontroller", "vmsvga"
]
end
db.vm.provision "shell", inline: $scriptdb
end
end
いくつかの項目は同じ値を設定しています。このような共通項目をまとめて定義し、仮想マシンごとの定義はconfig.vm.defineで定義します。
Vagrant.configure("2") do |config|
共通の定義
config.vm.define :websv do |web|
仮想マシン固有の定義
end
end
共通の定義部分をまとめて定義した Vagrantfile です。追加として今回は共通部分にもconfig.vm.provisionを入れています。
$script = <<-'SCRIPT'
echo hello, Vagrant world
SCRIPT
$scriptweb = <<-'SCRIPT'
echo hello, Web world
SCRIPT
$scriptdb = <<-'SCRIPT'
echo hello, db world
SCRIPT
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.provider "virtualbox" do |vb|
vb.gui = true
vb.memory = "4096"
vb.cpus = 2
vb.customize [
"modifyvm", :id,
"--ioapic", "on",
"--graphicscontroller", "vmsvga"
]
end
config.vm.provision "shell", inline: $script
config.vm.define :websv do |web|
web.vm.network "public_network", mac: "080027000001"
web.vm.hostname = "websv"
web.vm.provider "virtualbox" do |vb|
vb.name = "webserver"
end
web.vm.provision "shell", inline: $scriptweb
end
config.vm.define :dbsv do |db|
db.vm.network "public_network", mac: "080027000002"
db.vm.hostname = "dbsv"
db.vm.provider "virtualbox" do |vb|
vb.name = "dbserver"
end
db.vm.provision "shell", inline: $scriptdb
end
end
実行結果です。共通部分、固有部分を組み合わせて処理していることがわかります。
PS C:\vagrant\multi> vagrant up
Bringing machine 'websv' up with 'virtualbox' provider...
Bringing machine 'dbsv' up with 'virtualbox' provider...
==> websv: Importing base box 'centos/7'...
==> websv: Matching MAC address for NAT networking...
==> websv: Checking if box 'centos/7' version '1905.1' is up to date...
==> websv: Setting the name of the VM: webserver
==> websv: Clearing any previously set network interfaces...
==> websv: Preparing network interfaces based on configuration...
websv: Adapter 1: nat
websv: Adapter 2: bridged
==> websv: Forwarding ports...
websv: 22 (guest) => 2222 (host) (adapter 1)
==> websv: Running 'pre-boot' VM customizations...
==> websv: Booting VM...
==> websv: Waiting for machine to boot. This may take a few minutes...
websv: SSH address: 127.0.0.1:2222
websv: SSH username: vagrant
websv: SSH auth method: private key
websv:
websv: Vagrant insecure key detected. Vagrant will automatically replace
websv: this with a newly generated keypair for better security.
websv:
websv: Inserting generated public key within guest...
websv: Removing insecure key from the guest if it's present...
websv: Key inserted! Disconnecting and reconnecting using new SSH key...
==> websv: Machine booted and ready!
==> websv: Checking for guest additions in VM...
websv: No guest additions were detected on the base box for this VM! Guest
websv: additions are required for forwarded ports, shared folders, host only
websv: networking, and more. If SSH fails on this machine, please install
websv: the guest additions and repackage the box to continue.
websv:
websv: This is not an error message; everything may continue to work properly,
websv: in which case you may ignore this message.
==> websv: Setting hostname...
==> websv: Configuring and enabling network interfaces...
==> websv: Rsyncing folder: /cygdrive/c/vagrant/multi/ => /vagrant
==> websv: Running provisioner: shell...
websv: Running: inline script
websv: hello, Vagrant world
==> websv: Running provisioner: shell...
websv: Running: inline script
websv: hello, Web world
==> dbsv: Importing base box 'centos/7'...
==> dbsv: Matching MAC address for NAT networking...
==> dbsv: Checking if box 'centos/7' version '1905.1' is up to date...
==> dbsv: Setting the name of the VM: dbserver
==> dbsv: Fixed port collision for 22 => 2222. Now on port 2200.
==> dbsv: Clearing any previously set network interfaces...
==> dbsv: Preparing network interfaces based on configuration...
dbsv: Adapter 1: nat
dbsv: Adapter 2: bridged
==> dbsv: Forwarding ports...
dbsv: 22 (guest) => 2200 (host) (adapter 1)
==> dbsv: Running 'pre-boot' VM customizations...
==> dbsv: Booting VM...
==> dbsv: Waiting for machine to boot. This may take a few minutes...
dbsv: SSH address: 127.0.0.1:2200
dbsv: SSH username: vagrant
dbsv: SSH auth method: private key
dbsv:
dbsv: Vagrant insecure key detected. Vagrant will automatically replace
dbsv: this with a newly generated keypair for better security.
dbsv:
dbsv: Inserting generated public key within guest...
dbsv: Removing insecure key from the guest if it's present...
dbsv: Key inserted! Disconnecting and reconnecting using new SSH key...
==> dbsv: Machine booted and ready!
==> dbsv: Checking for guest additions in VM...
dbsv: No guest additions were detected on the base box for this VM! Guest
dbsv: additions are required for forwarded ports, shared folders, host only
dbsv: networking, and more. If SSH fails on this machine, please install
dbsv: the guest additions and repackage the box to continue.
dbsv:
dbsv: This is not an error message; everything may continue to work properly,
dbsv: in which case you may ignore this message.
==> dbsv: Setting hostname...
==> dbsv: Configuring and enabling network interfaces...
==> dbsv: Rsyncing folder: /cygdrive/c/vagrant/multi/ => /vagrant
==> dbsv: Running provisioner: shell...
dbsv: Running: inline script
dbsv: hello, Vagrant world
==> dbsv: Running provisioner: shell...
dbsv: Running: inline script
dbsv: hello, db world
PS C:\vagrant\multi>
プロビジョニングの部分です。どちらの仮想マシンも共通部分のプロビジョニングの実行後、仮想マシン固有のプロビジョニングを実行しています。
==> websv: Running provisioner: shell...
websv: Running: inline script
websv: hello, Vagrant world
==> websv: Running provisioner: shell...
websv: Running: inline script
websv: hello, Web world
==> dbsv: Running provisioner: shell...
dbsv: Running: inline script
dbsv: hello, Vagrant world
==> dbsv: Running provisioner: shell...
dbsv: Running: inline script
dbsv: hello, db world
Vagrant コマンド¶
vagrant upやvagrant haltなどの Vagrant コマンドは、定義した複数の仮想マシンすべてに対し適用されます。任意の仮想マシンに適用するには Vagrant コマンドに続けて Vagrant 内の仮想マシン名を指定します。
注釈
複数の仮想マシンを定義した環境のvagrant sshコマンドは仮想マシンの指定が必須です。
仮想マシン名を指定した実行結果です。
PS C:\vagrant\multi> vagrant up dbsv
Bringing machine 'dbsv' up with 'virtualbox' provider...
==> dbsv: Importing base box 'centos/7'...
==> dbsv: Matching MAC address for NAT networking...
==> dbsv: Checking if box 'centos/7' version '1905.1' is up to date...
==> dbsv: Setting the name of the VM: dbserver
==> dbsv: Clearing any previously set network interfaces...
==> dbsv: Preparing network interfaces based on configuration...
dbsv: Adapter 1: nat
dbsv: Adapter 2: bridged
==> dbsv: Forwarding ports...
dbsv: 22 (guest) => 2222 (host) (adapter 1)
==> dbsv: Running 'pre-boot' VM customizations...
==> dbsv: Booting VM...
==> dbsv: Waiting for machine to boot. This may take a few minutes...
dbsv: SSH address: 127.0.0.1:2222
dbsv: SSH username: vagrant
dbsv: SSH auth method: private key
dbsv:
dbsv: Vagrant insecure key detected. Vagrant will automatically replace
dbsv: this with a newly generated keypair for better security.
dbsv:
dbsv: Inserting generated public key within guest...
dbsv: Removing insecure key from the guest if it's present...
dbsv: Key inserted! Disconnecting and reconnecting using new SSH key...
==> dbsv: Machine booted and ready!
==> dbsv: Checking for guest additions in VM...
dbsv: No guest additions were detected on the base box for this VM! Guest
dbsv: additions are required for forwarded ports, shared folders, host only
dbsv: networking, and more. If SSH fails on this machine, please install
dbsv: the guest additions and repackage the box to continue.
dbsv:
dbsv: This is not an error message; everything may continue to work properly,
dbsv: in which case you may ignore this message.
==> dbsv: Setting hostname...
==> dbsv: Configuring and enabling network interfaces...
==> dbsv: Rsyncing folder: /cygdrive/c/vagrant/multi/ => /vagrant
==> dbsv: Running provisioner: shell...
dbsv: Running: inline script
dbsv: hello, Vagrant world
==> dbsv: Running provisioner: shell...
dbsv: Running: inline script
dbsv: hello, db world
PS C:\vagrant\multi>
PS C:\vagrant\multi> vagrant up websv
Bringing machine 'websv' up with 'virtualbox' provider...
==> websv: Importing base box 'centos/7'...
==> websv: Matching MAC address for NAT networking...
==> websv: Checking if box 'centos/7' version '1905.1' is up to date...
==> websv: Setting the name of the VM: webserver
==> websv: Fixed port collision for 22 => 2222. Now on port 2200.
==> websv: Clearing any previously set network interfaces...
==> websv: Preparing network interfaces based on configuration...
websv: Adapter 1: nat
websv: Adapter 2: bridged
==> websv: Forwarding ports...
websv: 22 (guest) => 2200 (host) (adapter 1)
==> websv: Running 'pre-boot' VM customizations...
==> websv: Booting VM...
==> websv: Waiting for machine to boot. This may take a few minutes...
websv: SSH address: 127.0.0.1:2200
websv: SSH username: vagrant
websv: SSH auth method: private key
websv:
websv: Vagrant insecure key detected. Vagrant will automatically replace
websv: this with a newly generated keypair for better security.
websv:
websv: Inserting generated public key within guest...
websv: Removing insecure key from the guest if it's present...
websv: Key inserted! Disconnecting and reconnecting using new SSH key...
==> websv: Machine booted and ready!
==> websv: Checking for guest additions in VM...
websv: No guest additions were detected on the base box for this VM! Guest
websv: additions are required for forwarded ports, shared folders, host only
websv: networking, and more. If SSH fails on this machine, please install
websv: the guest additions and repackage the box to continue.
websv:
websv: This is not an error message; everything may continue to work properly,
websv: in which case you may ignore this message.
==> websv: Setting hostname...
==> websv: Configuring and enabling network interfaces...
==> websv: Rsyncing folder: /cygdrive/c/vagrant/multi/ => /vagrant
==> websv: Running provisioner: shell...
websv: Running: inline script
websv: hello, Vagrant world
==> websv: Running provisioner: shell...
websv: Running: inline script
websv: hello, Web world
PS C:\vagrant\multi>
PS C:\vagrant\multi> vagrant ssh websv
[vagrant@websv ~]$
[vagrant@websv ~]$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:8a:fe:e6 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
valid_lft 86378sec preferred_lft 86378sec
inet6 fe80::5054:ff:fe8a:fee6/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:00:00:01 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.246/24 brd 192.168.1.255 scope global noprefixroute dynamic eth1
valid_lft 14379sec preferred_lft 14379sec
inet6 fe80::a00:27ff:fe00:1/64 scope link
valid_lft forever preferred_lft forever
[vagrant@websv ~]$
[vagrant@websv ~]$ logout
Connection to 127.0.0.1 closed.
PS C:\vagrant\multi>
PS C:\vagrant\multi> vagrant ssh dbsv
[vagrant@dbsv ~]$
[vagrant@dbsv ~]$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:8a:fe:e6 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
valid_lft 86142sec preferred_lft 86142sec
inet6 fe80::5054:ff:fe8a:fee6/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:00:00:02 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.31/24 brd 192.168.1.255 scope global noprefixroute dynamic eth1
valid_lft 14144sec preferred_lft 14144sec
inet6 fe80::a00:27ff:fe00:2/64 scope link
valid_lft forever preferred_lft forever
[vagrant@dbsv ~]$
[vagrant@dbsv ~]$ logout
Connection to 127.0.0.1 closed.
PS C:\vagrant\multi>
PS C:\vagrant\multi> vagrant halt websv
==> websv: Attempting graceful shutdown of VM...
PS C:\vagrant\multi>
PS C:\vagrant\multi> vagrant status websv
Current machine states:
websv poweroff (virtualbox)
The VM is powered off. To restart the VM, simply run `vagrant up`
PS C:\vagrant\multi>
PS C:\vagrant\multi> vagrant halt dbsv
==> dbsv: Attempting graceful shutdown of VM...
PS C:\vagrant\multi>
PS C:\vagrant\multi> vagrant status dbsv
Current machine states:
dbsv poweroff (virtualbox)
The VM is powered off. To restart the VM, simply run `vagrant up`
PS C:\vagrant\multi>