Vagrant使ってみた(MacにVagrant+VirtualBox(CentOS7)をインストール)

今さらだけど
Vagrant 使ってみた
WEBエンジニアみんな使ってるので使ってみた





vagrantのインストール
  1. ダウンロード
  2. まずVagrantから。ここ↓からダウンロード
    https://www.vagrantup.com/downloads.html
    ver 1.9.7 (2017/07/08 時点の最新)
    
  3. インストール
  4. vagrant_1.9.7_x86_64.dmg をダウンロード後、ダブルクリック(マウントする)
    vagrant.pkg をダブクリック(インストーラを起動)
    あとは指示に従ってインストールするだけ。
    
  5. バージョン確認
  6. $ vagrant --version
    Vagrant 1.9.7


    動いてますね(`・ω・´)ゞ





VirtualBoxのインストール

  1. ダウンロード
  2. 次にVirtualBox。ここ↓からダウンロード
    https://www.virtualbox.org/wiki/Downloads
    VirtualBox 5.1.22 (2017/07/08 時点の最新)
    [OS X hosts]をクリックしてダウンロード
    
  3. ダウンロード
  4. VirtualBox.pkgをダブルクリック(インストーラを起動)
    あとは指示に従ってインストールするだけ。
    





Boxの追加

  1. ダウンロードURL一覧ページへ
  2. Box : VagrantのOSイメージみたいなもの
    Boxはここ↓にダウンロードURLの一覧がある
    http://www.vagrantbox.es/
    実際にダウンロードするときはコマンドを打つ
    
    今回は以下をダウンロード
    CentOS 7.0 x64 (Minimal, VirtualBox Guest Additions 4.3.28, Puppet 3.8.1 - see here for more infos)
    URLをコピーしておく
    
  3. ダウンロード
  4. 以下のコマンドでboxを追加
    

    vagrant box add {name} {url}


    {name}はcentos64にして実行。20分ぐらいかかりました。

    vagrant box add centos64 https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.1.0/centos-7.0-x86_64.box


  5. boxの確認


  6. boxの確認コマンドを打つ

    $ vagrant box list
    centos64 (virtualbox, 0)


    CentOSのバージョン名に、box名を変更

    $ cd ~/.vagrant.d/boxes/
    $ mv centos64 centos70


    で変更完了。確認してみる。

    $ vagrant box list
    centos70 (virtualbox, 0)






    VMの作成&起動

    1. ~/Vagrant/CentOS70を作業ディレクトリにする。
    2. $ mkdir -p ~/Vagrant/CentOS70
      $ cd ~/Vagrant/CentOS70/


    3. 初期化コマンド
    4. $ vagrant init centos70
      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 完成。確認してみる。

      $ ls
      Vagrantfile


    5. 起動
    6. $ vagrant up
      Bringing machine 'default' up with 'virtualbox' provider...
      ==> default: Importing base box 'centos70'...
      ==> default: Matching MAC address for NAT networking...
      ==> default: Setting the name of the VM: CentOS70_default_1499570759675_77339
      ==> default: Clearing any previously set forwarded ports...
      ==> default: Clearing any previously set network interfaces...
      ==> default: Preparing network interfaces based on configuration...
          default: Adapter 1: nat
      ==> default: Forwarding ports...
          default: 22 (guest) => 2222 (host) (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:
          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 it's present...
          default: Key inserted! Disconnecting and reconnecting using new SSH 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.3.28
          default: VirtualBox Version: 5.1
      ==> default: Mounting shared folders...
          default: /vagrant => /Users/y.suzuki/Vagrant/CentOS70

      VirtualBoxを見るとCentOSが起動している。




    7. 状態の確認
    8. $ vagrant status
      Current machine states:
      
      default                   running (virtualbox)
      
      The VM is running. To stop this VM, you can run `vagrant halt` to
      shut it down forcefully, or you can run `vagrant suspend` to simply
      suspend the virtual machine. In either case, to restart it again,
      simply run `vagrant up`.




    9. Vagrantへ接続
    10. $ vagrant ssh
      Last login: Thu Jul 16 08:48:31 2015 from 10.0.2.2
      Welcome to your Vagrant-built virtual machine.
      [vagrant@localhost ~]$ exit
      logout
      Connection to 127.0.0.1 closed.




    11. 停止
    12. $ vagrant halt
      ==> default: Attempting graceful shutdown of VM...




      とりあえずここまで(`・ω・´)ゞ




    13. 初めて使った感想


    14. くっそ便利