installing-your-first-vagrant-box

仮想マシン実行のための Vagrant Box のインストール

仮想マシン実行のための Vagrant Box のインストール

Vagrant という名前は聞いたことがあっても、使ったことはないという人は多いのではないでしょうか?一度使ってみるとその便利さがわかって、よく使うツールになると思います。

Vagrant は、ポータブルで再現性があり、簡単に導入できて自由に処分できる仮想環境を作成するのに使用されます。開発者はもちろん、DevOps でも活用できます。たとえば、テスト用のVMをすばやく立ち上げてスクリプトをテストしたいと思ったとき、Vagrant を使えばいくつかのコマンドでそれが実現できます。仮想マシンは、Virtualbox、VMware、Hyper-V、AWSなどで作成することができます。

このブログでは、無料でインストールできるパブリックのリポジトリボックスである Vagrant cloud にある Vagrant Box をインストールし、それに CentOS をインストールする方法を示します。ここでは、Vagrant Box を Mac にインストールする方法を説明しますが、ホストを Windows や Linux にすることも可能です。Vagrant Box は仮想マシンなので、ローカルプラットフォームが実際に何であるかは重要ではありません。このブログはごく初歩的な紹介が目的ですが、使いこなしていくともっと多くの様々なことができるのがわかるはずです。

Vagrant のインストール

Vagrant は、この公式サイトからインストールすることができます。筆者は、可能なら Mac で homebrew を使ってパッケージをインストールする方を好みます。もし、homebrew を使ったことがない場合は、是非一度試してみることをお勧めします。

 Dans-MacBook-Pro:CentOS7 dan$ brew install caskroom/cask/vagrant

==> brew cask install caskroom/cask/vagrant

==> Satisfying dependencies

==> Downloading https://releases.hashicorp.com/vagrant/2.0.1/vagrant_2.0.1_x86_64.dmg

######################################################################## 100.0%

==> Verifying checksum for Cask vagrant

==> Installing Cask vagrant

==> Running installer for vagrant; your password may be necessary.

==> Package installers may write to any location; options such as --appdir are ignored.

Password:

==> installer: Package name is Vagrant

==> installer: Upgrading at base path /

==> installer: The upgrade was successful.

🍺  vagrant was successfully installed!

ご覧のように、非常に簡単です!これであとは、Virtualbox をインストールするだけです。homebrew で可能です。

Dans-MacBook-Pro:~ dan$ brew install caskroom/cask/virtualbox

Updating Homebrew...

==> brew cask install caskroom/cask/virtualbox

==> Satisfying dependencies

==> Downloading http://download.virtualbox.org/virtualbox/5.2.0/VirtualBox-5.2.0

######################################################################## 100.0%

==> Verifying checksum for Cask virtualbox

==> Installing Cask virtualbox

==> Running installer for virtualbox; your password may be necessary.

==> Package installers may write to any location; options such as --appdir are i

==> installer: Package name is Oracle VM VirtualBox

==> installer: Installing at base path /

==> installer: The install was successful.

🍺  virtualbox was successfully installed!

この時点で、Vagrant と Virtualbox がインストールされ、いくつかのボックスをダウンロードしてインストールする準備が整いました。

CentOS Vagrant Box のインストール

ここでちょっとした Vagrant のトリックを使います。CentOS 仮想マシンをインストールしようとしています。Vagrant cloud で "CentOS" を検索すると、ここにボックスが見つかります。

簡単にするために、Vagrant Box を表示するための迅速な方法を示します。まず、Vagrant 設定データを格納するディレクトリを作成します。Vagrant コマンドで特定のボックスと対話するには、id を使用するか、このディレクトリにいる必要があります。ここでは、作成したディレクトリに移動します。

Dans-MacBook-Pro:~ dan$ mkdir CentOS

Dans-MacBook-Pro:~ dan$ cd CentOS/

vagrant init コマンドでボックスを初期化します。

vagrant init centos/7

このコマンドは作業ディレクトリに "Vagrantfile" と呼ばれるファイルを作成します。この Vagrantfile の中にはコメントがついていない3つのラインがあるだけですが、ネットワーキング、プロバイダ、メモリなど他の多くのオプションを設定することができます。

Vagrant.configure("2") do |config|

config.vm.box = "centos/7"

end 

これは、追加の設定をすることなく Vagrant のボックスを起動するための簡便な手法です。

次に、CentOS Vagrant Box をダウンロードしてインストールします。それには、vagrant up コマンド1つだけで事足ります。

Dans-MacBook-Pro:CentOS dan$ 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' (v1710.01) for provider: virtualbox

    default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1710.01/providers/virtualbox.box

    default: Progress: 20% (Rate: 8793k/s, Estimated time remaining: 0:00:30)/Us==> default: Successfully added box 'centos/7' (v1710.01) 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: CentOS_default_1509818704898_8630

==> 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: /Users/dan/CentOS/ => /vagrant

Dans-MacBook-Pro:CentOS dan$ /Users/dan/CentOS

ダウンロードとインストールに必要な時間は、通常、ボックスのサイズによって異なります。この例では、ログオンできるよう CentOS ボックスをセットアップするのにおよそ2分かかりました。vagrant up コマンドの出力結果を見てわかるように、ボックスが準備されるときに多くのことが進行します。VM 用に仮想ネットワークが設定され、SSH(ポート22)用のポート転送設定やSSHキー作成が行われます。このキーを使って仮想マシンにログオンできます。

作成した仮想マシンが稼働していることを確認するためには、vagrant status が使えます。

Dans-MacBook-Pro:CentOS dan$ vagrant status

Current machine states:

 

default                   running (virtualbox)

SSH経由で Vagrant Box に接続

ボックスのタイプによって、SSH、PowerShell、またはRDP経由で Vagrant Box に接続できます。ここでは Linux ボックスを実行しているので、SSHを使用します。SSH経由で Vagrant Box に接続するには、vagrant ssh コマンドを使用します。通常のSSHセッションと同じようにしてボックスにログインできます。秘密キーは "/.vagrant/machines/default/virtualbox" にありますので、キーベースの認証を行います。

Dans-MacBook-Pro:virtualbox dan$ vagrant ssh

Last login: Sat Nov  4 18:19:35 2017 from 10.0.2.2

[vagrant@localhost ~]$

これだけで、完全に機能する CentOS 仮想マシンを Virtualbox の MacBook 上で稼働させることができます。

Vagrant Box のシャットダウンと破棄

ボックスの使用が完了したら、ボックスを正常にシャットダウンする vagrant halt コマンドを実行します。

Dans-MacBook-Pro:CentOS dan$ vagrant halt
 

Comments
Comments are disabled in preview mode.
Loading animation