alexpdh's blog

windows 环境下 vagrant 安装配置

vagrant02.jpg

vagrant 介绍

vagrant 是一个虚拟机运行环境管理工具,通过它我们可以快速的通过命令搭建一个比较完善的开发环境,其本身并不是虚拟机。我们经常使用的虚拟机有 VirtualBox、VMware 等。

环境和软件准备

  • Windows 7 旗舰版
  • vagrant_2.0.0_x86_64.msi
  • VirtualBox-5.1.28-117968-Win.exe

安装步骤

VirtualBox 下载

到 VirtualBox 官网 https://www.virtualbox.org/wiki/Downloads 下载安装包。

vagrant 下载

到 vagrant 官网 https://www.vagrantup.com/downloads.html 下载安装包,如果没有翻墙的话最好移步到我分享的百度云下载 链接:http://pan.baidu.com/s/1pLp4hfX 密码:swky 。

镜像文件下载

vagrant 官网提供了很多镜像文件包,https://app.vagrantup.com/boxes/search,可以通过两种方式下载镜像,一种是直接使用命令从互联网上下载,另一种通过加装本地镜像文件来安装。这里使用第二种方式。

安装
  1. 首先我们安装了 VirtualBox 完成之后,打开 VirtualBox 使用快捷键 Ctrl + G 打开“全局设置”,选择 常规 里的 默认虚拟电脑位置,设置为非系统盘的位置。
  2. 安装了 vagrant 完成之后,vagrant 会默认设置虚拟机模板镜像文件存放在c:\User\.Vagrant.d里,当使用Vagrant管理的虚拟机模板镜像较多时,这个目录也是比较大的,需要将这个目录放到其它磁盘上。添加环境变量:VAGRANT_HOME,值设置如:D:\vagrant-projects.vagrant.d
  3. 新建目录,如:D:\vagrant-projects,用来管理虚拟机。
  4. 下载镜像文件到本地,我们可以通过如下图方式获取到镜像文件下载的地址,以 centos/7 为例:vagrant01.jpg,将链接复制到浏览器手动下载。这样在浏览器里下载断了也可以续。
  5. 在 vagrant-projects 目录下新建 centos7 目录,用于存放 centos/7 文件位置。
  6. 使用命令:vagrant box add {title} {url} 加载镜像文件,其中 title 自己定义取名,url 为镜像文件位置,如:
1
2
3
4
5
6
7
8
D:\vagrant-projects\centos7>vagrant box add centos7-11 D:\vagrant-projects\virtu
albox\CentOS-7-x86_64-Vagrant-1708_01.VirtualBox.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos7-11' (v0) for provider:
box: Unpacking necessary files from: file:///D:/vagrant-projects/virtualbox/
CentOS-7-x86_64-Vagrant-1708_01.VirtualBox.box
box: Progress: 100% (Rate: 47.8M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'centos7-11' (v0) for 'virtualbox'!
  1. 使用 vagrant init centos7-11 命令初始化虚拟机,执行完之后,发现 centos7 目录下多了个配置文件 Vagrantfile。
1
2
3
4
5
D:\vagrant-projects\centos7>vagrant init centos7-11
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.
  1. 编辑 Vagrantfile 文件,将对应的注释取消掉,自定义一些配置。

配置说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#配置主机名,进入操作系统执行 hostname 显示的名字,我用的规则是 系统版本-IP
#配置端口映射,通过访问本机的 8080 端口,访问安装后虚拟机的 80 端口
config.vm.network "forwarded_port", guest: 80, host: 8080
#配置虚拟机的 ip,默认网卡一是 NAT 方式,不需要配置
#这里创建网卡二 Host-Only,对应 VirtualBox 的 vbox0
config.vm.network "private_network", ip: "211.168.33.11"
#配置共享文件夹目录,进入系统后,自动挂载 /data
config.vm.synced_folder "/vagrant-work", "/data""
#VirtualBox 的配置
config.vm.provider "virtualbox" do |vb|
#这里配置显示在 VirtualBox 的名字,要不然是默认的一长串字符,太丑了
vb.name = "centos7-11"
#
#Display the VirtualBox GUI when booting the machine
vb.gui = false
#Customize the amount of memory on the VM:
vb.memory = "1024"
vb.cpus = "2"

Vagrantfile 示例

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- 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-11"
config.vm.hostname = "centos7-11"
# config.ssh.username = "vagrant"
# config.ssh.password = "vagrant"
# 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: "211.168.33.11"
# 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 "/vagrant-work", "/data"
# 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|
vb.name = "centos7-11"
# # 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"
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
  1. 使用 vagrant up –provider virtualbox 命令启动虚拟机
  2. 启动之后使用 vagrant ssh 登录虚拟机。

vagrant 常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
vagrant box add 添加box的操作
vagrant init 初始化box的操作
vagrant up 启动虚拟机的操作
vagrant ssh 登录拟机的操作
vagrant status 查看当前文件夹下虚拟机状态
vagrant ssh-config 输出用于ssh连接的一些信息
vagrant halt 强制关机
vagrant reload 重新启动虚拟机,主要用于重新载入配置文件
vagrant resume 恢复前面被挂起的状态
vagrant suspend 挂起当前的虚拟机
vagrant box list 显示当前已经添加的box列表
vagrant box remove 删除相应的box

参考文献

alexpdh wechat
欢迎扫一扫关注 程序猿pdh 公众号!