vagrant的安装与初识

安装VirtualBox
到官网 https://www.virtualbox.org/wiki/Downloads 点击下载最新的virtualbox,直接安装。
 
安装Vagrant
到官网 https://www.vagrantup.com/downloads.html 下载最新的vagrant,然后安装,
在windows下安装vagrant,有可能需要配置环境变量,会要求重新启动一下系统。
 
如何配置Vagrant
一、下载一个合适的box
http://www.vagrantbox.es/ 下载你想要的操作系统box。我下载的是centos7。
 
二、创建vagrant开发环境目录
在D盘创建一个vagrant目录,并切换到vagrant目录下。
$ /d/vagrant
$ cd /d/vagrant
二、把box下载到本地,然后再进行安装。 
2. 把下载好的centos-7.0-x86_64.box,移动到D盘vagrant目录下面。
3. 添加box命令:
vagrant box add {title} {url}
vagrant init {title}
vagrant up

vagrant box add 是添加box的命令
{title}可以自行设置,我这里使用的是 centos7
{url}是下载到本地box路径。我的路径是:/d/vagrant/centos-7.0-x86_64.box
 
四、把centos-7.0-x86_64.box添加到虚拟机中。
1. 安装box,切换目录到vagrant下。
$ cd /d/vagrant
$ vagrant box add CentOs7 centos-7.0-x86_64.box
输出内容:
Downloading or copying the box...
Extracting box...te: 47.5M/s, Estimated time remaining: --:--:--)
Successfully added box 'base' with provider 'virtualbox'!

box中的镜像文件被放到了:/Users/helei/.vagrant.d/boxes/。
window系统中应该是放到了: C:\Users\当前用户名.vagrant.d\boxes\目录下。
# 如果是才add 的box,就必须执行本步骤,初始化一次后,以后启动系统,就不需要执行本步骤。
$ vagrant init CentOs7
输出内容:
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的文件,里面有很多配置信息。
2. 配置信息如下:
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
 # The most common configuration options are documented and commented below.
 # For a complete reference, please see the online documentation at
 # https://docs.vagrantup.com.

 # Every Vagrant development environment requires a box. You can search for
 # boxes at https://vagrantcloud.com/search.
 config.vm.box = "CentOS7"

 # Disable automatic box update checking. If you disable this, then
 # boxes will only be checked for updates when the user runs
 # `vagrant box outdated`. This is not recommended.
 # config.vm.box_check_update = false

 # Create a forwarded port mapping which allows access to a specific port
 # within the machine from a port on the host machine. In the example below,
 # accessing "localhost:8080" will access port 80 on the guest machine.
 # NOTE: This will enable public access to the opened port
 config.vm.network "forwarded_port", guest: 80, host: 8080

 # Create a forwarded port mapping which allows access to a specific port
 # within the machine from a port on the host machine and only allow access
 # via 127.0.0.1 to disable public access
 # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

 # Create a private network, which allows host-only access to the machine
 # using a specific IP.
 config.vm.network "private_network", ip: "192.168.33.10"

 # Create a public network, which generally matched to bridged network.
 # Bridged networks make the machine appear as another physical device on
 # your network.
 # config.vm.network "public_network"

 # Share an additional folder to the guest VM. The first argument is
 # the path on the host to the actual folder. The second argument is
 # the path on the guest to mount the folder. And the optional third
 # argument is a set of non-required options.
 config.vm.synced_folder "/data/wwwroot/vagrant", "/data/wwwroot/vagrant"

 # Provider-specific configuration so you can fine-tune various
 # backing providers for Vagrant. These expose provider-specific options.
 # Example for VirtualBox:
 #
 # 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"
 # end
 #
 # View the documentation for the provider you are using for more
 # information on available options.

 # Enable provisioning with a shell script. Additional provisioners such as
 # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
 # documentation for more information about their specific syntax and use.
 # config.vm.provision "shell", inline: <<-SHELL
 # apt-get update
 # apt-get install -y apache2
 # SHELL
end
注意:config.vm.synced_folder配置。
前面是本地项目存放目录,后面是虚拟机存放目录。
config.vm.synced_folder "/data/wwwroot/vagrant", "/data/wwwroot/vagrant"
 
3. 启动系统
$ vagrant up
输出内容:
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...
...
ssh链接到安装的虚拟机
可以使用第三方(xshell等)shell工具或系统自带的,进行登录。
在系统中,如mac,git,可直接使用 vagrant ssh 来完成链接。
使用第三方如xshell,ip地址是:127.0.0.1,虚拟机启动时观察映射的22端口号是多少,一般是2200 或者2222
用户名与密码均是: vagrant
注意:登录虚拟机时不要用配置文件设置的IP地址进行登录。
 
vagrant的命令详解
命令 作用
vagrant box add 添加box的操作
vagrant init 初始化box的操作,会生成vagrant的配置文件Vagrantfile
vagrant up 启动本地环境
vagrant ssh 通过 ssh 登录本地环境所在虚拟机
vagrant halt 关闭本地环境
vagrant suspend 暂停本地环境
vagrant resume 恢复本地环境
vagrant reload 修改了 Vagrantfile 后,使之生效(相当于先 halt,再 up)
vagrant destroy 彻底移除本地环境
vagrant box list 显示当前已经添加的box列表
vagrant box remove 删除相应的box
vagrant package 打包命令,可以把当前的运行的虚拟机环境进行打包
vagrant plugin 用于安装卸载插件
vagrant status 获取当前虚拟机的状态
vagrant global-status 显示当前用户Vagrant的所有环境状态

您可以选择一种方式赞助本站

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: