Vagrantを試して見る(MacOS Yosemite)
元々やろうと思っているのは、Pythonの開発環境のセットアップを 一から確認しようと思っていたのだけど、CentOSのVMを立てて…とかやるのが面倒で、 便利ツールないかな〜と。で、Vagrantがあるか!という経緯です。
1. Vagrantの導入、インストール
まずは、Vagrantのページから、ダウンロードします。 vagrant_1.6.5.dmgというファイルでした。
こちらをマウントして、「Vagrant.pkg」をダブルクリックしてインストールします。
インストール先は、アプリケーションフォルダとなり、/Applications/Vagrant です。
このディレクトリ内の、binの下に、vagrantコマンドがインストールされ、/usr/bin/vagrantにシンボリックリンクが貼られます。
2. VirtualBoxの導入、インストール
Vagrantのセットアップの前に、仮想環境としてフリーのVirtualBoxをインストールしておきます。
https://www.virtualbox.org/wiki/Downloads から、OS X hostsのバイナリファイルをダウンロードして インストールします。 ※VirtualBox-4.3.20-96996-OSX.dmgというファイルでした。
3. Vagrantを使ってみる!
Vagrantのページにある通り、早速、initとupして、使えるか確認してみた。
(Ubuntu 12.04 LTS 32-bitが使えるようになります)
まずは、vagrant initから。
bash-3.2$ vagrant init hashicorp/precise32 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.
実行したディレクトリに、Vagrantfileという設定ファイルが作成されます。
次に、vagrant upコマンドで仮想環境を作ります。
bash-3.2$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Box 'hashicorp/precise32' could not be found. Attempting to find and install... default: Box Provider: virtualbox default: Box Version: >= 0 ==> default: Loading metadata for box 'hashicorp/precise32' default: URL: https://vagrantcloud.com/hashicorp/precise32 ==> default: Adding box 'hashicorp/precise32' (v1.0.0) for provider: virtualbox default: Downloading: https://vagrantcloud.com/hashicorp/boxes/precise32/versions/1.0.0/providers/virtualbox.box ==> default: Successfully added box 'hashicorp/precise32' (v1.0.0) for 'virtualbox'! ==> default: Importing base box 'hashicorp/precise32'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'hashicorp/precise32' is up to date... ==> default: Setting the name of the VM: ninoue_default_1417246291827_68138 ==> 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: Machine booted and ready! ==> default: Checking for guest additions in VM... default: The guest additions on this VM do not match the installed version of default: VirtualBox! In most cases this is fine, but in rare cases it can default: prevent things such as shared folders from working properly. If you see default: shared folder errors, please make sure the guest additions within the default: virtual machine match the version of VirtualBox you have installed on default: your host and reload your VM. default: default: Guest Additions Version: 4.2.0 default: VirtualBox Version: 4.3 ==> default: Mounting shared folders... default: /vagrant => /Users/xxxxxx bash-3.2$
作成されたUbuntuにアクセスしてみる。
bash-3.2$ vagrant ssh Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686) * Documentation: https://help.ubuntu.com/ New release '14.04.1 LTS' available. Run 'do-release-upgrade' to upgrade to it. Welcome to your Vagrant-built virtual machine. Last login: Fri Sep 14 06:22:31 2012 from 10.0.2.2 vagrant@precise32:~$ vagrant@precise32:~$ pwd /home/vagrant
vagrant sshコマンドで、VirtualBoxに作成されたUbuntuにアクセスすることができました。
ネットワークの設定はNATの設定がされているようで、Ubuntuから、Internet上のサイトへPingすることができました。
vagrant@precise32:~$ ping openstack.org PING openstack.org (192.237.193.83) 56(84) bytes of data. 64 bytes from 192.237.193.83: icmp_req=1 ttl=63 time=160 ms 64 bytes from 192.237.193.83: icmp_req=2 ttl=63 time=162 ms 64 bytes from 192.237.193.83: icmp_req=3 ttl=63 time=160 ms 64 bytes from 192.237.193.83: icmp_req=4 ttl=63 time=160 ms 64 bytes from 192.237.193.83: icmp_req=5 ttl=63 time=161 ms ^C --- openstack.org ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4004ms rtt min/avg/max/mdev = 160.174/160.976/162.305/0.852 ms vagrant@precise32:~$
作成したUbuntuですが、削除するには、vagrant destroyコマンドを実行します。
bash-3.2$ vagrant destroy default: Are you sure you want to destroy the 'default' VM? [y/N] y ==> default: Forcing shutdown of VM... ==> default: Destroying VM and associated drives... bash-3.2$
壊していいか確認がありますので、[y]を入力すると、VirtualBox上からも作成されたUbuntuが削除されます。 一度、initして、Vagrantfileができていて、Boxファイルもダウンロード済みの状態ですので、 再度、vagrant upした時は、起動完了まで、あっという間に終わります。
bash-3.2$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'hashicorp/precise32'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'hashicorp/precise32' is up to date... ==> default: Setting the name of the VM: ninoue_default_1417247233651_39052 ==> 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: Machine booted and ready! ==> default: Checking for guest additions in VM... default: The guest additions on this VM do not match the installed version of default: VirtualBox! In most cases this is fine, but in rare cases it can default: prevent things such as shared folders from working properly. If you see default: shared folder errors, please make sure the guest additions within the default: virtual machine match the version of VirtualBox you have installed on default: your host and reload your VM. default: default: Guest Additions Version: 4.2.0 default: VirtualBox Version: 4.3 ==> default: Mounting shared folders... default: /vagrant => /Users/xxxxxx bash-3.2$
Vagrantの活用(boxの追加) CentOS 6.5のbox(仮想マシンのイメージ)を追加して、実行してみます。
https://vagrantcloud.com/chef/boxes/centos-6.5
bash-3.2$ vagrant box add chef/centos-6.5 https://vagrantcloud.com/chef/boxes/centos-6.5 ==> box: Loading metadata for box 'https://vagrantcloud.com/chef/boxes/centos-6.5' This box can work with multiple providers! The providers that it can work with are listed below. Please review the list and choose the provider you will be working with. 1) virtualbox 2) vmware_desktop Enter your choice: 1 ==> box: Adding box 'chef/centos-6.5' (v1.0.0) for provider: virtualbox box: Downloading: https://vagrantcloud.com/chef/boxes/centos-6.5/versions/1.0.0/providers/virtualbox.box ==> box: Successfully added box 'chef/centos-6.5' (v1.0.0) for 'virtualbox'! bash-3.2$
※addするときのBox名ですが、どうも決まっているみたいです。 centos65x8664としようとしたら、上書きできないとメッセージが出ました。
bash-3.2$ vagrant box add centos65_x86_64 https://vagrantcloud.com/chef/boxes/centos-6.5 ==> box: Loading metadata for box 'https://vagrantcloud.com/chef/boxes/centos-6.5' The box you're adding has a name different from the name you requested. For boxes with metadata, you cannot override the name. If you're adding a box using `vagrant box add`, don't specify the `--name` parameter. If the box is being added via a Vagrantfile, change the `config.vm.box` value to match the name below. Requested name: centos65_x86_64 Actual name: chef/centos-6.5 bash-3.2$
vagrant box listコマンドで、インストールされているboxが確認できます。
bash-3.2$ vagrant box list chef/centos-6.5 (virtualbox, 1.0.0) hashicorp/precise32 (virtualbox, 1.0.0) bash-3.2$
CentOS 6.5を利用してみます。
※その前に、Vagrantfileを削除する必要があります。
bash-3.2$ vagrant init chef/centos-6.5 `Vagrantfile` already exists in this directory. Remove it before running `vagrant init`. bash-3.2$ bash-3.2$ rm Vagrantfile bash-3.2$
次に、initです。
bash-3.2$ vagrant init chef/centos-6.5 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. bash-3.2$
CentOS6.5をupします。
bash-3.2$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'chef/centos-6.5'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'chef/centos-6.5' is up to date... ==> default: Setting the name of the VM: ninoue_default_1417250008430_63086 ==> 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: Warning: Remote connection disconnect. Retrying... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... ==> default: Mounting shared folders... default: /vagrant => /Users/xxxxxx bash-3.2$
sshして、ログインします。
bash-3.2$ vagrant ssh Last login: Fri Mar 7 16:57:20 2014 from 10.0.2.2 [vagrant@localhost ~]$ [vagrant@localhost ~]$ uname -a Linux localhost.localdomain 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux [vagrant@localhost ~]$ [vagrant@localhost ~]$ cat /etc/centos-release CentOS release 6.5 (Final) [vagrant@localhost ~]$
無事、CentOS 6.5にアクセスできました!