はじめに
n番煎じではありすが、Vagrant/VirtualBoxを利用して2つのローカル・ネットワークに所属するVMを2台まとめて作成する手順のメモです。
事前の準備
VirtualBoxのインストール
Vagrantが利用する仮想インフラのプロバイダーとしてVirtualBoxをインストールしておきます。ダウンロードはこちらから。
Vagrantのインストール
同様にVagrant自身もインストールしておきます。ダウンロードはこちらから
2台のVMを作成するVagrantの構成ファイルを用意する
下の例のような構成ファイルを作成して、"Vagrantfile"という名前で任意のディレクトリーに配置します。そのディレクトリー配下に、Vagrantの定義ファイルが作成されます。
この構成ファイルには、以下の処理を含んでいます。
- ChefのBentoプロジェクトが提供するCentOS 7.2のBoxイメージを利用する
- VM間での通信のために2つのHost onlyネットワークを構成する
- 1台目のVMには最大10GBのディスクを3本定義してアサインする
ネットワークの構成
サーバー名 | ネットワーク1 | ネットワーク2 | |
---|---|---|---|
VM #1 | iscsi | 192.168.80.101 | 192.168.90.101 |
VM #2 | db1 | 192.168.80.102 | 192.168.90.102 |
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "bento/centos-7.2"
# 1台目のVM定義
config.vm.define :iscsi do | iscsi |
iscsi.vm.hostname = "iscsi"
iscsi.vm.network "private_network", ip: "192.168.80.150"
iscsi.vm.network "private_network", ip: "192.168.90.150"
iscsi.vm.synced_folder ".", "/vagrant", disabled: true
iscsi.vm.provider "virtualbox" do | vb |
# Customize the amount of memory on the VM:
vb.memory = "1024"
# 最大10GBのDISKを3本定義し、iscsi VMにアタッチする
["1", "2", "3"].each do |disk_number|
disk_path = './iscsi.disk' + disk_number + '.vdi'
if not File.exist?(disk_path)
vb.customize ['createhd', '--filename', disk_path, '--size', 10 * 1024 ^ 2 ]
vb.customize ['storageattach', :id, '--storagectl', 'SATA Controller',
'--port', disk_number, '--device', 0, '--type', 'hdd', '--medium', disk_path]
end
end
end
end
# 2台目のVM定義
config.vm.define :db1 do | db1 |
db1.vm.hostname = "psdb1"
db1.vm.network "private_network", ip: "192.168.80.101"
db1.vm.network "private_network", ip: "192.168.90.101"
db1.vm.synced_folder ".", "/vagrant", disabled: true
db1.vm.provider "virtualbox" do | vb |
# Customize the amount of memory on the VM:
vb.memory = "4096"
end
end
end
VMの作成
vagrant up
コマンドで、Vagrantfileに定義した内容を元に自動的にプロビジョニングが開始されます。vagrantコマンドはコマンドプロンプトからでもPowerShellからでも実行可能です。なお、仮想ネットワークの作成を行うので、コマンドプロンプトやPowerShellは「管理者として実行」するようにしてください。
PS C:\VMs\vagrant\2VMS> vagrant up
Bringing machine 'iscsi' up with 'virtualbox' provider...
Bringing machine 'db1' up with 'virtualbox' provider...
==> iscsi: Importing base box 'bento/centos-7.2'...
==> iscsi: Matching MAC address for NAT networking...
==> iscsi: Checking if box 'bento/centos-7.2' is up to date...
==> iscsi: Setting the name of the VM: 2VMs_iscsi_1481257059460_92050
==> iscsi: Clearing any previously set network interfaces...
==> iscsi: Preparing network interfaces based on configuration...
iscsi: Adapter 1: nat
iscsi: Adapter 2: hostonly
iscsi: Adapter 3: hostonly
==> iscsi: Forwarding ports...
iscsi: 22 (guest) => 2222 (host) (adapter 1)
==> iscsi: Running 'pre-boot' VM customizations...
==> iscsi: Booting VM...
==> iscsi: Waiting for machine to boot. This may take a few minutes...
iscsi: SSH address: 127.0.0.1:2222
iscsi: SSH username: vagrant
iscsi: SSH auth method: private key
iscsi: Warning: Remote connection disconnect. Retrying...
iscsi: Warning: Remote connection disconnect. Retrying...
iscsi: Warning: Remote connection disconnect. Retrying...
iscsi:
iscsi: Vagrant insecure key detected. Vagrant will automatically replace
iscsi: this with a newly generated keypair for better security.
iscsi:
iscsi: Inserting generated public key within guest...
iscsi: Removing insecure key from the guest if it's present...
iscsi: Key inserted! Disconnecting and reconnecting using new SSH key...
==> iscsi: Machine booted and ready!
==> iscsi: Checking for guest additions in VM...
==> iscsi: Setting hostname...
==> iscsi: Configuring and enabling network interfaces...
==> db1: Importing base box 'bento/centos-7.2'...
==> db1: Matching MAC address for NAT networking...
==> db1: Checking if box 'bento/centos-7.2' is up to date...
==> db1: Setting the name of the VM: 2VMs_db1_1481257162632_74608
==> db1: Fixed port collision for 22 => 2222. Now on port 2200.
==> db1: Clearing any previously set network interfaces...
==> db1: Preparing network interfaces based on configuration...
db1: Adapter 1: nat
db1: Adapter 2: hostonly
db1: Adapter 3: hostonly
==> db1: Forwarding ports...
db1: 22 (guest) => 2200 (host) (adapter 1)
==> db1: Running 'pre-boot' VM customizations...
==> db1: Booting VM...
==> db1: Waiting for machine to boot. This may take a few minutes...
db1: SSH address: 127.0.0.1:2200
db1: SSH username: vagrant
db1: SSH auth method: private key
db1: Warning: Remote connection disconnect. Retrying...
db1: Warning: Remote connection disconnect. Retrying...
db1: Warning: Remote connection disconnect. Retrying...
db1:
db1: Vagrant insecure key detected. Vagrant will automatically replace
db1: this with a newly generated keypair for better security.
db1:
db1: Inserting generated public key within guest...
db1: Removing insecure key from the guest if it's present...
db1: Key inserted! Disconnecting and reconnecting using new SSH key...
==> db1: Machine booted and ready!
==> db1: Checking for guest additions in VM...
==> db1: Setting hostname...
==> db1: Configuring and enabling network interfaces...
vagrant up
コマンドが完了すると、VirtualBox マネージャー側にも登録されたVMがリストされます。(VirtualBoxマネージャーも「管理者で実行」する必要があります)
ここまでで、稼働するVMが2台立ち上がりました。
VMにログインするための鍵ファイルは、Vagrantfileを配置したパス以下に"private_key"という名前で格納されます。
PS C:\VMs\vagrant\2VMS> ls .vagrant\machines\iscsi\virtualbox
ディレクトリ: C:\VMs\vagrant\2VMS\.vagrant\machines\iscsi\virtualbox
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2016/12/09 13:18 40 action_provision
-a--- 2016/12/09 13:17 10 action_set_name
-a--- 2016/12/09 13:17 1 creator_uid
-a--- 2016/12/09 13:17 36 id
-a--- 2016/12/09 13:17 32 index_uuid
-a--- 2016/12/09 13:18 1702 private_key
-a--- 2016/12/09 13:18 2 synced_folders
Teratermならこの鍵ファイルをそのまま参照して利用できます。Puttyの場合はPuTTYgenなどを使ってppkファイルに変換してください。PuTTYgenについてはこのあたりが参考になるかと。
"C:\Program Files (x86)\teraterm\ttermpro.exe" 192.168.80.150:22 /auth=publickey /user=vagrant /keyfile=C:/vms/vagrant/2VMS/.vagrant/machines/iscsi/virtualbox/private_key