VagrantとVirtualBoxで仮想環境を作ってみる。ついでにchef-soloの導入まで。
タイトルの通り。
環境および構築
Windows7環境でやった。
VirtualBox
下記からVirtualBox binariesのVirtualBox platform packagesのVirtualBox 4.3.6 for Windows hostsを入手しインストール。
https://www.virtualbox.org/wiki/Downloads
Vagrant
下記からVagrantを入手してインストール。(バージョンは1.4.2)
http://www.vagrantup.com/downloads.html
他にもRubyGemsやbundlerでインストールする方法もあるみたい。
仮想環境を作ってみる
■手順概要
■手順詳細
- 作業ディレクトリの作成と移動
- mkdir C:\work
- cd C:\work
- 仮想マシンの追加、起動
- vagrant box add {name} {url}
- name : VM管理用の名前(好きなのでおk)
- url : テンプレートのURL→ここ「http://www.vagrantbox.es/」から好きなのを選ぶ
- 例)「> vagrant box add CentOS64 http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-i386-v20130731.box」
- エラーが出たら→後述の「プロキシの設定」
- 「C:\Users\<ユーザ名>\.vagrant.d\boxes」配下にboxが追加されている
- 「vagrant box list」でも確認できる
- mkdir {name}
- cd {name}
- vagrant init {name}
- vagrant up
- vagrant box add {name} {url}
- 仮想マシンに接続
- 仮想マシンの状態確認
- vagrant status
- 仮想マシンの停止
- vagrant halt
- 仮想マシンの削除
- vagrant destroy {name}
困ったときは
vagrant -hを叩くとサブコマンドの一覧と説明が表示される。
vagrant [SubCommand] -hを叩くとサブコマンドの説明が表示される。
例)
> vagrant up -h
プロキシの設定
下記のようなエラーが出たらプロキシを設定してみる。
>vagrant box add CentOS56 http://yum.mnxsolutions.com/vagrant/centos_56_32.box Downloading box from URL: http://yum.mnxsolutions.com/vagrant/centos_56_32.box Download failed. Will try another box URL if there is one.-) An error occurred while downloading the remote file. The error message, if any, is reproduced below. Please fix this error and try again. The requested URL returned error: 401 407
環境変数を以下のように設定する。
http_proxy=http://<username>:<password>@<proxy-host>:<proxy-port> https_proxy=http://<username>:<password>@<proxy-host>:<proxy-port>
ターミナルで接続する方法
例えばTeraTermとかで接続したいとき。
vagrant initした際に作成されたVagrantfileのconfig.vm.networkを下記のように設定。
(コメントアウトされてるのでそれを有効にすればよい。)
config.vm.network :private_network, ip: "192.168.33.10"
これでターミナルから「192.168.33.10」でsshできる。
ユーザとパスワードは「vagrant」。rootになるにはノンパスで「sudo -s」。
これは「ホストオンリーネットワーク」といって、ホストOSからしかゲストOSへアクセスできない。
VirtualBoxでいうと「ホストオンリーアダプター」を設定していることになる。
※「:hostonly」って書き方もあるが、古いらしい。
他にも以下のようなネットーワーク設定が可能。
■ホストマシンの8080版ポートへの接続を仮想マシンの80番ポートに転送する設定
config.vm.network :forwarded_port, guest: 80, host: 8080
仮想マシンにApacheとか乗っけてサーバ公開したいときとかはこの設定かな。
■デフォルトではTCPでの接続のみがフォワーディングされるが、UDPを転送可能にする設定
config.vm.network :forwarded_port, guest: 1024, host: 10240, protocol: 'udp'
■仮想マシン間を接続した仮想ローカルネットワークを構築する設定(内部ネットワーク)
config.vm.network :private_network, ip: "192.168.50.4", virtualbox__intnet: "network01"
上記は「network01」というローカルネットワークに接続させる設定。
■ブリッジっぽい設定
config.vm.network :public_network
これでローカルネットワークからゲストOSが見えるようになる。
VirtualBoxには全部で下記のネットワーク設定があるみたい。
-
- NAT
- NATネットワーク
- ブリッジアダプター
- 内部ネットワーク
- ホストオンリーアダプター
- 汎用ドライバー
Vagrantの設定との対応とか、必要になったら調べよっかな。。
エイリアスの設定
IPで接続するのはヤダ。名前付けたい。
> vagrant ssh-config --host [名前]
例)vagrant ssh-config --host hoge
なんかこれでやると「127.0.0.1」にエイリアスついただけで役にたたんかったwww
やり方あると思うけど、別にIP接続でいいからいーや。
ゲストOSへプロキシを設定する方法
今のままだとyumとかプロキシ環境ではゲストOSでyumとか叩いても407になる。
ChefとかPuppetとか使ってprovisionするんだったらこの設定はやっといてね。
「vagrant-proxyconfというプラグイン」というプラグインでゲストOSにプロキシを設定できる。
https://github.com/tmatilai/vagrant-proxyconf
# 中身はrubyで「apt_proxy」「chef_proxy」「env_proxy」「yum_proxy」を変更するようにprovisionしてるだけっぽい。
VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| # 省略〜 # 以下を追加 if Vagrant.has_plugin?("vagrant-proxyconf") config.proxy.http = "http://<username>:<password>@<proxy-host>:<proxy-port>" config.proxy.https = "https://<username>:<password>@<proxy-host>:<proxy-port>" config.proxy.no_proxy = "localhost,127.0.0.1" end end
sshで入ってから「yum upgrade」でも叩いてネットワーク繋がってることを確認する。
その他Vagrantfileの書き方はここを参照。
chef-solo, knife-soloの導入
■chef-solo
下記からchef-client-11.8.2-1.windows.msiを入手しインストール。(chef-soloも入る)
http://www.getchef.com/chef/install/
以下のパスを通す。
C:\opscode\chef\bin C:\opscode\chef\embedded\bin
knife configureする。これをしないとknifeコマンド使えない。
> knife configure WARNING: No knife configuration file found Where should I put the config file? [C:/Users/[ユーザ名]/.chef/knife.rb] Please enter the chef server URL: [https://[マシン名]:443] Please enter an existing username or clientname for the API: [ユーザ名] Please enter the validation clientname: [chef-validator] Please enter the location of the validation key: [/etc/chef-server/chef-validator.pem] Please enter the path to a chef repository (or leave blank): ***** You must place your client key in: C:/Users/[ユーザ名]/.chef/[ユーザ名].pem Before running commands with Knife! ***** You must place your validation key in: C:/etc/chef-server/chef-validator.pem Before generating instance data with Knife! ***** Configuration file written to C:/Users/[ユーザ名]/.chef/knife.rb
とりあえず全部えんたー。
■knife-solo
> gem install knife-solo
下記のコマンドを実行するとカレントディレクトリの「chef-repo」ディレクトリ配下にChefに必要な設定ファイル群のひな形が生成される。
> knife solo init chef-repo
Vagrantfileがあるディレクトリをカレントにして実行する(とよい?)。
Vagrant、VirtualBox、Chefの連携
vagrant-omnibus
仮想マシンに自動的にChef環境をインストールするVagrantプラグイン。
以下のコマンドでインストール。
vagrant plugin install vagrant-omnibus
インストール後、Vagrantfileに下記の設定を追加する。
# config.omnibus.chef_version = "<インストールするChefのバージョン番号>" config.omnibus.chef_version = "11.8.2" # 「config.omnibus.chef_version = :latest」とすると最新バージョンになる。
chef-soloを利用したプロビジョニングの設定
Vagrantfileに下記の設定を追加する。
config.vm.provision :chef_solo do |chef| chef.cookbooks_path = "chef-repo/cookbooks" chef.roles_path = "chef-repo/roles" chef.data_bags_path = "chef-repo/data_bags" # 他にもいろいろ設定できる end
ディレクトリやパス名はVagrantfileからの相対パス。
他にも以下のような設定がある。(全部じゃないよ)
項目名 | 説明 |
---|---|
chef.add_recipe | 実行するレシピ名 |
chef.cookbooks_path | クックブックが格納されているディレクトリ |
chef.data_bags_path | Data Bagが格納されているディレクトリ |
chef.encrypted_data_bag_secret_key_path | 暗号化Data Bagを利用する場合に使用する秘密鍵のパス名 |
chef.run_list | 実行するクックブックのリスト |
とりあえずApacheインストールしてみる。
Vagrantfileがあるディレクトリで下記を実行してCookbookのひな形を作成する
knife cookbook create httpd -o chef-repo/cookbooks
ファイルを以下のように編集する。
■chef-repo/cookbooks/httpd/recipes/default.rb
# apacheのインストール CentOSの場合 package "httpd" do action :install end # apacheの起動 CentOSの場合 service "httpd" do action [:start, :enable] end template "/var/www/html/index.html" do source "index.html.erb" mode 0644 end
上記はyumでhttpdがインストール・起動されて、/var/www/html/配下にindex.html.erbを元にしたindex.htmlが配置される設定。
次に下記のファイルを作成・編集する。
■chef-repo/cookbooks/httpd/default/index.html.erb
Chef httpd test.
「.erb」はRubyのテンプレートファイルの拡張子。
VagrantfileのChef設定部分を下記のように記述して作成したrecipeをVagrantに登録する。
config.vm.provision :chef_solo do |chef| chef.cookbooks_path = "chef-repo/cookbooks" chef.roles_path = "chef-repo/roles" chef.data_bags_path = "chef-repo/data_bags" # ↓追記した行 chef.run_list = ["httpd"] end
ここで、「vagrant up」している状態で「vagrant provision」するとApacheがインストール・起動される。
> vagrant provision DL is deprecated, please use Fiddle [default] Skipping Apt proxy config as the machine does not support it [default] Configuring proxy for Chef provisioners... [default] Configuring proxy environment variables... [default] Configuring proxy for Yum... [default] Chef 11.8.2 Omnibus package is already installed. [default] Running provisioner: chef_solo... Generating chef JSON and uploading... Running chef-solo... [2014-03-27T07:45:35+00:00] INFO: Forking chef instance to converge... [2014-03-27T07:45:35+00:00] INFO: *** Chef 11.8.2 *** [2014-03-27T07:45:35+00:00] INFO: Chef-client pid: 7554 [2014-03-27T07:45:38+00:00] INFO: Setting the run_list to ["recipe[httpd]"] from JSON [2014-03-27T07:45:38+00:00] INFO: Run List is [recipe[httpd]] [2014-03-27T07:45:38+00:00] INFO: Run List expands to [httpd] [2014-03-27T07:45:38+00:00] INFO: Starting Chef Run for localhost [2014-03-27T07:45:38+00:00] INFO: Running start handlers [2014-03-27T07:45:38+00:00] INFO: Start handlers complete. [2014-03-27T07:46:04+00:00] INFO: service[httpd] started [2014-03-27T07:46:06+00:00] INFO: service[httpd] enabled [2014-03-27T07:46:06+00:00] INFO: template[/var/www/html/index.html] created file /var/www/html/index.html [2014-03-27T07:46:06+00:00] INFO: template[/var/www/html/index.html] updated file contents /var/www/html/index.html [2014-03-27T07:46:06+00:00] INFO: template[/var/www/html/index.html] mode changed to 644 [2014-03-27T07:46:06+00:00] INFO: Chef Run complete in 28.502398838 seconds [2014-03-27T07:46:06+00:00] INFO: Running report handlers [2014-03-27T07:46:06+00:00] INFO: Report handlers complete [2014-03-27T07:45:35+00:00] INFO: Forking chef instance to converge...
ブラウザで「http://192.168.33.10/index.html」にアクセスして「Chef httpd test.」と表示されれば成功。
表示されない場合はiptablesとSELinuxではじかれてる可能性があるので下記を実行してからやってみる。
$ /etc/rc.d/init.d/iptables stop $ chkconfig iptables off $ chkconfig --list iptables iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off $ setenforce 0
recipeに下記のように書いておいてもいいかな。
service "iptables" do action [:stop, :disable] end
ここまでで紹介した内容はホストマシン(ローカルマシン)においたCookbookでゲストマシン(仮想マシン)をConfigurationする方法。
この方法の他に
- ゲストマシンからchef-solo
- ゲストマシンへCookbookを転送して、ゲストマシンにログインしてchef-soloを叩く
- ゲストマシンからchef
- 別途chef-serverを構築して、そこにおいたCookbookにゲストマシンからアクセスする方法
がある。
個人のローカルマシンで遊ぶ分にはVagrant経由でやるほうが楽かな。
Berkshelf
Cookbook作るのめんどいよね。
Berkshelfってのを使えば作成済みのCookbook取得できるっぽい。
自作のCookbookじゃなくてBerkshelfでApacheインストールしようと思うので一旦Apacheをアンインストール。
下記のファイルを更新してvagrant provision。
■chef-repo/cookbooks/httpd/recipes/default.rb
package "httpd" do action :remove end
各種インストール。
■Berkshelfのインストール
> gem install berkshelf
■Berkshelfプラグインのインストール
> vagrant plugin install vagrant-berkshelf
Vagrantfileと同じ位置にBerksfileを下記のように作成。
■Berksfile
site :opscode cookbook "apache2"
■Vagrantfileの更新
# 〜省略〜 # ↓追記した行 config.berkshelf.enabled = true config.vm.provision :chef_solo do |chef| # 〜省略〜 # ↓変更した行 chef.run_list = ["apache2"] end # 〜省略〜
berksコマンドを使用してBerksfileに定義したCookbookををopscodeから取得する。
準備されているCookbookは以下にある。
https://github.com/opscode-cookbooks/
http://community.opscode.com/cookbooks
berks --path=chef-repo/cookbooks
※念のため、カレントディレクトリをVagrantfileおよびBerksfileと同じ位置で実行してね。
って実行したけどプロキシ環境だったから怒られた。。。
本当だったら「chef-repo/cookbooks」配下に「apache2」ができてvagrant provisionすればおしまいのはず。
berksのプロキシ設定がわからなかったので別の方法で。
ブツ自体はGithubにあるみたいなのでgit cloneした。
gitについてはここを参照して。
git clone https://github.com/opscode-cookbooks/apache2 chef-repo/cookbooks/apache2
Vagrantfileの「config.berkshelf.enabled = true」行とBerksfileファイルを削除。
その後、vagrant provisionすればよい。
の予定だったが、やっぱりダメだった。
原因はCookbookが足りないこと。
今回はiptablesとlogrotateとpacmanのCookbookが足りなかったので下記コマンドで追加。
git clone https://github.com/opscode-cookbooks/apache2 chef-repo/cookbooks/apache2 git clone https://github.com/stevendanna/logrotate chef-repo/cookbooks/logrotate git clone https://github.com/jesseadams/pacman chef-repo/cookbooks/pacman
そして、Vagrantfileのrun_listにも上記追加。
chef.run_list = ["apache2", "iptables", "logrotate", "pacman"]
これでいけるか!?と思ったがpacmanのrecipeがバグってて動かず。。。
pacmanのrecipe全部コメントアウトしてやったけどやっぱりダメだった。(状況は変わったけど。。。
pacmanってなんだよ。
cookbookは自分で書いたほうがよいのかな。めんどいけど。
参考:http://vboxmania.net/
http://qiita.com/taiki45/items/b46a2f32248720ec2bae
http://qiita.com/Kentrow@github/items/b5a7dc8819a2e0e0f603
仮想環境構築ツール「Vagrant」で開発環境を仮想マシン上に自動作成す
http://knowledge.sakura.ad.jp/tech/1552/
ChefでCookbookを作成するときのちょっとしたコツ 9選
http://heartbeats.jp/hbblog/2013/01/chef-cookbook-tips.html