Vagrant on Mac OS X

支 持 本 站: 捐贈伺服器等運維費用,需要您的支持!

test Vagrant on Mac OS X

install Vagrant

  • download vagrant from http://www.vagrantup.com/downloads.html
  • install vagrant_1.3.6.dmg
  • $ vagrant -v Vagrant 1.6.3
  • $ vagrant plugin list
    vagrant-login (1.0.1, system)
    vagrant-share (1.1.0, system)

install VirtualBox

  • download from https://www.virtualbox.org/wiki/Downloads
  • install VirtualBox-4.3.12-93733-OSX.dmg
  • start VirtualBox from Application

Vagrant up

  • Discover ready-made boxes from https://vagrantcloud.com/discover/featured or http://www.vagrantbox.es

setup a cent-os box from vagrantcloud

  • mkdir ~/vagrant; cd ~/vagrant; mkdir centos; cd centos
  • $ 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
  • $ vagrant up

    Bringing machine 'default' up with 'virtualbox' provider...
    ==> default: Box 'chef/centos-6.5' could not be found. Attempting to find and install...
        default: Box Provider: virtualbox
        default: Box Version: >= 0
    ==> default: Loading metadata for box 'chef/centos-6.5'
        default: URL: https://vagrantcloud.com/chef/centos-6.5
    ==> default: Adding box 'chef/centos-6.5' (v1.0.0) for provider: virtualbox
        default: Downloading: https://vagrantcloud.com/chef/centos-6.5/version/1/provider/virtualbox.box
    ==> default: Successfully added box 'chef/centos-6.5' (v1.0.0) for 'virtualbox'!
    ==> 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: username_default_1404531271065_27238
    ==> 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: Machine booted and ready!
    ==> default: Checking for guest additions in VM...
    ==> default: Mounting shared folders...
        default: /vagrant => /Users/username
    
  • $ vagrant box list
    chef/centos-6.5 (virtualbox, 1.0.0)

  • $ ls -l ~/.vagrant.d/boxes/chef-VAGRANTSLASH-centos-6.5/1.0.0/virtualbox/

    total 828160
    -rw-r--r--  1 username  staff        258  7  5 14:05 Vagrantfile
    -rw-r--r--  1 username  staff      11372  7  5 14:05 box.ovf
    -rw-r--r--  1 username  staff         26  7  5 14:05 metadata.json
    -rw-r--r--  1 username  staff  423996416  7  5 14:05 packer-centos-6.5-x86_64-disk1.vmdk
    
  • SSH
    $ vagrant ssh
    Last login: Fri Mar 7 16:57:20 2014 from 10.0.2.2
    [vagrant@localhost ~]$ w
    03:42:38 up 7 min, 1 user, load average: 0.00, 0.00, 0.00
    USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
    vagrant pts/0 10.0.2.2 03:42 0.00s 0.04s 0.03s w
    [vagrant@localhost ~]$ uname -a
    Linux localhost.localdomain 2.6.32-431.el6.x8664 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x8664 x8664 x8664 GNU/Linux

  • SYNCED FOLDERS
    $ cd /vagrant/
    [vagrant@localhost vagrant]$ ls
    Vagrantfile
    [vagrant@localhost vagrant]$ touch test
    ~$ exit
    $ ls
    test Vagrantfile

setup a freebsd box from vagrantbox.es

  • cd ~/vagrant; mkdir freebsd; cd freebsd
  • $ vagrant box add freebsd http://iris.hosting.lv/freebsd-9.2-i386.box
    ==> box: Adding box ‘freebsd’ (v0) for provider:
    box: Downloading: http://iris.hosting.lv/freebsd-9.2-i386.box
    ==> box: Successfully added box ‘freebsd’ (v0) for ‘virtual box’!
  • $ vagrant box list
    chef/centos-6.5 (virtualbox, 1.0.0)
    freebsd (virtualbox, 0)
  • $ ls -lstr ~/.vagrant.d/boxes/
    total 0
    0 drwxr-xr-x 3 username staff 102 7 5 14:14 freebsd
    0 drwxr-xr-x 4 username staff 136 7 5 14:05 chef-VAGRANTSLASH-centos-6.5
  • $ vagrant init freebsd
  • $ vagrant up
  • $ vagrant ssh
    FreeBSD 9.2-RELEASE (GENERIC) #0 r255898: Fri Sep 27 03:52:52 UTC 2013
    https://github.com/arkadijs/vagrant-freebsd
    [vagrant@vagrant-freebsd-92-i386 ~]$ uname -a
    FreeBSD vagrant-freebsd-92-i386 9.2-RELEASE FreeBSD 9.2-RELEASE #0 r255898: Fri Sep 27 03:52:52 UTC 2013 root@bake.isc.freebsd.org:/usr/obj/usr/src/sys/GENERIC i386

provision

install apache into centos

  • cd ~/vagrant/centos; vagrant ssh
  • vi /vagrant/bootstrap.sh

    #!/usr/bin/env bash
    sudo yum -y install httpd
    sudo echo "ServerName localhost:80" >> /etc/httpd/conf/httpd.conf
    sudo apachectl start
    
  • vi /vagrant/Vagrantfile

    Vagrant.configure("2") do |config|
      config.vm.box = "chef/centos-6.5"
      config.vm.provision :shell, path: "bootstrap.sh"
      config.vm.network :forwarded_port, host: 4567, guest: 80
    end
    
  • exit

  • vagrant reload —provision (or vagrant destroy; vagrant up)
  • check http://127.0.0.1:4567 in browser
  • vagrant ssh
  • ps ax|grep httpd
  • exit
  • vagrant suspend/vagrant halt/vagrant destroy

MULTI-MACHINE

  • cd ~/vagrant; mkdir web2db; cd web2db
  • vi Vagrantfile

    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
      config.vm.provision "shell", inline: "echo Web"
      config.vm.define "web" do |web|
        web.vm.box = "chef/centos-6.5"
        web.vm.hostname = "web01"
        web.vm.provision :shell, path: "bootstrapweb.sh"
        web.vm.network :forwarded_port, host: 8888, guest: 80
      end
      config.vm.provision "shell", inline: "echo Mysql"
      config.vm.define "db" do |db|
        db.vm.box = "chef/centos-6.5"
        db.vm.hostname = "db01"
        db.vm.provision :shell, path: "bootstrapdb.sh"
      end
      config.vm.provision "shell", inline: "echo Done"
    end
    
  • vi bootstrapweb.sh

    #!/usr/bin/env bash
    
    sudo yum -y install httpd
    sudo echo "ServerName localhost:80" >> /etc/httpd/conf/httpd.conf
    sudo apachectl start
    
  • vi bootstrapdb.sh

    #!/usr/bin/env bash
    
    sudo yum -y install mysql-server
    sudo echo "character-set-server = utf8" >> /etc/my.cnf
    sudo /etc/rc.d/init.d/mysqld start
    
  • vagrant on

  • vagrant ssh db
  • $ ps ax|grep mysql

     1968 ?        S      0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
     2073 ?        Sl     0:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
    
  • $ vagrant halt
    スクリーンショット 2014-07-05 17.07.30



支 持 本 站: 捐贈伺服器等運維費用,需要您的支持!
支 持 本 站: 捐贈伺服器等運維費用,需要您的支持!

發布時間: