LoginSignup
18
19

More than 5 years have passed since last update.

Vargrantのセットアップと使い方

Last updated at Posted at 2014-06-07

chefを始めたくてローカル検証環境の整備にvagrantを使った時のメモ
環境はMacbook Air (Mavericks)

logo_vagrant.png

Vagrantとは・・・

VirtualBox,VMplayer,EC2(AWS)等でGUIをポチポチして作っていた仮想マシンを、共通の設定ファイル(Vagrantfile)を元に生成できるプロビジョニングツールらしい。

VirtualBoxのインストール

vbox_logo2_gradient.png
公式サイトからダウンロードしてインストールする。
全部デフォルトでいいと思う。

Vagrantのインストール

公式サイトからパッケージファイルをダウンロードしてインストールする。
これも全部デフォルト。。

インストールしたらバージョンを確認する。

$ vagrant -v

Vagrant 1.6.3

Vagrant Pluginのインストール

毎回vmにchefをインストールするのは面倒なので vagrant-omnibus をインストールしておく。
vagrant-omnibusvagrant up 時にvmにchefが入っているか確認してインストールしてくれるプラグイン。

※使用する際はVagrantfileへの設定記述が必要

$ vagrant plugin install vagrant-omnibus

インストール後、入っているpluginを確認する。

$ vagrant plugin list

vagrant-login (1.0.1, system)
vagrant-omnibus (1.4.1)
vagrant-share (1.1.0, system)

後々、使いそうなpluginも入れておく。

$ vagrant plugin install vagrant-aws
$ vagrant plugin install vagrant-configspec

自分の場合はunfプラグインが無いと怒られたので下記を実行した。

$ gem install fog
$ gem install unf
$ vagrant plugin install unf

Boxイメージの作成

Boxイメージとはvm構築する際のOSイメージ。
今回はChef社(旧opscode社)が公開しているboxイメージを利用する。
https://github.com/opscode/bento

centos-6.5 と名前を付けて保存する。

$ vagrant box add centos-6.5 http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-6.5_chef-provisionerless.box

Vagrantfileの作成

Vagrantfile とはvmを構築する際の情報(CPU,Memory,IPなど)が記載されたファイルで、この情報を元にvmが作成される。

まず、作業用ディレクトリを作成する。

$ mkdir ~/vagrant

Vagrantfileを初期化する。
名前は centos-6.5 としておく。

$ cd ~/vagrant
$ vagrant init centos-6.5

vmを起動する。

vagrant upで起動できる。

$ vagrant up

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos6' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Adding box 'centos6' (v0) for provider: virtualbox
    default: Downloading: http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-6.5_chef-provisionerless.box
==> default: Successfully added box 'centos6' (v0) for 'virtualbox'!
==> default: Importing base box 'centos6'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant-virtualbox_default_1402029256683_3544
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (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: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/kazuki/vagrant-virtualbox

vmに接続する

起動した仮想マシンにssh接続する。

$ vagrant ssh

Last login: Fri Mar  7 16:57:20 2014 from 10.0.2.2
[vagrant@localhost ~]$

また、vagrant ssh-configでssh接続する際の情報が見れるので~/.sss/configに記述しておく。

$ vagrant ssh-config --host test >> ~/.ssh/config

これでssh testでつなぐことが出来る。

vmを削除する

動いている仮想マシンを削除する。

$ cd ~/vagrant
$ vagrant destroy

    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...

また、再度vagrant upするとまっさらな状態のvmが立ち上がる。

Vagrantコマンド

コマンド 説明
vagrant init vagrant 初期化(Vagrantfileの作成)
vagrant up vagrant 起動
vagrant halt vagrant 終了
vagrant reload vagrant リロード(Vagrantfileの再読み込み)
vagrant destroy vagrant 削除
vagrant ssh vagrant sshログイン
18
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
19