[Vagrant]基本的な使い方
Vagrantのコマンド
前回はVagrantのインストールまで行いました。
今回は実際にVagrantを使ってみましょう
基本的に下記のコマンドを覚えていれば、だいたい大丈夫です
vagrant init [box]
vagrantを使用する準備を行います。
具体的には、vagrantを使用するための設定ファイル「Vagrantfile」を作成します
Vagrantfilenには、使用するboxの指定や、仮想環境のメモリ・ネットワーク・CPU等々の情報が記述されています
boxはatlasで管理されているものであれば、そのままbox名を記述するといろいろやってくれます
vagrant up
仮想マシンを立ち上げます
vagrantfileがあるディレクトリで実行します
イメージファイルがまだダウンロードできていない場合は自動的にダウンロードしてくれます
ダウンロードが完了すると仮想マシンが立ち上がります
vagrant box list
管理されているVagrant Boxの一覧を表示します
vagrant status
仮想マシンの動作状況を確認します
vagrant halt
仮想マシンを停止します
vagrant ssh
仮想マシンにsshでログインします
rootパスワードは基本的に「vagrant」となっています
実際セットアップするまでの手順
実際には下記のような手順でコマンドを叩いて行くと、簡単に仮想環境が出来上がります。
centosの仮想環境を構築する例です
$ mkdir centos
$ cd centos/
$ vagrant init centos/7
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos/7' could not be found. Attempting to find and install...
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Loading metadata for box 'centos/7'
default: URL: https://atlas.hashicorp.com/centos/7
==> default: Adding box 'centos/7' (v1606.01) for provider: virtualbox
default: Downloading: https://atlas.hashicorp.com/centos/boxes/7/versions/1606.01/providers/virtualbox.box
==> default: Successfully added box 'centos/7' (v1606.01) for 'virtualbox'!
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' is up to date...
==> default: Setting the name of the VM: centos_default_1468892144144_15308
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection timeout. Retrying...
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if its present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
==> default: Rsyncing folder: /Users/s-makinaga/VirtualBox VMs/Vagrant/centos/ => /home/vagrant/sync
$ vagrant box list
centos/7 (virtualbox, 1606.01)
$ vagrant ssh
[vagrant@localhost ~]$
[vagrant@localhost ~]$ exit
ログアウト
Connection to 127.0.0.1 closed.
$ vagrant halt
==> default: Attempting graceful shutdown of VM...
$ vagrant status
Current machine states:
default poweroff (virtualbox)
The VM is powered off. To restart the VM, simply run `vagrant up`
Vagrantfileをいじる
vagrantfileには仮想マシンの設定やboxの指定、フォルダの同期設定なんかを記述します
主だった設定項目について説明
boxの指定
config.vm.box = “box名”
ネットワークの設定
config.vm.network “private_network”, ip: “192.168.10.100”
フォルダの同期設定
config.vm.synced_folder “ホストPC側のパス”, “Vagrant側のパス”
メモリサイズ
vb.memory = “1024”
CPUの割り当てコア数
vb.cpus = “2”
Vagrantfile変更後の反映
vagrantfile変更後は、仮想マシンを再起動すれば反映されます
vagrant reload ですね
それにしてもvagrantを使えば非常に簡単にテスト環境ができてしまいます
さらにchefなんかを組み合わせればかなりの業務効率化を図れます
次回はvagrant環境でwordpressの開発環境を構築してみます
