最近、Vagrant+Chef+ServerSpecで遊んでます。というか、今年はこれらをどうやって弊社の事業へ生かしていくか、考えないといけないコトがほとんど確定していて、それもふまえてのお勉強です。
さて、まず、なにはともあれ chef や ServerSpec で遊ぶための箱庭が必要です。と言うことで、毎度慣れ親しんだ Debianをつかって、Vagrant用の BASE BOX を 1から作ります。
折角なので、先日、発表されたばかりの Debian(Wheezy)7.4を使ってみます。
今回作った boxファイルを公開してます。 => debian-7.4_c64.box
よろしかったら、お使いください。不都合あれば、ご連絡くださいませ。
1.準備するもの
次の環境が必要です。なお、我が家は OS X Mavericks (10.9.1) 上でやってます。適宜、読みかえられる人が以降の文章を読む権利が与えられます。
※いずれも2014/2/11現在
注意点
Vagrant 1.4.2では、package コマンドにbugがあるので、必ず1.4.3を使いましょう。
2.前提条件
ところで、Vagrant BASE BOX を作るための前提条件をご存じでしょうか。なんか、方言もイロイロあるみたいなのですが、そもそもの requirementについては、ちゃんとVagrantのDocumentにまとめられています。そりゃそうだ。
ここを抜粋すると、次を準備することが必須の(望ましい)ようです。
Vagrant BASE BOX の掟
Vagrant Boxesには、Linux BOXを作るときのサンプルが公開されてますが、まずはコレにならうとすると。
- 対応した
Provider
を使うこと。(VirtualBox/VMware)
カスタマイズできる。サンプルとしてAWSのProvider
が公開されてる - Package Managerが使えること
-
vagrant
ユーザでSSHで接続できること -
puppet``chef
が使えるようになってると(・∀・)イイネ!! - DISKは容量おおきいと困るから、DynamicにResizeできると(・∀・)イイネ!!
- メモリは 512MB でいい
- AUDIOやUSBなど使いもしないものは disableにしておくこと
- vagrant ユーザは、insecure keypairを使ってログインできること
- rootのパスワードは
vagrant
- 一番重要なのは、
vagrant
ユーザーがパスワードなしでsudo
できること
他にも、過去の制約なのか、方言のように言い伝えられているものもありますので、参考にしてみました。
3.Debian7.4のBASE BOXを作成しよう
さて、掟にならって、Debian(Wheezy)7.4 をインストールしてみましょう。VirtualBoxにCDマウントして適当に起動してください。
今回は次のように設定しています。
VirtualBox の HW設定
構成要素 | 設定値 |
---|---|
CPU | x2 |
Memory | 2,048MB |
HDD(SATA) | 32GB |
AUDIO | disable |
USB | disable |
Pointing Device | PS/2 Mouse |
Network Adapter1 | NAT |
※特記ないものはdefaultのままです。 |
Debian(Wheezy)7.4導入設定
構成要素 | 設定値 |
---|---|
Language | English |
Location | Japan |
Locales | United States-en_US.UTF8 |
Keymap | Japanese |
Hostname | vagrant-debian-wheezy |
Domain | vagrantup.com |
Partition | LVM |
Software(tasksel) | SSH SERVER(ONLY) |
Partition設定
mount先 | 容量 |
---|---|
/ | 348.1MB |
/home | 18.6GB |
/usr | 9.0GB |
/tmp | 398.5MB |
/var | 3.0GB |
/boot | 254.8MB |
swap | 2.7GB |
- GUIDED LVM のまま
4.Vagrant用の構成をしましょう
導入完了して、起動した後、VirtualBox用のAddOnとVagrantへ最適な構成を実施します。
思ったよりやること少ないです。
というか、多分最低限です。
Virtualbox Guest Additionsの導入
インストール後のおまじないからいきましょう。
sudo
入れてないので、まだ rootユーザでやっときます。
# aptitude update ; aptitude upgarade -y
# aptitude install sudo build-essential module-assistant
# m-a prepare
VirtualBox で "Devices > Install Guest Additions" やっておきます。
# mount /media/cdrom0
# bash /media/cdrom0/VBoxLinuxAdditions.run
※ 参考 : Install virtualbox guest additions on Debian 7 wheezy
X Window のモジュールは入れていないので、ここで X Window 向けのコンパイルエラーでますが、気にしません。気にしたら負けです。
vagrantユーザをパスワードなしでsudoさせる
/etc/sudoers
を修正します。
適当なところに次のものを入れておきましょう
vagrant ALL=(ALL) NOPASSWD: ALL
これで、vagrantユーザで sudo
できますね( ̄ー ̄)ニヤリ
さぁ、ここで再起動しておきましょう。
vagrant ユーザへ、vagrant で sshログインできるようにする
vagrant ユーザを使って、vagrant の標準の公開鍵を使って、sshログインできるように、insecure keypair
な秘密鍵を登録しておきます。これで、vagrant ssh
できるようになるわけですね! かっこいい!
vagrant@vagrant-debian-wheezy:~$ sudo aptitude install curl
vagrant@vagrant-debian-wheezy:~$ mkdir .ssh
vagrant@vagrant-debian-wheezy:~$ chmod 700 .ssh
vagrant@vagrant-debian-wheezy:~$ cd .ssh
vagrant@vagrant-debian-wheezy:~/.ssh$ curl -L -o authorized_keys http://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- 0:00:05 --:--:-- 0
100 409 100 409 0 0 70 0 0:00:05 0:00:05 --:--:-- 70
vagrant@vagrant-debian-wheezy:~/.ssh$ chmod 600 authorized_keys
boxの起動を速くする小手先技
ここで、ちょいとした小手先の技で、vagrant boxの起動を少し速くしてしまいましょう。
各設定ファイルに、次の設定を追加して入れてあげてください。
UseDNS NO
grub の設定変更後は、sudo update-grub
で反映しましょう。
GRUB_HIDDEN_TIMEOUT=0
chef-soloを導入しよう!
最後にchefを入れます。
我が家の場合、結局 knife solo prepare するので、なくてもいいんですけれども、「chef-solo
入ってます」って言うと、今時感!!
curl -L https://www.opscode.com/chef/install.sh | sudo bash
を実行するだけです。お手軽!
vagrant@vagrant-debian-wheezy:~$ curl -L https://www.opscode.com/chef/install.sh | sudo bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 13347 100 13347 0 0 1108 0 0:00:12 0:00:12 --:--:-- 7317
Downloading Chef for debian...
downloading https://www.opscode.com/chef/metadata?v=&prerelease=false&p=debian&pv=6&m=x86_64
to file /tmp/install.sh.6909/metadata.txt
trying wget...
url https://opscode-omnibus-packages.s3.amazonaws.com/debian/6/x86_64/chef_11.10.0-1.debian.6.0.5_amd64.deb
md5 7164567ac606dfbf620a75599b3aa6e8
sha256 ce58b724359d2569fd61d0a8a93f80273d037faab390dcaf3da93320ed209042
downloaded metadata file looks valid...
downloading https://opscode-omnibus-packages.s3.amazonaws.com/debian/6/x86_64/chef_11.10.0-1.debian.6.0.5_amd64.deb
to file /tmp/install.sh.6909/chef_11.10.0-1.debian.6.0.5_amd64.deb
trying wget...
Checksum compare with sha256sum succeeded.
Installing Chef
installing with dpkg...
Selecting previously unselected package chef.
(Reading database ... 35468 files and directories currently installed.)
Unpacking chef (from .../chef_11.10.0-1.debian.6.0.5_amd64.deb) ...
Setting up chef (11.10.0-1.debian.6.0.5) ...
Thank you for installing Chef!
※ chef の導入コードは、getchef.com を確認していただくのが安全です
4.Vagrant用の BASE BOX をパッケージングしよう
ここまできたら後すぐです。ちょいちょいとコマンド打ったら終わりです。
終わりすぎて寂しいです。
vagrant package --base <vms名>
なんとこれだけで、vagrant で使うための、box
ファイルができちゃいます。寂しいですね。
vms名
には、VirtualBoxで登録されている、virtualmachine の名前ですね。virtualbox を起動したときに表示されてるアレです。
よく分からない人はVBoxManage list vms
コマンドを打ってみれば分かります
なお我が家ではこんな感じでした
$ VBoxManage list vms
"Debian Wheezy" {37e03644-3540-4831-ad39-ab4c8114cec1}
"debian-7.4_c64" {5bb8d549-e721-4cde-a363-04c581f877b6}
"temp_default_1392080742549_89863" {8e410833-bd8c-4d7d-8685-8d6e666affe7}
なお、この時に作っていた vms
は "temp_default_1392080742549_89863" でした(実は途中でvagrant box化してた)
というわけでやってみます、こう
$ vagrant package --base temp_default_1392080742549_89863
[temp_default_1392080742549_89863] Clearing any previously set forwarded ports...
[temp_default_1392080742549_89863] Exporting VM...
[temp_default_1392080742549_89863] Compressing package to: /xxxxxx/package.box
できましたヽ(´ー`)ノ
後は、この package.box
を使って、vagrant へ登録するまでです。
なお、この box
は公開していますので、上の手順は一切合切省いて、下のコマンドを実行するだけで、この vagrant box
を追加することができますヽ(´ー`)ノステキ
次の3工程で起動させます。
- vagrant box へ登録
vagrant box add <vms名> [boxファイルURL]
- vagrant 初期化
vagrant init <vms名
- vagrant で対象の起動
vagrant up
$ vagrant box add debian-7.4-c64 https://dl.dropboxusercontent.com/u/8506456/debian-7.4_c64.box
Downloading box from URL: file:/Volumes/WORKs/Vagrant/debian-7.4_c64/package.box
Extracting box...te: 137M/s, Estimated time remaining: --:--:--)
Successfully added box 'debian-7.4_c64' with provider 'virtualbox'!
$ vagrant init debian-7.4_c64
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.
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'debian-7.4_c64'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[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
やっほいヽ(´ー`)ノ