Vagrantを導入してみた
金融系のインフラ運用などの枯れた製品・手法を好んでいて、
尚且つ外部ネットワークを遮断していて、社外のトレンドなどが入ってきにくい現場から、
運用も開発も自分達でガンガンやる今の現場に移ったら、全く知らないツールばかりで浦島太郎状態、
飛び交う固有名詞がチンプンカンプン、という状態になってしまいました・・・。
とは言えテンパることもなくマイペースに進んでいこうと試み中です。
まぁ正直世間に追いつける気はしないですが・・・(笑)。
と言う訳でまずは色々なところで話を聞くdockerから調べていこうかなと思い、
色々ググっていたところ、「Vagrant」というツールの記事を見てそちらの方に興味を引かれたので導入してみました。
Vagrantとは
Vagrant自体は色んなところで詳細を解説していたのですが、
自分は「仮想環境を簡単に作れる上に操作もしたり、配布できたりするツール」と端的に理解してます。
この"簡単"にというのがミソなわけで、Vagrantでは構成情報が記載されたファイルを基に
簡単なコマンドを実行することで自動でVMMに仮想環境を構築してくれるので、
通常実施していた様な仮想環境構築に必要な諸々の作業を省いて、非常に楽に意図する仮想環境の構築が可能です。
また、構築した環境をboxというパッケージ(という表現でいいのかな?)にして、
他の環境やメンバーに配布することで、作成した環境と同じ仮想環境が容易に作成できます。
さらにChefなど様々なツールのプラグインもある様なので、他のツールとも連携させれば
サーバー環境だけでなく、その上のS/Wなども含めたテスト環境自体の構築など
環境整備の工数削減を図っていけるところなどが一番の魅力なのかなと思ってます。
とは言え個人やチーム用の環境であれば、exp/impの方が分かりやすいし楽な気がしているのですが・・・。
環境
今回(2018/02)は以下構成の環境にVagrantをインストールして仮想環境を作成してみました。
- ローカル環境:Windows7 SP1
- VMM:VirtualBox 5.1.14
- Vagrant Version:2.0.2
環境構築
Vagrantのインストール
公式HP から使用しているローカルPCのOSに合わせたインストーラーをDL。
インストールウィザードが起動するので基本的にそのままでNEXTを選択していく。
ライセンスの同意にはチェックを入れ、インストール先について特に希望がなければそのまま先に進めていく。
先に進んでいくとInstallボタンのある画面になるのでInstallを選択してしばらく待つ。
インストールが完了すると画面が変わるのでFinishを押下してウィザードを終了。
ウィザードを閉じると設定反映のための再起動を求められるので、再起動を行う。
再起動によってインストールは完了。
コマンドプロンプトやcygwinで下記コマンドを実行して
Vagrantのバージョンが表示されれば問題なくインストールができたと判断できる。
iwate@MET-NO001 /cygdrive/c/Users/xA47
$ vagrant --version
Vagrant 2.0.2
Windows 7 環境の場合にやっておくこと
VagrantはPowerShellも内部的に使用しているみたいですが、
Vagrant 1.9.7から必要なPowerShellのバージョンが上がった模様。
Windows7を使用している場合、PowerShellのデフォルトのバージョンが2.0のため、
そのままVagrantでの構築を進めていくと下記の様にVagrant up
でハングしてしまう。
これは現在最新のVagrant 2.0.2でも同様なので、
それを回避するためにPowerShellのバージョンをアップさせる。
iwate@MET-NO001 /cygdrive/c/iwate/CentOS7-test
$ vagrant up --debug
INFO global: Vagrant version: 2.0.2
INFO global: Ruby version: 2.4.3
INFO global: RubyGems version: 2.6.14
INFO global: VAGRANT_EXECUTABLE="C:\\HashiCorp\\Vagrant\\embedded\\gems\\gems\\vagrant-2.0.2\\bin\\vagrant"
INFO global: VAGRANT_INSTALLER_EMBEDDED_DIR="C:\\HashiCorp\\Vagrant\\embedded"
INFO global: VAGRANT_INSTALLER_ENV="1"
INFO global: VAGRANT_INSTALLER_VERSION="2"
INFO global: VAGRANT_LOG="debug"
INFO global: resolv replacement has been enabled!
INFO global: Plugins:
DEBUG bundler: Current generated plugin dependency list: []
DEBUG bundler: Generating new builtin set instance.
DEBUG bundler: Generating new plugin set instance. Skip gems - []
DEBUG bundler: Activating solution set: []
DEBUG global: Loading core plugin: C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-2.0.2/plugins/commands/box/plugin.rb
INFO manager: Registered plugin: box command
<<中略>>
DEBUG loader: Loading from: root (cache)
DEBUG loader: Configuration loaded successfully, finalizing and returning
DEBUG push: finalizing
INFO subprocess: Starting process: ["C:\\Windows\\System32\\WindowsPowerShell\\v1.0/powershell.EXE", "-NoLogo", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command", "Write-Output $PSVersionTable.PSVersion.Major"]
INFO subprocess: Command not in installer, restoring original environment...
DEBUG subprocess: Selecting on IO
DEBUG subprocess: stdout: 2 ←★ここで止まってしまう
Powershellアップグレード手順
MS公式HP でDownloadを選択。
Win7AndW2K8R2-KB3191566-x64.zip にチェックを入れてNEXTを押下。
ダウンロードされたzipファイルを解凍して、中にあるインストーラーをWin7AndW2K8R2-KB3191566-x64.msuを実行。
基本的に表示されるポップアップやライセンス条項については[はい]や[同意します]を選択。
そのまま続けていけばインストールは完了。もし再起動を促されたら再起動を実施すること。
再起動後、コマンドプロンプトを起動してPowerShellのバージョンを確認。
下記コマンドを実行して PSVersion の欄が5.1になっていることを確認する。
C:\Windows\System32>powershell $PSVersionTable
Name Value
---- -----
PSVersion 5.1.14409.1012
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14409.1012
CLRVersion 4.0.30319.18063
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Vagrantを使ってみる
公式サイトのBoxes (VagrantのボックスがUPされている場所)でからCentOS7のBoxをDLして使用。
上記Boxesから目当てのBoxを探してその押下する。
今回は「centos/7」というBoxを選択。
該当のBoxのページに飛ぶので、
「How to use this box with Vagrant:」のスペースに記載のコマンドをコピーしておく。
VagrantfileとNEWのタブがあるが、それぞれで少し作業が変わるだけで特に大きな違いはない(ハズ)。
1.Vagrntfileを作成、編集して使用する場合
まずはVagrantで使用するフォルダを作成し、
そのフォルダに移動してからvagrant init
でVagrantfileを作成。
iwate@MET-NO001 /cygdrive/c/iwate/CentOS7-test
$ cd /cygdrive/c/iwate/cent7
iwate@MET-NO001 /cygdrive/c/iwate/cent7
$ vagrant init
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
実行フォルダ上にVagrantfileを作成されたことを確認し、テキストエディタで開く。
15行目の辺りにある「config.vm.box = "base"」をコメントアウトして、
BoxesのVagrantfileタブに記載のあるBox名を記載する。
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
# config.vm.box = "base" ★この行をコメントアウト
config.vm.box = "centos/7" ★この行をBoxesのVagrantfileタブから転記
vagrant up
を実行し仮想環境の構築・起動を実施。
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos/7' could not be found. Attempting to find and install...
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Loading metadata for box 'centos/7'
default: URL: https://vagrantcloud.com/centos/7
==> default: Adding box 'centos/7' (v1801.02) for provider: virtualbox
default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1801.02/providers/virtualbox.box
default:
==> default: Successfully added box 'centos/7' (v1801.02) for 'virtualbox'!
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' is up to date...
==> default: Setting the name of the VM: cent7_default_1517903326008_98878
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
==> default: Rsyncing folder: /cygdrive/c/iwate/cent7/ => /vagrant
ここまで来ると作成した仮想環境がVirtualBoxに登録され、起動した状態になっている。
2.BoxesからそのままVagrantfileを作成して使用する場合
1.のVagrantで使用するフォルダを作成し、そのフォルダに移動してから
vagrant init Box名
でVagrantfileを作成。
$ vagrant init centos/7
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
作成されたVgrantfileはそのまま使用できるので仮想環境を構築。
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' is up to date...
==> default: Setting the name of the VM: cent7_default_1517989232861_60483
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
==> default: Rsyncing folder: /cygdrive/c/iwate/cent7/ => /vagrant
作成した環境へのログイン
作成した仮想環境にはteratermなどからも接続はできるが、
vagrant ssh
コマンドを使用しての接続も可能。
$ vagrant ssh
[vagrant@localhost ~]$
[vagrant@localhost ~]$ ls
[vagrant@localhost ~]$ pwd
/home/vagrant
[vagrant@localhost ~]$
[vagrant@localhost ~]$ df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00 xfs 38G 617M 37G 2% /
devtmpfs devtmpfs 236M 0 236M 0% /dev
tmpfs tmpfs 245M 0 245M 0% /dev/shm
tmpfs tmpfs 245M 4.4M 240M 2% /run
tmpfs tmpfs 245M 0 245M 0% /sys/fs/cgroup
/dev/sda2 xfs 1014M 63M 952M 7% /boot
tmpfs tmpfs 49M 0 49M 0% /run/user/1000
[vagrant@localhost ~]$ exit
logout
Connection to 127.0.0.1 closed.
仮想環境の削除
vagrant halt
でvagrant経由で起動している仮想環境を停止させ、
vagrant destroy
コマンドで削除をする。
$ vagrant halt
==> default: Attempting graceful shutdown of VM...
$ vagrant destroy
default: Are you sure you want to destroy the 'default' VM? [y/N] y ★「y」を入力
==> default: Destroying VM and associated drives...
この後VirtualBoxのマネージャーなどを開くと作成した環境が削除されていることが確認できる。
ただ、この状況ではカレントフォルダに「.Vagrant」フォルダと「Vagrantfile」が残っているためか、
再度vagrant init
を実行しても仮想環境は作成されない。
vagrant box remove <Box名>
でBoxを削除してみてもOS上はファイルとフォルダが残っているので駄目な模様。
OSで削除してからだと作成できる・・・ここら辺の動作はイマイチ分からない。
$ vagrant box remove centos/7
Removing box 'centos/7' (v1801.02) with provider 'virtualbox'...
$ vagrant init centos/7
`Vagrantfile` already exists in this directory. Remove it before
running `vagrant init`.
まとめ
導入レベルではまだイマイチ動作が分からないし、exp/impとのメリデメも説明できない。
もう少し踏み込んで触ってみないとメリットを感じられないのかもしれない。