StupidDog's blog

IT関連の手近な所で、疑問に思った事を調べた記録

「Ubuntu12.10にVagrant/PuppetでRailsの環境を作成する(その1)」|ただいまRubyの修行中

はじめに

フィルタ系プログラムばかり作っていては勿体ないので、Ruby on Railsにも手を出すことにします。
実行環境を作成するにあたり毎回手探りで調節するのは嫌なので、以前から気になっていたVagrant/Puppetを使用することにします。

Vagrantとは?

  • 仮想マシンの作成や環境構築、仮想マシンの破棄までを自動化するツール
  • 違う環境に移行可能な開発環境を簡単に構築・管理し、配布することができる開発環境作成ツール

詳しい説明はリンク先のサイトが分かりやすいので、自分が欲しいと思った利点だけまとめると

  • 誰かが作成したベースの環境を利用でき、ベースのダウンロードから自動化することができる。
  • 環境に適用する設定を、ファイルに定義し自動で設定できる。
  • ベースから作成した環境は簡単に破棄し、再作成できる。
  • 自分で作成した環境を配布用にまとめる事もできるので、現場固有の開発環境や、開発後の保守の為に環境を保存する事もできる。

Puppetとは?

Puppetを使ったLinuxシステムの設定自動管理

  • あらかじめ用意しておいた設定ファイルに基づいてサーバーのさまざまな設定を自動的に行うソフトウェア

Vagrantがハード(仮想マシン)環境の構築ツールとして、Puppetはソフト環境の構築ツールみたいな感じかなぁ。
Puppetは、Vagrantと合わせなくても単体で利用できる運用ツール。
同じ用途にChefなるものがあります。

Chefでサーバ管理を楽チンにしよう!(第1回)
を流し読みした結果から、今回はPuppetを選択しました。

  • 使用するまでにChefの方が構成要素が多い
  • Puppet の開発元である Puppet Labs が Puppet Labs Vagrant BoxesというサイトでVagrantで使用できるBoxファイルを公開している。

余計な物を入れたくないし、自動化して楽に構築をしたいのにツール自体を動かすための環境構築や維持が難しいのは嫌です。
まぁ、今回の選択理由は、Puppetの開発元がVagrantで使用できるPuppet入りのイメージを公開しているからです。

構築手順

  1. VirtualBoxのインストール
  2. Vagrantのダウンロードとインストール
  3. ベースとなる仮想環境の構築 (Ubuntu Server 13.10 LTS i386)
  4. 構築した環境の確認 (起動/接続/終了)

VirtualBoxのインストール

Download VirtualBox for Linux Hosts
Debian-based Linux distributionsに記載されている手順どおりに、apt-getによりインストールします。

「/etc/apt/sources.list」ファイルに以下の行を追加。(※ 一番最後に追加しました)

deb http://download.virtualbox.org/virtualbox/debian saucy contrib
deb http://download.virtualbox.org/virtualbox/debian raring contrib
deb http://download.virtualbox.org/virtualbox/debian quantal contrib
deb http://download.virtualbox.org/virtualbox/debian precise contrib
deb http://download.virtualbox.org/virtualbox/debian lucid contrib non-free
deb http://download.virtualbox.org/virtualbox/debian wheezy contrib
deb http://download.virtualbox.org/virtualbox/debian squeeze contrib non-free

署名確認のための公開鍵を設定。(※ wgetにより公開鍵ファイルを取得し設定)

$ wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -

良く分かってないけど、apt-get upgradeなどでカーネル更新後、VirtualBoxのホスト・カーネル・モジュールの対応をしてくれるのでdkms入れておいた方がいいよ。
とのことなので、dkms(Dynamic Kernel Modules Support)のインストールも行う。

sudo apt-get install dkms

VirtualBoxのインストール。(※ 現行最新の4.3系)

$ sudo apt-get update
$ sudo apt-get install virtualbox-4.3

Vagrantのダウンロードとインストール

Vertion1.0.xの頃はRubyGemでインストールできたけど、今はインストーラ/パッケージのみに統一されているらしい。
なので、パッケージをダウンロードする必要があります。

注意
以下のようにすると…「なーんだaptでインストールできるじゃん♪」ってなるけど罠です。
先にいれたVirtualBoxはパージされ、VirtualBox4.1.18とVagrant1.0.3-1がねじ込まれます。

$ sudo apt-get install vagrant

もし、これをしてしまったら、ねじ込まれた物をパージしてVirtualBoxのインストールからやり直しです。

パッケージは公式サイトのダウンロードページから環境に合わせて最新をダウンロードします。
(2014.1.29現在、Debian/Ubuntu 32-bit版は"vagrant_1.4.3_i686.deb"でした)

deb形式のパッケージなのでdpkgでインストールします。

$ sudo dpkg -i vagrant_1.4.3_i686.deb

ベースとなる仮想環境の構築 (Ubuntu Server 13.10 LTS i386)

Vagrantでは、OSのインストーラを使って仮想マシンにOSをインストールするのではなく、あらかじめOSがインストールされたディスクイメージを使用して仮想マシンを作成する。VagrantではディスクイメージやVirtualBoxの設定ファイル、メタデータなどを「box」(複数形は「boxes」)という形式のファイルにまとめて取り扱う

で、欲しい環境のBoxファイルを探すことになります。
今回は、Puppet Labs がBoxファイルを公開しているので利用させてもらいます。
Puppet Labs Vagrant Boxes

Current Boxs のページの一番下にある Ubuntu Server 13.10 i386 が目的のファイルです。
右端のplainリンクからアドレスをコピーしてコマンドへ貼り付けます。

下記のコマンドで設定ファイル「Vagrantfile」の雛形を作成します。

$ vagrant init ubuntu_server-13_10-i386 http://puppet-vagrant-boxes.puppetlabs.com/ubuntu-1310-i386-virtualbox-puppet.box
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.

コメントがいっぱいな設定ファイル(Vagratfile)が作成されますが、不要なコメントを省くと、これだけの内容です。

# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2" 

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu_server-13_10-i386"
  config.vm.box_url = "http://puppet-vagrant-boxes.puppetlabs.com/ubuntu-1310-i386-virtualbox-puppet.box"
end

そして、環境の構築と起動ですが、BoxファイルのダウンロードはVagrantが行ってくれます。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Box 'ubuntu_server-13_10-i386' was not found. Fetching box from specified URL for
the provider 'virtualbox'. Note that if the URL does not have
a box for this provider, you should interrupt Vagrant now and add
the box yourself. Otherwise Vagrant will attempt to download the
full box prior to discovering this error.
Downloading box from URL: http://puppet-vagrant-boxes.puppetlabs.com/ubuntu-1310-i386-virtualbox-puppet.box
Extracting box...te: 5678k/s, Estimated time remaining: --:--:--)
Successfully added box 'ubuntu_server-13_10-i386' with provider 'virtualbox'!
[default] Importing base box 'ubuntu_server-13_10-i386'...
[default] Matching MAC address for NAT networking...
[default] Clearing any previously set forwarded ports...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
[default] Machine booted and ready!
[default] Mounting shared folders...
[default] -- /vagrant
[default] VM already provisioned. Run `vagrant provision` or use `--provision` to force it

構築した環境の確認

仮想マシンの画面が表示されないので、良く分かりませんが「vagrant status」で状態を確認できます。

$ vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.


構築した環境にはSSHで接続できます。
ターミナルから「vagrant ssh」でログインできます。

$ vagrant ssh
Last login: Thu Jan 16 16:37:07 2014 from 10.0.2.2
vagrant@ubuntu1310-i386:~$ exit
logout
Connection to 127.0.0.1 closed.

127.0.0.1で接続してたのか~。


起動した仮想マシンを停止するには「vagrant halt」とします。

$ vagrant halt
[default] Attempting graceful shutdown of VM...
$ vagrant status
Current machine states:

default                   poweroff (virtualbox)

The VM is powered off. To restart the VM, simply run `vagrant up`

まとめ

今回の環境構築を自動化したいノДT)
とりあえずベースとなる環境が出来たので、次はRails環境の構築と、その構築をPuppetで自動化する。