windows安装与使用vagrant虚拟化开发环境

vagrant是一个基于ruby的管理虚拟机的工具,可管理VirtualBox,VMware等虚拟机,用于创建和部署虚拟化开发环境,有占用资源小,可自动化部署等优点。

安装VirtualBox

vagrant对VirtualBox 虚拟机 提供的虚拟镜像是最多的,下载VirtualBox并安装。

安装vagrant

下载vagrant,安装重启后就可以使用了。

vagrant常用命令

vagrant box 命令

1
2
3
4
vagrant box add #添加虚拟机
vagrant box list #查看已添加的虚拟机
vagrant box remove #移除虚拟机
vagrant box update #更新虚拟机

vagrant 命令

1
2
3
4
5
6
7
8
9
10
vagrant init # 初始化
vagrant up # 启动虚拟机
vagrant halt # 关闭虚拟机
vagrant reload # 重启虚拟机
vagrant ssh # SSH 至虚拟机
vagrant status # 查看虚拟机运行状态
vagrant destroy # 销毁当前虚拟机
vagrant suspend #挂起虚拟机
vagrant resume #恢复挂起的虚拟机
vagrant package #打包虚拟机

添加vagrant虚拟机

https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.1.0/centos-7.0-x86_64.box 粘贴到迅雷下载,将下载vagrant用于VirtualBox的官方打包的CentOS7 64位镜像,其他镜像可在 http://www.vagrantbox.es/ 查看并下载

接下来将其放在一个目录,例如E:\vagrant

在E:\vagrant目录下依次输入

1
2
3
4
vagrant box add centos7 centos-7.0-x86_64.box 
vagrant init centos7
vagrant up
vagrant ssh

详细过程如下

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
E:\vagrant 
# vagrant box list
There are no installed boxes! Use `vagrant box add` to add some.

E:\vagrant
# vagrant box add centos7 centos-7.0-x86_64.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos7' (v0) for provider:
box: Unpacking necessary files from: file://E:/vagrant/centos-7.0-x86_64.box
box: Progress: 100% (Rate: 405M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'centos7' (v0) for 'virtualbox'!

E:\vagrant
# vagrant box list
centos7 (virtualbox, 0)

E:\vagrant
# 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.

E:\vagrant
# vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant_default_1497955973604_50433
==> default: Clearing any previously set forwarded ports...
==> default: Fixed port collision for 22 => 2222\. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2200 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2200
default: SSH username: vagrant
default: SSH auth method: private key
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default:
default: Guest Additions Version: 4.3.28
default: VirtualBox Version: 5.0
==> default: Mounting shared folders...
default: /vagrant => E:/vagrant

E:\vagrant
#
# vagrant ssh
Last login: Thu Jul 16 08:48:31 2015 from 10.0.2.2
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$

这样一个就连接到了一个centos7虚拟机上,安装系统后默认账户vagrant和root的密码均为vagrant,ssh的私钥文件在.vagrant\machines\default\virtualbox。

注意:vagrant ssh命令依赖ssh,windows环境变量里必须存在ssh,否则会出错,可使用xshell,secureCRT,putty等工具连接,我用的 cmder 默认有ssh。

使用CentOS7配置开发环境

首先更换yum源

1
2
3
4
5
6
cd /etc/yum.repos.d
sudo mv CentOS-Base.repo CentOS-Base.repo.back
sudo curl -O http://mirrors.163.com/.help/CentOS7-Base-163.repo
sudo yum clean all
sudo yum makecache
sudo systemctl stop firewalld.service

这后配置相应如lamp,lnmp等相关的开发环境。

配置Vagrantfile

将以下内容复制到Vagrantfile文件中

1
2
3
4
5
6
7
8
9
config.vm.network "private_network", ip: "192.168.33.10", auto_config:true	# 配置私网ip
config.vm.synced_folder "E:/vagrant/www/", "/var/www", :nfs => true # 配置共享目录
config.vm.network "forwarded_port", guest:80, host:8081 # 将虚拟机80端口映射到主机8081端口
config.vm.hostname = "dev" # 配置主机名

config.vm.provider "virtualbox" do |vb|
vb.memory = "2048" # 配置内存2048MB
vb.cpus = "2" # 配置CPU 2核
end

详细配置参见vagrant 文档

打包vagrant虚拟机

在虚拟机中配置好开发环境后,将其打包

vagrant package  --output dev_centos7.box --base centos7

之后将产生dev_centos7.box文件,以后只需这个box文件和Vagrantfile文件执行vagrant up将可以启动虚拟开发环境了。