Vagrant

公開
カテゴリ
コンピュータ
タグ
VirtualBoxVagrant
目次
  1. インストール
  2. 設定
  3. Vagrantプラグイン
  4. 使い方
  5. 初期化
  6. 設定
  7. 起動
  8. 状態確認
  9. SSHログイン情報確認
  10. SSHログイン
  11. 動作確認
  12. 再起動
  13. 停止
  14. 削除
  15. プロビジョニング
  16. Box化
  17. その他

Vagrant(ベイグラント)は仮想マシンの作成や環境構築の自動化ツールです。
VirtualBox等の仮想化ソフトの設定を自動で行います。

https://www.vagrantup.com/

インストール

Vagrantのサイトからダウンロード・インストールしてください。
※VirtualBoxのインストールが必要。

Chocolatey

choco install -y vagrant

設定

Vagrantプラグイン

vagrant-vbguest

VirtualBoxの共有フォルダー(vboxsf)が、マウントできない場合があるのでプラグインをインストールします。
※Guest Additions最適化

vagrant plugin install vagrant-vbguest

使い方

適当なディレクトリを作成します。

mkdir debian
cd debian

初期化

Boxを指定して初期化します。
BoxとはVagrant用のイメージファイルになります。
OSのインストール・設定が行われているOSのデータをBox化しています。

以下のサイトでVagrantのさまざまなBoxが検索できます。
https://app.vagrantup.com/boxes/search

PHP・MySQL・PostgreSQLなど開発環境がすでにインストールされているBoxもあります。
※ laravel/homestead が有名。

debian/buster64 というBoxを指定。

vagrant init debian/buster64

Vagrantfileが作成されます。

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "debian/buster64"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # 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
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

設定

vagrant initコマンドを使わなくとも手動でVagrantfileを作成しても大丈夫です。
必要な設定のみ記述します。

Vagrant.configure("2") do |config|
  config.vm.box = "debian/buster64"
  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.synced_folder "./data", "/vagrant", type:"virtualbox", create: true
end

config.vm.network "private_network"でプライベートネットワークのIPアドレス指定。

config.vm.synced_folderでホストOSとゲストOSの共有ファイルの設定を行います。
設定が上記の場合、ホストOSの./dataとゲストOSの/vagrantのディレクトリが同期されます。

typeで同期する種類が選べます。(VirtualBox・NFS・rsync・SMB)
デフォルトでは、rsyncが設定されますが、Windowsの場合はrsyncを導入しないと動きません。

起動

vagrant up

使用したことがないBoxの場合は、Boxのダウンロードから始まります。

状態確認

vagrant status

runningの場合起動中。

SSHログイン情報確認

vagrant ssh-config

SSHログイン

vagrant ssh

SSHログインその他

vagrant ssh-configで表示された情報でもログイン可能です。
以下のように、~/.ssh/configに書き込むことも可能。

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

※Vagrantを複数立ち上げる場合、ポートが、かぶる可能性があります。

Vagrantの設定private_networkを設定しているので下記からもアクセス可能です。

host: 192.168.33.10
port: 22

動作確認

vagrant ssh
> sudo apt update
> sudo apt install apache2
> exit

WEBサーバ起動確認
http://192.168.33.10/

再起動

vagrant reload

停止

vagrant halt

削除

vagrant destroy

※-fでforceオプション

プロビジョニング

Vagrantではプロビジョニング(設定の自動化)も行えます。

Vagrant.configure("2") do |config|
  config.vm.box = "debian/buster64"
  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.synced_folder "./data", "/vagrant", type:"virtualbox", create: true

  config.vm.provision "shell", inline: <<-SHELL
    apt update
    apt install -y apache2
  SHELL
end

config.vm.provision "shell" でインストール時にシェルコマンドを実行する。

仮想マシン削除・起動

vagrant destroy -f
vagrant up

WEBサーバ起動確認
http://192.168.33.10/

Box化

自分の作成したOSイメージを保存・再配布などが可能です。

vagrant halt
vagrant package

作成されたBoxを追加すると、Box名として使用できます。

vagrant box add mybox/debian package.box

Box一覧

vagrant box list

その他

ssh.insert_key

デフォルトでは、仮想マシン毎にPrivate Keyが生成されますが、下記設定を行うと共通のPrivate Keyを使うことができます。

  config.ssh.insert_key = false

※ Boxを作成・配布する場合は、設定したほうが良いです。

リソース設定

CPU・メモリなどの設定が可能です。

Vagrant.configure("2") do |config|
  config.vm.box = "debian/buster64"

  config.vm.provider :virtualbox do |v|
    v.memory = 2048
    v.cpus = 2
  end

  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.synced_folder "./data", "/vagrant", type:"virtualbox", create: true
end

Ansible

プロビジョニングにAnsibleを使用可能です。

Ansibleは構成管理ツールです。
Ansible 2.9 日本語ドキュメント

Ansibleの設定ファイルをprovisionディレクトリに設置し、初回起動時に実行してくれます。

Vagrant.configure("2") do |config|
  config.vm.box = "debian/buster64"

  config.vm.provider :virtualbox do |v|
    v.memory = 2048
    v.cpus = 2
    v.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root","1"]
  end

  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.synced_folder "./data", "/vagrant", type:"virtualbox", create: true

  config.vm.synced_folder "./provision/", "/tmp/provision", type:"virtualbox"
  config.vm.provision :ansible_local do |ansible|
    ansible.playbook = "/tmp/provision/site.yml"
    ansible.install = true
  end
end

シンボリックリンク

Windowsの場合、共有フォルダーにシンボリックリンクが張れません。
下記のように設定し、シェルを管理者権限で動かすことにより、シンボリックリンクが張れるようになります。

Vagrant.configure("2") do |config|
  config.vm.box = "debian/buster64"

  config.vm.provider :virtualbox do |v|
    v.memory = 2048
    v.cpus = 2
    v.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root","1"]
  end

  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.synced_folder "./data", "/vagrant", type:"virtualbox", create: true
end

記事番号 67