LoginSignup
3
3

More than 5 years have passed since last update.

vagrant + chef(knife) で rails プロジェクトを構築する前編

Last updated at Posted at 2015-12-14

必須ツールのインストール

VirtualBox 5.0.10
vagrant 1.7.4
rbenv 0.4.0-148-g5b9e4f0
ruby 2.1.5
gem install bundler

各自の環境で異なる値の説明

下記は各自の環境で適宜変更する

chef-repo: chef のプロジェクト
guest_os: ゲストPC名
user_name: github のユーザ名+DBのユーザ名
192.168.1.1: ルータのIP
192.168.1.2: ホストOSのIP
192.168.1.100: ゲストOSのIP(ホストと同じネットワークセグメント)

仮想環境の構築

仮想環境初期化

# box 名を centos-5.10 とする
vagrant box add --name centos-5.10 http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-5.10_chef-provisionerless.box

# CentOS6.5の場合
vagrant box add --name opscode_centos-6.5 http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-6.5_chef-provisionerless.box

# CentOS7.0の場合
vagrant box add --name opscode_centos-7.0 http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-7.0_chef-provisionerless.box

mkdir -p vagrant/guest_os
cd vagrant/guest_os
vagrant init

Vagrantfile を下記の該当部分を適宜編集する

  # 先にダウンロードした box 名を指定する
  config.vm.box = "centos-5.10"
  # bridge 接続で、ホストOS外からアクセスできるようにする
  config.vm.network :public_network, ip: "192.168.1.100"

  # 複数のゲストを同時に立ち上げる時に port を任意で変える(設定しなくても可)
  config.vm.network "forwarded_port", id: "ssh", guest: 22, host: 10100

  # CPU/メモリを増量する
  config.vm.provider "virtualbox" do |vb|
    vb.name = "guest_os"
    vb.cpus = 2
    vb.memory = "4096"
  end  

仮想環境の起動と停止

vagrant up
vagrant halt
vagrant reload

vagrant プラグインインストール

よく使用する vagrant のプラグインをインストールする

vagrant plugin install sahara
vagrant plugin install vagrant-omnibus
vagrant plugin install vagrant-cachier
vagrant plugin install vagrant-vbguest

sahara

ゲストOSでの変更を、SQLトランザクションの様に、すべて元に戻したり(rollback)、変更を確定(commit)する事ができる。
sandbox 環境を on にすると、ロールバックできる状態になる

vagrant sandbox on
vagrant sandbox rollback
# 変更を確定する場合は、commit
vagrant sandbox commit

vagrant-vbguest

vagrant up 時に VirtualBox Guest Additionsのバージョンが古いエラーが出る場合、vagrant-vbguest をインストールして vagrant reload する

An error occurred during installation of VirtualBox Guest Additions 4.3.30. Some functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.

vagrant-cachier

  config.vm.box = "centos-5.10"
  # この辺りに追記する
  if Vagrant.has_plugin?("vagrant-cachier")
    config.cache.scope = :box
  end

ネットワークの設定

vagrant ssh
vagrant ssh-config --host guest_os >> ~/.ssh/config
ssh guest_os

macアドレスの衝突を解決

vagrant up 時に下記のようなエラーがでる場合は、
ゲストOS側で 70-persistent-net.rules を削除する

The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

ARPCHECK=no /sbin/ifup eth1 2> /dev/null

Stdout from the command:


Determining IP information for eth1... failed.


Stderr from the command:

sudo ln -s -f /dev/null /etc/udev/rules.d/70-persistent-net.rules

vagrant reload

selinix の無効化

下記を修正してOSを再起動する

SELINUX=disabled

ipv6 の無効化

ipv6 を無効化しないと knife solo 時に下記エラーがでる

curl: (6) Couldn't resolve host 'www.opscode.com'
CentOS5 の場合

以下はゲストOS側での操作
参考

echo "install ipv6 /bin/true" > /etc/modprobe.d/disable-ipv6.conf
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=vagrant-c5-x86_64
echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/sysctl.conf
reboot
CentOS6 の場合

以下はゲストOS側での操作
参考

/etc/sysctl.conf
# ipv6 disable
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
sudo /sbin/sysctl -p

ホスト側に chef 関係のインストール

chef-repo をホストOSに入れる

git clone https://github.com/user_name/chef-repo.git
cd chef-repo
bundle install --path vendor/bundle
#bundle exec berks install --path cookbooks
# berks 0.5 以上
bundle exec berks vendor cookbooks

knife solo でサーバ環境構築

bundle exec knife configure
bundle exec knife solo prepare guest_os nodes/guest_os.idcf.json
bundle exec knife solo cook guest_os nodes/guest_os.idcf.json
3
3
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
3
3