IBM 現場SEのITな日々

IBM現場SEが日々駆使している技術、現場SEの経験・知見を綴るブログです

(志賀)超初心者向け:完全無償な検証環境を作る方法(VirtualBox+Vagrant)[CentOS導入編2]

VirtualBox+VagrantCentOSの仮想環境を実装する(実践編つづき)

 前回の続きで、作成したVagrantの環境で実際に仮想マシンを作成してみます。

4:ゲストOSをVagrant経由で起動・停止・削除してみる

以下のコマンドを実行します。

起動:vagrant up {仮想マシン名}

停止:vagrant halt {仮想マシン名}

削除:vagrant destroy {仮想マシン名}

仮想マシン名を省略すると、定義されているすべての仮想マシンが対象になります。たくさん定義していてうっかり全部起動してしまうとリソースが足りなくなって処理途中でハングしますのでご注意ください。

 

まずは起動です。初回は起動とともに仮想マシンを作成します。

PS C:\asitestenv> vagrant status
Current machine states:

client1                   not created (virtualbox)
client2                   not created (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`.
PS C:\asitestenv> vagrant up
Bringing machine 'client1' up with 'virtualbox' provider...
Bringing machine 'client2' up with 'virtualbox' provider...
==> client1: Importing base box 'CentOS6-9-Server'...
==> client1: Matching MAC address for NAT networking...
==> client1: Setting the name of the VM: asitestenv_client1_1501650989537_83989
==> client1: Fixed port collision for 22 => 2222. Now on port 2201.
==> client1: Clearing any previously set network interfaces...
==> client1: Preparing network interfaces based on configuration...
    client1: Adapter 1: nat
    client1: Adapter 2: hostonly

 

(中略)


==> client1: Setting hostname...
==> client1: Configuring and enabling network interfaces...
==> client2: Importing base box 'CentOS7-3-Server'...
==> client2: Matching MAC address for NAT networking...
==> client2: Setting the name of the VM: asitestenv_client2_1501651051208_54393
==> client2: Fixed port collision for 22 => 2222. Now on port 2202.
==> client2: Clearing any previously set network interfaces...
==> client2: Preparing network interfaces based on configuration...
    client2: Adapter 1: nat
    client2: Adapter 2: hostonly

 

(中略)


==> client2: Setting hostname...
==> client2: Configuring and enabling network interfaces...
PS C:\asitestenv> vagrant status
Current machine states:

client1                   running (virtualbox)
client2                   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`.
PS C:\asitestenv>

 状態がrunningになっていること確認できました。

 

sshで作成した仮想マシンに接続してみます。

Vagrantで自動作成したLinux仮想マシンは、vagrantユーザでssh鍵認証でログイン可能です。"vagrant ssh {仮想マシン名]"コマンドでも可能ですが、複数仮想マシンを起動する前提ですし、ここはあえてteratermで接続する方法を紹介しておきます。

鍵ファイルのパスを以下コマンドで確認します。

ssh設定確認:vagrant ssh-config

PS C:\asitestenv> vagrant ssh-config
Host client1
  HostName 127.0.0.1
  User vagrant
  Port 2201
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile C:/asitestenv/.vagrant/machines/client1/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

Host client2
  HostName 127.0.0.1
  User vagrant
  Port 2202
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile C:/asitestenv/.vagrant/machines/client2/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

PS C:\asitestenv>

IdentityFileエントリにある鍵ファイルを指定してsshログインします。

f:id:users-6h324:20170802142216p:plain

接続してOSバージョンやホスト名、NW設定等できていることを確認できました。

[vagrant@client1 ~]$ hostname
client1
[vagrant@client1 ~]$ cat /etc/redhat-release
CentOS release 6.9 (Final)
[vagrant@client1 ~]$ ifconfig -a
eth0      Link encap:Ethernet  HWaddr 52:54:00:2A:86:4C
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::5054:ff:fe2a:864c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:672 errors:0 dropped:0 overruns:0 frame:0
          TX packets:457 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:76066 (74.2 KiB)  TX bytes:63492 (62.0 KiB)

eth1      Link encap:Ethernet  HWaddr 08:00:27:AF:9D:0B
          inet addr:192.168.50.14  Bcast:192.168.50.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:feaf:9d0b/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:98 errors:0 dropped:0 overruns:0 frame:0
          TX packets:88 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:15692 (15.3 KiB)  TX bytes:14905 (14.5 KiB)

(以降略)

 

停止コマンドを実行します。

PS C:\asitestenv> vagrant halt
==> client2: Attempting graceful shutdown of VM...
==> client1: Attempting graceful shutdown of VM...
PS C:\asitestenv> vagrant status
Current machine states:

client1                   poweroff (virtualbox)
client2                   poweroff (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`.
PS C:\asitestenv>

ここでまた"vagrant up"を実行したら、上記の停止したclient1/client2が起動してきます。もしhalt前に変更を加えていた場合でも、その設定は残っています。

今回はそのまま削除コマンドを実行します。

PS C:\asitestenv> vagrant status
Current machine states:

client1                   poweroff (virtualbox)
client2                   poweroff (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`.
PS C:\asitestenv> vagrant destroy
    client2: Are you sure you want to destroy the 'client2' VM? [y/N] y
==> client2: Destroying VM and associated drives...
    client1: Are you sure you want to destroy the 'client1' VM? [y/N] y
==> client1: Destroying VM and associated drives...
PS C:\asitestenv> vagrant status
Current machine states:

client1                   not created (virtualbox)
client2                   not created (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`.
PS C:\asitestenv>

これで完全に削除されました。ここで再び”vagrant up"を実行すると、初回同様にBox定義からインポートしてVagrantfileの内容に従い仮想マシンを1から作成されます。

 

ここまでで環境の作成とテストが完了です。

 

最後に

ここまでやるとお気づきになると思いますが、恐らく初心者が仮想環境を作る上で一番のハードルになると思われるネットワーク周りの設定が「サーバのIPアドレスを指定するだけ」です。これを手動でやろうと思うと、VirtualBox側のネットワークアダプタを定義した後に、操作性の低いコンソール画面上でOSをインストール→ssh疎通のための設定をすることになります。(しかもたいていキーボードが英語になっていて地味にイライラを増幅する。)じっと画面遷移を見守っているといつの間にかssh疎通可能なマシンが立ち上がっているって、本当にすばらしいです。

ここまで持ち上げましたが、使い込んでいくとオープンソースなのでもちろんバグに遭遇したりもします。あくまでテスト環境として、お勧めです。