码迷,mamicode.com
首页 > 其他好文 > 详细

用 vagrant 实现虚拟开发环境的快速部署

时间:2015-11-10 01:56:52      阅读:529      评论:0      收藏:0      [点我收藏+]

标签:vagrant box vagrantfile

  • Vagrant Introduction

        Vagrantprovides easy to configure, reproducible, and portable work environments builton top of industry-standard technology and controlled by a single consistentworkflow to help maximize the productivity and flexibility of you and your team.

        Toachieve its magic, Vagrant stands on the shoulders of giants. Machines areprovisioned on top of VirtualBox,VMware, AWS, or any other provider. Then, industry-standard provisioning toolssuch as shell scripts, Chef, or Puppet, can be used to automatically installand configuresoftwareon the machine. 

        Writtenby Ruby.

         More referhttps://docs.vagrantup.com/v2/why-vagrant/index.html

        Official website:  https://www.vagrantup.com/

        Mirror site: http://www.vagrantbox.es/ (download box file from here)


  • Vagrant commands

vagrant -h   (or vagrantcommand -h)

box                 manages boxes: installation, removal, etc.

connect           connect to a remotely shared Vagrant environment

destroy            stops and deletes all traces of thevagrant machine

global-status    outputs statusVagrant environments for this user

halt                   stops the vagrant machine

help                  shows the help for a subcommand

init                     initializes a new Vagrant environment bycreating a Vagrantfile

login                  log in to HashiCorp‘s Atlas

package            packages a running vagrant environment intoa box

plugin                manages plugins: install, uninstall,update, etc.

provision           provisions the vagrant machine

push                  deploys codein this environment to a configured destination

rdp                     connects tomachine via RDP

reload                restarts vagrantmachine, loads new Vagrantfileconfiguration

resume              resumea suspended vagrant machine

share                 shareyour Vagrant environment with anyone inthe world

ssh                     connects tomachine via SSH

ssh-config          outputs OpenSSH valid configuration to connect to the machine

status                 outputs statusof the vagrant machine

suspend             suspends themachine

up                       starts andprovisions the vagrant environment

version                prints currentand latest Vagrant version


  • Vagrant configuration -- Vagrantfile (gave 3 examples)

- following case for single machine

Vagrant.configure("2") do |config|

    config.vm.define :node1 do |node1|

         node1.vm.provider "virtualbox" do |v|

             v.customize ["modifyvm", :id, "--name", "node1", "--memory", "1024"]   #v.gui = true

         end

         node1.vm.box = " centos66"  

         node1.vm.hostname = "node1"  

         node1.vm.network :public_network, ip:"192.168.199.76", bridge: "eth0"

         node1.vm.synced_folder "C:/workstation", "/data"

     end

end 


  - following two case for multi machine

case 1:

Vagrant.configure("2") do |config|

  config.vm.define :node1 do |node1|

    node1.vm.provider "virtualbox" do |v|

          v.customize ["modifyvm", :id, "--name", "node1", "--memory", "1024"]

          #v.gui = true     

    end

    node1.vm.box = "centos66"  

    node1.vm.hostname = "node1"

    node1.vm.network :public_network, ip:"192.168.199.76", bridge: "eth0"

 end

  config.vm.define :node2 do |node2|

    node2.vm.provider "virtualbox" do |v|

          v.customize ["modifyvm", :id, "--name", "node2", "--memory", "1024"]

    end

    node2.vm.box = "centos66"

    node2.vm.hostname = "node2"

    node2.vm.network :public_network, ip: "192.168.199.77", bridge: "eth0"            

    #node2.vm.synced_folder "/u2/vm/mysql/node2_data", "/data"

  end

end

case 2:

Vagrant.configure("2") do |config|

  {

    :node1 => {

      :box     => ‘centos66‘,

      :box_url => ‘http://192.168.1.200/vms/centos66_v2.box‘,

    },

    :node2 => {

      :box     => ‘centos66‘,

      :box_url => ‘http://192.168.1.200/vms/centos66_v2.box‘,

    },

  }.each do |name,cfg|

    config.vm.synced_folder "./", "/data/web/apache_default/",

        owner: "apache", group: "deploycode"

    config.vm.define name do |local|

      local.vm.box = cfg[:box]

      local.vm.box_url = cfg[:box_url]

      local.vm.network "public_network" 

      #local.vm.network "forwarded_port", guest: 80, host: 2080,

      #auto_correct: true

      #local.vm.network "forwarded_port", guest: 3306, host: 3310,

          #auto_correct: true

      local.vm.provider :virtualbox do |vb|

          vb.customize ["modifyvm", :id, "--memory", 512, "--cpus", 1]

      end

    end

  end

end


  • Vagrantfile parameters explain

    node1.vm.provider"virtualbox " 定义使用virtualbox作为虚拟化平台

    v.customize["modifyvm", :id,"--name", "node1", "--memory", "1024"] 定义虚拟机大小

    v.gui= true   定义启动虚拟机的时候是否启动GUI界面,true为启动,false不启动

    node1.vm.box= "centos66"  定义虚拟机使用哪个box文件

    node1.vm.hostname= "node1"    定义虚拟机的主机名

    node1.vm.synced_folder“ C:/workstation”,“ /data ”  把本地文件夹共享给虚拟机,支持逻辑路径,即./

    node1.vm.network:public_network, ip:“192.168.199.76”,bridge: “eth0 定义虚拟的网络类型,分配IP,使用网桥的方式并绑定到eth0上

  -- 网络类型有:

    Public network:公共网络(虚拟机和宿主机一样的网络)

  eg: config.vm.network" public_network", ip: "192.168.1.200 "


Privatenetwork:私有网络(宿主机可以访问虚拟机,虚拟机间可以相互访问,

虚拟机可以访问外网)

 eg: config.vm.network" private_network ", ip: "192.168.1.200"


Forwardedport:端口映射,把宿主机的某一端口映射到虚拟机的某一端口上

   eg: config.vm.forwarded_port80,8080, protocol: “udp”   默认只转发tcp协议



  • Vagrantfile complete case

    cd C:\workstation

    Download a box file into this directory,such ascentos66_v2.box

    vagrantbox add centos66 centos66_v2.box

    vagrantbox list

    vagrant init

    Modified the Vagrantfile as before

    vagrant up centos66

    vagrant status centos66

    Then you can login into this machine and install MySQL

    - ip:port -> 127.0.0.1:2222

                 - username:password -> vagrant:vagrant

    - login to super user(root) with sudo su -

      vagrant halt

      vagrant package --base centos66 --output centos66_new.box --vagrantfile Vagrantfile

      vagrant destroy centos66

      vagrant box remove centos66

本文出自 “zhangdh开放空间” 博客,请务必保留此出处http://linuxblind.blog.51cto.com/7616603/1711159

用 vagrant 实现虚拟开发环境的快速部署

标签:vagrant box vagrantfile

原文地址:http://linuxblind.blog.51cto.com/7616603/1711159

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!