LoginSignup
30

More than 5 years have passed since last update.

Vagrant+Ansible+VirtualBoxでlocal・本番構築自動化(Vagrant+Virtualbox編)

Last updated at Posted at 2015-05-10

目的

  • 環境構築の自動化

  • 各サーバにおけるミドルウェアのバージョン・conf設定管理をを集約

  • 冪等性

最終的には1コマンドでローカル環境の構築から本番環境用ミドルウェアの管理、conf設定、mysqlレプリケーションの設定なども行います。


ansibleとに似たインフラストラクチャ自動化フレームワークでchef等もありますが、
個人的にchefより使いやすいと感じたので少し記載します。

前編では、下準備のためVagrant+virtualboxでのローカル環境用下準備について簡単に説明します。


・本稿で扱う事

Vagrant+virtualboxで複数のインスタンス(webサーバ,dbサーバ等)を含むローカル環境を用意

・Vagrant

開発環境を簡単に構築・管理し、配布することができる開発環境作成ツールです。

今回はwebサーバ、cacheサーバ,mysqlサーバ..
のように複数インスタンスをローカル環境でも簡単に構築でき、
他者への配布が容易であるため使用しています。

今回はwebサーバ1台、memcachedサーバ一台、mysqlサーバ2台を作成します。

・用意するもの

VirtualBox
https://www.virtualbox.org/wiki/Downloads
Vagrant
https://www.vagrantup.com

macの場合、下記のようにbrew caskでもOKです。

brew cask install virtualbox
brew cask install vagrant
・boxファイルの用意

雛形となるboxを用意
もちろんcentos以外も選択可能です。

vagrant box add centos6.5 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box

下記コマンドでcentOSが入ったことを確認

vagrant box list

下記表示になれば成功
centos6.5 (virtualbox, 0)

・複数インスタンスを用意

ここでwebサーバ1台、memcachedサーバ一台、mysqlサーバ2台用のディレクトリを作成します。
仮想サーバ1台の場合はweb等のディレクトリでも構いませんが今回は複数サーバーを構築するため
totalとします。

mkdir total
cd ./total

下記コマンドでVagrantfileを作成します。

vagrant init

最新バージョンの場合、デフォルトVagrantfileにも設定方法がコメントアウトされ記載されています。

・Vagrantfile設定

今回はVagrantfileで下記のように設定しました。
コメントアウト内に説明を記載しています。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.define :web01 do |web| # 上記do |config|のconfigと紐付いています。
     web.vm.box = "centos6.5" #作成したbox名を指定
     web.vm.network :private_network, ip:"192.168.20.10"#複数インスタンスを
                                                        #用意しているためprivateipを設定。
                                                        #publicも可能
     web.ssh.insert_key=false
     web.vm.provider "virtualbox" do |v| 
         v.customize ["modifyvm", :id, "--memory", 1024, "--cpus", "1", "--ioapic", "off"]
     end # インスタンスのメモリ、cpu数を設定しています。 do |v|とv.customizeのvが紐付いています。
  end    # cpuを複数に設定する場合は--ioapicをonにします。

  config.vm.define :dbm01 do |dbm|
     dbm.vm.box = "centos6.5"
     dbm.vm.network :private_network, ip:"192.168.30.10"
     dbm.ssh.insert_key=false
     dbm.vm.provider "virtualbox" do |v|
         v.customize ["modifyvm", :id, "--memory", 512, "--cpus", "1", "--ioapic", "off"]
     end
  end

  config.vm.define :dbs02 do |dbs|
     dbs.vm.box = "centos6.5"
     dbs.vm.network :private_network, ip:"192.168.30.11"
     dbs.ssh.insert_key=false
     dbs.vm.provider "virtualbox" do |v|
         v.customize ["modifyvm", :id, "--memory", 512, "--cpus", "1", "--ioapic", "off"]
     end
  end

  config.vm.define :mem01 do |mem|
     mem.vm.box = "centos6.5"
     mem.vm.network :private_network, ip:"192.168.40.10"
     mem.ssh.insert_key=false
     mem.vm.provider "virtualbox" do |v|
         v.customize ["modifyvm", :id, "--memory", 512, "--cpus", "1", "--ioapic", "off"]
     end
  end
end

下記コマンドで起動。
vagrant up
対象インスタンスを指定してssh接続
vagrant ssh web01

これにてローカル環境用のインスタンス用意は完了です。

Vagrant+virtualboxについては下記リンク先で頻出コマンド、設定のカスタムに空いて記載しています。
[Vagrant+Ansible+VirtualBoxでlocal・本番構築自動化] のおまけ

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
30