Vagrant的更新比较多,因此大家不要尽信网上的教程,包括此文,建议使用最新版,配置还是以Vagrant官网文档为准。
本文主要以vagrant+VirtualBox为例
Vagrant能做什么?
1、统一开发环境。一次配置打包,统一分发给团队成员,统一团队开发环境,解决诸如“编码问题”,“缺少模块”,“配置文件不同”带来的问题;
2、避免重复搭建开发环境。新员工加入,不用浪费时间搭建开发环境,快速加入开发,减少时间成本的浪费;
3、多个相互隔离开发环境。可以在不用box里跑不同的语言,或者编译安装同一语言不同版本,搭建多个相互隔离的开发环境,卸载清除时也很快捷轻松。
安装步骤这里就不多说
工具及环境 | 版本 | 下载地址 |
机环境 | win10 x64 旗舰版 | http://msdn.itellyou.cn/ |
VirtualBox | VirtualBox-5.0.28-Win | https://www.virtualbox.org/ |
vagrant | vagrant_1.8.6 | http://www.vagrantup.com/ |
box | CentOS-6-x86_64.box | https://atlas.hashicorp.com/boxes/search |
xshell | xshell_5(Build1055) | http://www.netsarang.com/download/main.html |
常用命令:
1 2 3 4 5 6 7 8 9 10 |
vagrant box list # 列出当前导入的box vagrant init #初始化 vagrant destory # 销毁虚拟机 vagrant box remove [name] # 移除虚拟机 vagrant up [name] # 启动虚拟机 vagrant halt [name] # 关闭虚拟机 vagrant status [name] # 查看虚拟机的状态 vagrant ssh [name] #ssh到虚拟机 vagrant reload #重启虚拟机(重新载入配置文件) vagrant package #打包分发 |
1、添加box
新建一个文件夹作为工作目录(建议不要使用中文),打开Dos窗口,进入该目录(以下打操作均在此目录中进行)
1 2 |
#提前下载好的box文件centos6.box #我们给这个box命名为centos6 |
1 |
vagrant box add centos6 d:/centos6.box |
1 2 |
#box文件也可以是远程地址 base 为默认名称 vagrant box add base http://files.vagrantup.com/centos6.box |
补充:
如果使用命令从官网添加,网速很慢,有时还会中断,我们可以下载box到本地之后再安装
官网box的下载链接不太好找,利用“油猴”脚本插件可很快找到下载地址(如下图)
“油猴”插件下载根据自己的浏览器安装
添加脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
// ==UserScript== // @id vagrant-box-download-helper@everyx // @name Vagrant Box Download Helper // @version 1.0 // @namespace http://userscript.everyx.in/vagrant-box-download-helper // @author everyx // @description Add a download button in atlas.hashicorp.com. // @include https://atlas.hashicorp.com/* // @run-at document-end // ==/UserScript== main(); function main() { var elementList = document.querySelectorAll(".set-item"); for (var i = 0; i < elementList.length; i++) { addDownloadButton(elementList[i]); } } function addDownloadButton(element) { var rows = element.querySelectorAll(’.status’); var versionHref = element.querySelector(’.row a:last-child’).href; for (var i = 0; i < rows.length; i++) { var currentRow = rows[i]; var downloadBtn = document.createElement(’a’); downloadBtn.className =’button right icon ion-arrow-down-a’; downloadBtn.title = ’download’; downloadBtn.style = ’font-weight: bold; color:#2F88F7;’; var provider = currentRow.querySelector(’.subtitle’).innerHTML.trim().replace(/s+/g, ’’).replace(/<.*$/g, ’’); downloadBtn.href = versionHref + ’/providers/’ + provider + ’.box’; currentRow.appendChild(downloadBtn); } } |
2、初始化
1 2 3 |
vagrant init #如果你添加的box名称不是base,那么需要在初始化的时候指定名称,例如 vagrant init centos6 |
3、启动虚拟机
会出现如下类似信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Bringing machine ’default’ up with ’virtualbox’ provider... [default] Importing base box ’base’... [default] Matching MAC address for NAT networking... [default] Setting the name of the VM... [default] Clearing any previously set forwarded ports... [default] Creating shared folders metadata... [default] Clearing any previously set network interfaces... [default] Preparing network interfaces based on configuration... [default] Forwarding ports... [default] -- 22 => 2222 (adapter 1) [default] Booting VM... [default] Waiting for VM to boot. This can take a few minutes. [default] VM booted and ready for use! [default] Mounting shared folders... [default] -- /vagrant |
如果只有一台虚拟机,我们直接可以用xshell进行链接
root:vagrant password:vagrant
补充:
1 |
The error I get: "rsync" could not be found on your PATH. Make sure that rsync is properly installed on your system and available on the PATH. |
解决方法:
1)、在DOS窗口输入 vagrant plugin install vagrant-vbguest 等待安装完成
2)、在你创建的工作目录中找到Vagrantfile,添加
1 |
config.vm.synced_folder ".", "/vagrant", type: "virtualbox" |
保存,然后重新启动
3.1启动多台虚拟机
vagrant ssh [name] #[name]是第一步命名的box
事例:我们使用了:web以及:db分?做了两个VM的设置,并且给每个VM设置了不同的hostname和IP,设置好之后再使用vagrant up将虚拟机跑起来
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Vagrant.configure("2") do |config| config.vm.define :web do |web| web.vm.provider "virtualbox" do |v| v.customize ["modifyvm", :id, "--name", "web", "--memory", "512"] end web.vm.box = "base" web.vm.hostname = "web" web.vm.network :private_network, ip: "11.11.1.1" end config.vm.define :db do |db| db.vm.provider "virtualbox" do |v| v.customize ["modifyvm", :id, "--name", "db", "--memory", "512"] end db.vm.box = "base" db.vm.hostname = "db" db.vm.network :private_network, ip: "11.11.1.2" end end |
用vagrant ssh 分别启动
1 2 3 4 5 |
$ vagrant ssh web vagrant@web:~$ $ vagrant ssh db vagrant@db:~$ |
4、打包vagrant package
关闭当前机器,然后打包
Vagrantfile配置
工作目录下有一个文件Vagrantfile,里面包含有大量的配置信息,主要包括三个方面的配置,虚拟机的配置、SSH配置、Vagrant的一些基础配置。
1、虚拟机网络设置
1 2 3 4 |
#config.vm.network "private_network", ip: "192.168.33.10" config.vm.network "public_network" #私有网络(private_network) #公有网络(public_network) |
2、同步目录
1 2 |
#config.vm.synced_folder "../data", "/vagrant_data" config.vm.synced_folder ".", "/vagrant", type: "virtualbox" |
3、端口转发
1 2 |
config.vm.network "forwarded_port", guest: 80, host: 8080 #把虚拟机的8080端口转发到本机80端口 |
4、内存和cpu核心
1 2 3 4 5 6 7 8 |
config.vm.provider "virtualbox" do |vb| #Display the VirtualBox GUI when booting the machine vb.gui = true #Customize the amount of memory on the VM: vb.memory = "1024" vb.cpus = 2 vb.name = "my_vm" end |
文章参考:
Vagrant with VirtualBox on Windows10: “Rsync” could not be found on your PATH
文章评论