2年前くらいからVagrantを使っていて最近便利だな~と思うことが多いVagrantをまとめておく。

Vagrantってなに?

ざっくりいうと、VirtualBox, Hyper-V, Docker, VMwareなどの仮想マシンを管理するソフトウェアをコマンドラインから操作できるようにするOSSです。

Vagrantの便利なところ

  • 複数のVMを一度に立ち上げたりするときに便利
  • Vagrant Boxを使えばローカルで全く同じOSが使える(例えば、Amazon Linux)
  • 簡単なProvisioningができる

環境

  • Windows10
  • Vagrant 1.8.5
  • VirtualBox 5.0.26 r108824

インストール

vagrantをローカルで使う場合ProviderはVirtualBox, Hyper-V, Docker, VMwareなどがサポートされています。デフォルトのProviderはVirtualBoxです。

ここではVirtualBoxを使います。

vagrantインストール

vagrantダウンロード

Cygwinのインストール(windowsユーザのみ)

WindowsユーザはCygwinがあると楽。Git for windowsのコンソールを使うこともできるけどCygwin使っとけば問題ない。
cygwinのインストールはこちら。
Vagrantを使うときはrsync, opensshが必要なのでインストールしておく。vimも入っていないのでついでにインストールするとよい。

VagrantでVMを起動

VagrantでCentOS7のVMを3つ起動してみる。

Vagrantfileを作る

Cygwinから以下のコマンドを実行する。

適当なディレクトリを作ってVagrantfileを作ります。

$ mkdir node-3 && cd node-3

$ vagrant -v
Vagrant 1.8.5
$ vagrant init

Vagrantfileの編集

CentOS7のVMを3つ作るために設定を作ります。

起動時にyum updateとdevelopment toolsをインストールするようにしておきます。provisionに書いたコマンドは初回起動時にしか自動実行されないので、再度実行したいときは--provisionをつけること。

メモリは2GB割り当てておきます。

$ vim Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.provision "shell", inline: <<-SHELL
sudo yum update -y
sudo yum groupinstall "development tools" -y
SHELL
config.vm.define :node1 do |node|
node.vm.network :private_network, ip: "192.168.33.10"
node.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
end
end
config.vm.define :node2 do |node|
node.vm.network :private_network, ip: "192.168.33.11"
node.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
end
end
config.vm.define :node3 do |node|
node.vm.network :private_network, ip: "192.168.33.12"
node.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
end
end
end

host -> guestで通信したいポートがある場合はポートフォワードの設定が必要なので、以下のように設定すること。

node.vm.network :forwarded_port, id: "https", guest: 443, host: 8443

Vagrant Box

Vagrant BoxはCentOS公式に書かれているBoxを使っています。誰が作ったBoxなのか調べてから使ったほうがよいです。

Boxのダウンロードは以下からできる。

VMを起動

$ vagrant up

Authentication failureでVMの起動はできるけどVMに接続できない。

default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...

vagrant 1.8.5でバグがあるらしく、以下の部分を変更する必要がある。

C:\HashiCorp\Vagrant\embedded\gems\gems\vagrant-1.8.5\plugins\guests\linux\cap\public_key.rb
L57: + chmod 0600 ~/.ssh/authorized_keys

もう一度vagrant up

$ vagrant up

しばらく待つと起動が完了してコンソールが戻ってくる。

$ vagrant status
Current machine states:

node1 running (virtualbox)
node2 running (virtualbox)
node3 running (virtualbox)

3つのVMが起動していることが確認できる。virtualboxからも3つ起動していることが確認できる。

VMに接続

vagrant ssh [node名]でVMへ接続できる。sudoもできるようになっている。

$ vagrant ssh node1
[vagrant@localhost ~]$ sudo su -
[root@localhost ~]#

VM間通信

node1にvagrant sshした後、他のnodeにpingしてみる。

$ ip addr show eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:19:f3:14 brd ff:ff:ff:ff:ff:ff
inet 192.168.33.10/24 brd 192.168.33.255 scope global eth1
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe19:f314/64 scope link
valid_lft forever preferred_lft forever
$ ping -c 3 192.168.33.11
PING 192.168.33.11 (192.168.33.11) 56(84) bytes of data.
64 bytes from 192.168.33.11: icmp_seq=1 ttl=64 time=0.445 ms
64 bytes from 192.168.33.11: icmp_seq=2 ttl=64 time=0.437 ms
64 bytes from 192.168.33.11: icmp_seq=3 ttl=64 time=0.451 ms

--- 192.168.33.11 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.437/0.444/0.451/0.018 ms
$ ping -c 3 192.168.33.12
PING 192.168.33.12 (192.168.33.12) 56(84) bytes of data.
64 bytes from 192.168.33.12: icmp_seq=1 ttl=64 time=0.258 ms
64 bytes from 192.168.33.12: icmp_seq=2 ttl=64 time=0.421 ms
64 bytes from 192.168.33.12: icmp_seq=3 ttl=64 time=0.426 ms

--- 192.168.33.12 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.258/0.368/0.426/0.079 ms

Vagrantコマンド

$ vagrant
Usage: vagrant [options] <command> [<args>]

-v, --version Print the version and exit.
-h, --help Print this help.

Common commands:
box manages boxes: installation, removal, etc.
connect connect to a remotely shared Vagrant environment
destroy stops and deletes all traces of the vagrant machine
global-status outputs status Vagrant environments for this user
halt stops the vagrant machine
help shows the help for a subcommand
init initializes a new Vagrant environment by creating a Vagrantfile
login log in to HashiCorp's Atlas
package packages a running vagrant environment into a box
plugin manages plugins: install, uninstall, update, etc.
port displays information about guest port mappings
powershell connects to machine via powershell remoting
provision provisions the vagrant machine
push deploys code in this environment to a configured destination
rdp connects to machine via RDP
reload restarts vagrant machine, loads new Vagrantfile configuration
resume resume a suspended vagrant machine
share share your Vagrant environment with anyone in the world
snapshot manages snapshots: saving, restoring, etc.
ssh connects to machine via SSH
ssh-config outputs OpenSSH valid configuration to connect to the machine
status outputs status of the vagrant machine
suspend suspends the machine
up starts and provisions the vagrant environment
version prints current and latest Vagrant version

For help on any individual command run `vagrant COMMAND -h`

Additional subcommands are available, but are either more advanced
or not commonly used. To see all subcommands, run the command
`vagrant list-commands`.

おわり。

参考

  1. https://www.vagrantup.com/docs/
  2. https://github.com/mitchellh/vagrant