vagrant
vagrantとは
仮想マシンを動かすラッパーツール
仮想マシンは選択可能
- VirtualBox
- VMware
- Amazon EC2
用語
プロバイダ
- 仮想環境のこと、VirtualBoxなど
Vagrantfile
- 仮想マシンの構成を記述する
- Rubyベース
- 基本的に、本ファイルがあればどこでも同じ環境を再現できる
用語
プロビジョニング
- vagrant upをした後に実行される一連の処理のこと
- シェルスクリプト、Chef(chef-solo, chef-client)、Puppetなどが使えます
Boxファイル
- 仮想マシン起動の際にベースとなるテンプレートファイル
- ISOファイルではありません
- 仮想環境別に必要、通常はOSイメージから作成する
- Vagrant利用の上で最低限必要な設定(ユーザの作成、sshdの起動、プロビジョニング
ツールのインストール)のみを行っておく
vagrant up
1. 必要なツールをインストールする
https://www.vagrantup.com/
https://www.virtualbox.org/
2. vagrant box を取得する
http://www.vagrantbox.es/
vagrant box add [url]
3. 仮想マシンの初期化
vagrant init [box-name]
4. 仮想マシンの立ち上げ
vagrant up
vagrant file
vagrant init
Vagrant.configure(2) do |config|
config.vm.box = "base"
# config.vm.box_check_update = false
# Create a forwarded port mapping
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a private network
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM.
# config.vm.synced_folder "../data", "/vagrant_data"
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
# config.vm.provision "shell", inline: <<-SHELL
# sudo apt-get update
# sudo apt-get install -y apache2
# SHELL
end
vagrant file
network
Vagrant.configure(2) do |config|
config.vm.box = "base"
# config.vm.box_check_update = false
# Create a forwarded port mapping
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a private network
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM.
# config.vm.synced_folder "../data", "/vagrant_data"
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
# config.vm.provision "shell", inline: <<-SHELL
# sudo apt-get update
# sudo apt-get install -y apache2
# SHELL
end
vagrant file
provisioning
Vagrant.configure(2) do |config|
config.vm.box = "base"
# config.vm.box_check_update = false
# Create a forwarded port mapping
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a private network
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM.
# config.vm.synced_folder "../data", "/vagrant_data"
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
# config.vm.provision "shell", inline: <<-SHELL
# sudo apt-get update
# sudo apt-get install -y apache2
# SHELL
end
vagrant コマンド
vagrant box add [box-name] [box-url]
vagrant box list
Vagrant box remove [box-name]
vagrant up 立ち上げ
vagrant halt 停止、シャットダウン
vagrant suspend 中断
vagrant resume 復帰
vagrant destroy 仮想マシン削除(Boxの削除ではない)
vagrant コマンド
vagrant reload 設定変更(vagrant file)を反映する、ネットワークの構成変更等
vagarnt provision プロビジョンを実行する(vagrant upした後でも可能)
共有フォルダ
デフォルトで 以下は共有フォルダになっています
- host OS の vagrant fileを置いてあるフォルダ
- guest os の /vagrant
※virtualbox guest additions
共有フォルダ
config.vm.synced_folder “host_path”, “guest_path”, type: “nfs”
type
- virtualbox(Windows/Mac/Linux)
- NFS(Mac/Linux)
- rsync(Windows/Mac/Linux)
- SMB(Windows)
共有フォルダ
config.vm.synced_folder “host_path”, “guest_path”, type: “nfs”
type
- virtualbox(Windows/Mac/Linux) 遅い
- NFS(Mac/Linux) それなりに高速
- rsync(Windows/Mac/Linux) ホスト OS 側から仮想環境への一方向だけの同期
- SMB(Windows)
ss-vagrant
https://github.com/shirasagi/ss-vagrant
VirtualBox + CentOS + Shirasagi
- 自治体サンプル
- 企業サンプル
ss-vagrant
Vagrant.configure(2) do |config|
config.vm.box = "ss-vagrant-v1.2.0"
config.vm.box_url = https://github.com/shirasagi/ss-vagrant/releases/download/v1.2.0/ss-vagrant-virtualbox-
x86_64.box
config.vm.network :forwarded_port, guest: 3000, host: 3000
config.vm.network "private_network", ip: "192.168.33.10“
config.vm.provider :virtualbox do |vb|
# IPv6 と DNS でのネットワーク遅延対策で追記
vb.customize ["modifyvm", :id, "--natdnsproxy1", "off"]
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "off"]
end
end
windowsでの簡単な立ち上げ方(オフライン)
1. VirtualBoxをインストール
2. vagrantをインストール
3. sshクライアントをインストール
4. VagrantFile と ss-vagrant-virtualbox-x86_64.box を用意
5. フォルダー C:vagrant を作成します
6. VagrantFile と ss-vagrant-virtualbox-x86_64.box を作成したフォルダーにコピーします
7. コマンドプロンプトを立ち上げて以下のコマンドを実行します
> cd C:vagrant
> vagrant box add ss ss-vagrant-virtualbox-x86_64.box
> vagrant up
windowsでの簡単な立ち上げ方(オフライン)
8. ssh で仮想環境にログイン
9. シラサギを起動
$ cd $HOME/shirasagi
$ bundle exec rake unicorn:start
10. ブラウザから確認
自治体サンプル http://localhost:3000/
企業サンプル http://192.168.33.10:3000/
管理画面 http://localhost:3000/.mypage
Vagrant Box build
仮想環境上で vagrant package
packer を使う
- https://www.packer.io/downloads.html
- template.jsonを元にBoxを作成
packer build [template.json]
- centos用テンプレート: https://github.com/boxcutter/centos
Vagrant plugin
vagrant plugin install [plugin-name]
sahara
- サンドボックスモードを有効にするプラグイン
- ロールバック機能で実行前の状態に戻すことができます
vagrant-hostmanager
- HOST名の書き換え(host OS含む)

勉強会0614 vagrant

  • 1.
  • 2.
  • 3.
    用語 プロバイダ - 仮想環境のこと、VirtualBoxなど Vagrantfile - 仮想マシンの構成を記述する -Rubyベース - 基本的に、本ファイルがあればどこでも同じ環境を再現できる
  • 4.
    用語 プロビジョニング - vagrant upをした後に実行される一連の処理のこと -シェルスクリプト、Chef(chef-solo, chef-client)、Puppetなどが使えます Boxファイル - 仮想マシン起動の際にベースとなるテンプレートファイル - ISOファイルではありません - 仮想環境別に必要、通常はOSイメージから作成する - Vagrant利用の上で最低限必要な設定(ユーザの作成、sshdの起動、プロビジョニング ツールのインストール)のみを行っておく
  • 5.
    vagrant up 1. 必要なツールをインストールする https://www.vagrantup.com/ https://www.virtualbox.org/ 2.vagrant box を取得する http://www.vagrantbox.es/ vagrant box add [url] 3. 仮想マシンの初期化 vagrant init [box-name] 4. 仮想マシンの立ち上げ vagrant up
  • 6.
    vagrant file vagrant init Vagrant.configure(2)do |config| config.vm.box = "base" # config.vm.box_check_update = false # Create a forwarded port mapping # config.vm.network "forwarded_port", guest: 80, host: 8080 # Create a private network # config.vm.network "private_network", ip: "192.168.33.10" # Create a public network, which generally matched to bridged network. # config.vm.network "public_network" # Share an additional folder to the guest VM. # config.vm.synced_folder "../data", "/vagrant_data" # config.vm.provider "virtualbox" do |vb| # # Display the VirtualBox GUI when booting the machine # vb.gui = true # # Customize the amount of memory on the VM: # vb.memory = "1024" # end # config.vm.provision "shell", inline: <<-SHELL # sudo apt-get update # sudo apt-get install -y apache2 # SHELL end
  • 7.
    vagrant file network Vagrant.configure(2) do|config| config.vm.box = "base" # config.vm.box_check_update = false # Create a forwarded port mapping # config.vm.network "forwarded_port", guest: 80, host: 8080 # Create a private network # config.vm.network "private_network", ip: "192.168.33.10" # Create a public network, which generally matched to bridged network. # config.vm.network "public_network" # Share an additional folder to the guest VM. # config.vm.synced_folder "../data", "/vagrant_data" # config.vm.provider "virtualbox" do |vb| # # Display the VirtualBox GUI when booting the machine # vb.gui = true # # Customize the amount of memory on the VM: # vb.memory = "1024" # end # config.vm.provision "shell", inline: <<-SHELL # sudo apt-get update # sudo apt-get install -y apache2 # SHELL end
  • 8.
    vagrant file provisioning Vagrant.configure(2) do|config| config.vm.box = "base" # config.vm.box_check_update = false # Create a forwarded port mapping # config.vm.network "forwarded_port", guest: 80, host: 8080 # Create a private network # config.vm.network "private_network", ip: "192.168.33.10" # Create a public network, which generally matched to bridged network. # config.vm.network "public_network" # Share an additional folder to the guest VM. # config.vm.synced_folder "../data", "/vagrant_data" # config.vm.provider "virtualbox" do |vb| # # Display the VirtualBox GUI when booting the machine # vb.gui = true # # Customize the amount of memory on the VM: # vb.memory = "1024" # end # config.vm.provision "shell", inline: <<-SHELL # sudo apt-get update # sudo apt-get install -y apache2 # SHELL end
  • 9.
    vagrant コマンド vagrant boxadd [box-name] [box-url] vagrant box list Vagrant box remove [box-name] vagrant up 立ち上げ vagrant halt 停止、シャットダウン vagrant suspend 中断 vagrant resume 復帰 vagrant destroy 仮想マシン削除(Boxの削除ではない)
  • 10.
    vagrant コマンド vagrant reload設定変更(vagrant file)を反映する、ネットワークの構成変更等 vagarnt provision プロビジョンを実行する(vagrant upした後でも可能)
  • 11.
    共有フォルダ デフォルトで 以下は共有フォルダになっています - hostOS の vagrant fileを置いてあるフォルダ - guest os の /vagrant ※virtualbox guest additions
  • 12.
    共有フォルダ config.vm.synced_folder “host_path”, “guest_path”,type: “nfs” type - virtualbox(Windows/Mac/Linux) - NFS(Mac/Linux) - rsync(Windows/Mac/Linux) - SMB(Windows)
  • 13.
    共有フォルダ config.vm.synced_folder “host_path”, “guest_path”,type: “nfs” type - virtualbox(Windows/Mac/Linux) 遅い - NFS(Mac/Linux) それなりに高速 - rsync(Windows/Mac/Linux) ホスト OS 側から仮想環境への一方向だけの同期 - SMB(Windows)
  • 14.
    ss-vagrant https://github.com/shirasagi/ss-vagrant VirtualBox + CentOS+ Shirasagi - 自治体サンプル - 企業サンプル
  • 15.
    ss-vagrant Vagrant.configure(2) do |config| config.vm.box= "ss-vagrant-v1.2.0" config.vm.box_url = https://github.com/shirasagi/ss-vagrant/releases/download/v1.2.0/ss-vagrant-virtualbox- x86_64.box config.vm.network :forwarded_port, guest: 3000, host: 3000 config.vm.network "private_network", ip: "192.168.33.10“ config.vm.provider :virtualbox do |vb| # IPv6 と DNS でのネットワーク遅延対策で追記 vb.customize ["modifyvm", :id, "--natdnsproxy1", "off"] vb.customize ["modifyvm", :id, "--natdnshostresolver1", "off"] end end
  • 16.
    windowsでの簡単な立ち上げ方(オフライン) 1. VirtualBoxをインストール 2. vagrantをインストール 3.sshクライアントをインストール 4. VagrantFile と ss-vagrant-virtualbox-x86_64.box を用意 5. フォルダー C:vagrant を作成します 6. VagrantFile と ss-vagrant-virtualbox-x86_64.box を作成したフォルダーにコピーします 7. コマンドプロンプトを立ち上げて以下のコマンドを実行します > cd C:vagrant > vagrant box add ss ss-vagrant-virtualbox-x86_64.box > vagrant up
  • 17.
    windowsでの簡単な立ち上げ方(オフライン) 8. ssh で仮想環境にログイン 9.シラサギを起動 $ cd $HOME/shirasagi $ bundle exec rake unicorn:start 10. ブラウザから確認 自治体サンプル http://localhost:3000/ 企業サンプル http://192.168.33.10:3000/ 管理画面 http://localhost:3000/.mypage
  • 18.
    Vagrant Box build 仮想環境上でvagrant package packer を使う - https://www.packer.io/downloads.html - template.jsonを元にBoxを作成 packer build [template.json] - centos用テンプレート: https://github.com/boxcutter/centos
  • 19.
    Vagrant plugin vagrant plugininstall [plugin-name] sahara - サンドボックスモードを有効にするプラグイン - ロールバック機能で実行前の状態に戻すことができます vagrant-hostmanager - HOST名の書き換え(host OS含む)