Vagrant虚拟化之基本使用

简介

  Vagrant是一个使用Ruby开发,基于VirtualBox、VMware等虚拟机管理软件的接口,提供一个可配置、跨平台、轻量级的便携式虚拟机开发工具。

Vagrant的特点:
1. Vagrant会创建共享文件夹,用来在主机和虚拟机之间进行资源共享;
2. Vagrant通过添加box镜像进行快速部署,部署完毕后可以通过package进行打包分发,避免二次重建环境;
3. Vagrant可以使用puppet、chref等管理工具进行自动化配置部署;
4. Vagrant支持单机模拟多台机器,且支持一个配置文件Vagrantfile就可以跑分布式系统。

  从以上vagrant貌似是在传统虚拟机的基础上,通过box镜像的方式进行了更高级别的应用。让开发人员、测试人员甚至是整个团队都可以很好的搭建开发、测试环境及多部门的协作。

  为了让大家更好的了解vagrant,我们先来讲下vagrant的基本使用。

环境部署

我的宿主主机是win10,需要安装virtualbox和vagrant,在这里就不多说了。

1.主机上创建测试环境目录

D:\vagrant

2.添加box

vagrant box add base ubuntu/trusty64

其中base是默认的box的名称,可以是任意字符串,主要用来标识下你添加的box。如果我们不想用base,还可以用

vagrant box add test ubuntu/trusty64
或
vagrant box add --name test ubuntu/trusty64

windowsxitong 添加的box镜像默认被放到了C:\Users\用户名.vagrant.d\boxes下。

要点:
box是一个操作系统环境,实际上它是一个zip包,包含了Vagrant的配置信息和VirtualBox的虚拟机镜像文件。
根据vagrant官网介绍,目前只有两个官方推荐box站点:
https://atlas.hashicorp.com/boxes/search
https://atlas.hashicorp.com/bento

在此我们测试使用的是第一个的ubuntu/trusty64

3.初始化

#初始化默认的base
vagrant init
或
#初始化自定义的test
vagrant init test

初始化完成后,在D:\vagrant目录下会生成vagrant的配置文件Vagrantfile。此时Vagrantfile文件中的config.vm.box 根据你刚才init命令变为”base”或 “test”

4.启动虚拟机

vagrant up

5.连接虚拟机

vagrant ssh

6.重载配置文件

vagrant reload

7.查看状态

$ vagrant global-status
id       name    provider   state    directory
-------------------------------------------------------------------------
439842d  default virtualbox poweroff C:/cygwin/home/cityre
cc34b90  default virtualbox poweroff D:/vagrant
6967221  default virtualbox poweroff D:/vagrant_base
cd48748  admin   virtualbox running  D:/vagrant

我们修改Vagrantfile这个配置文件后,想要配置生效,需要重载配置文件,这个过程是需要先关闭再启动虚拟机的。

另外,我们若要上面的全局状态信息是缓存的,如果你已经删除了这个 box ,可能执行这个命令还会看到删除前的状态,就像是一个 snapshot。我们可以进入对应的目录,通过以下命令清理缓存状态:

vagrant global-status --prune

以上是整个vagrant虚拟机从创建到登陆的过程,虽然简单,但是还有些细节需要我们注意。

细节

1.虚拟机名称和Virtualbox gui 名称
首先我们需要知道当vagrant box add 时的名称和刚才说的这两个名称是完全不相干的,我们可以理解成是box的别名。

(1)虚拟机名称

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: A newer version of the box 'ubuntu/trusty64' is available! You currently
==> default: have version '20160826.0.0'. The latest is version '20160826.0.1'. Run
==> default: `vagrant box update` to update.

当启动虚拟机时,我们从提示看到“Bringing machine ‘default’ up with ‘virtualbox’ provider”,意思是通过virtualbox启动名称为“default”的虚拟机。也就是说虚拟机的默认名称为default,但是我们运行多个虚拟机时,都为default的话是不是会搞混?因此我们需要单独命名这个虚拟机,通过修改Vagrantfile:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.hostname = "ubuntu-admin"
end
也可以
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.define :admin do |admin|
      admin.vm.hostname = "ubuntu-admin"
  end
end

通过config.vm.define我们将虚拟机的名称改成了admin,并且将虚拟机的hostname改成了ubuntu-admin

注意:
默认情况是配置文件是全局的,如果在一个配置文件里配置多个虚拟机需要在每个对应的虚拟机下单独配置;也就说所有关于vagrant虚拟机中部署的系统配置配置需要在config.vm.define这块配置,或是在全局下配置。
但是如果你在全局下环境下安装了相关包,后来在同一个配置文件中添加config.vm.define,会导致全局环境的配置失效,此时虚拟机中运行的是config.vm.defaine块下的配置。
如下:

  config.vm.define :admin do |admin|
    admin.vm.box = "ubuntu/trusty64"
    admin.vm.hostname = "ubuntu-admin"
    admin.vm.network "private_network", ip: "10.1.2.1"
  end

重启虚拟机

$ vagrant up
Bringing machine 'admin' up with 'virtualbox' provider...
==> admin: Importing base box 'ubuntu/trusty64'...
==> admin: Matching MAC address for NAT networking...
==> admin: Checking if box 'ubuntu/trusty64' is up to date...
==> admin: A newer version of the box 'ubuntu/trusty64' is available! You currently
==> admin: have version '20160826.0.0'. The latest is version '20160826.0.1'. Run
==> admin: `vagrant box update` to update.
==> admin: Setting the name of the VM: vagrant_admin_1472630156067_53267
==> admin: Clearing any previously set forwarded ports...
==> admin: Fixed port collision for 22 => 2222. Now on port 2200.
==> admin: Clearing any previously set network interfaces...
==> admin: Preparing network interfaces based on configuration...
    admin: Adapter 1: nat
    admin: Adapter 2: hostonly
==> admin: Forwarding ports...
    admin: 22 (guest) => 2200 (host) (adapter 1)
==> admin: Booting VM...
==> admin: Waiting for machine to boot. This may take a few minutes...
    admin: SSH address: 127.0.0.1:2200
    admin: SSH username: vagrant
    admin: SSH auth method: private key
    admin: Warning: Remote connection disconnect. Retrying...
    admin: Warning: Remote connection disconnect. Retrying...
    admin:
    admin: Vagrant insecure key detected. Vagrant will automatically replace
    admin: this with a newly generated keypair for better security.
    admin:
    admin: Inserting generated public key within guest...
    admin: Removing insecure key from the guest if it's present...
    admin: Key inserted! Disconnecting and reconnecting using new SSH key...
==> admin: Machine booted and ready!
==> admin: Checking for guest additions in VM...
    admin: The guest additions on this VM do not match the installed version of
    admin: VirtualBox! In most cases this is fine, but in rare cases it can
    admin: prevent things such as shared folders from working properly. If you see
    admin: shared folder errors, please make sure the guest additions within the
    admin: virtual machine match the version of VirtualBox you have installed on
    admin: your host and reload your VM.
    admin:
    admin: Guest Additions Version: 4.3.36
    admin: VirtualBox Version: 5.1
==> admin: Setting hostname...
==> admin: Configuring and enabling network interfaces...
==> admin: Mounting shared folders...
    admin: /vagrant => D:/vagrant

从上面打印的信息,我们看到虚拟机的名字已经由default改成admin了,看来我们的配置生效了。另外登陆虚拟机看下hostname

vagrant@ubuntu-admin:~$ hostname
ubuntu-admin
vagrant@ubuntu-admin:~$ 

hostname也有原来默认的改成我们定义的了。

(2)Virtualbox gui名称
默认情况下,Virtualbox gui名称如下图:
Virtualbox gui名称
可以看到第一个名称是我们在默认启动后生成的,格式为DIRECTORY_default_TIMESTAMP.
其中第二个是我们自定义的名称,按照以下设置

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.define :admin do |admin|
      admin.vm.hostname = "ubuntu-admin"
  end
    config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
      vb.name = "ubuntu-admin"
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  end
end

重启后Virtualbox gui名称就更改了。
注意:
(1).默认情况下虚拟机内存为512MB,我们可以通过注释掉vb.memory = “1024”来更改
(2).若启动后需要显示virtualbox界面,则注释掉vb.gui = true
也就说所有关于virtualbox虚拟机的配置需要在config.vm.provider这块配置。

2.虚拟机网络配置
虚拟机的网路配置有3种:
(1)config.vm.network “forwarded_port”, guest: 80, host: 8080
将虚拟机的80端口映射到宿主主机的8080端口
(2)config.vm.network “private_network”, ip: “10.1.2.1”
将虚拟机只和你的主机联通,不允许网络中其他人通信
(3)config.vm.network “public_network”
将虚拟机和本地网络公用
我们一般使用第二种,自high就行了,如下:

config.vm.network "private_network", ip: "10.1.2.1"

重启后,我们就可以通过10.1.2.1进行ssh登陆了,默认的用户名密码都为vagrant。

3.box镜像升级
默认情况下,box镜像会自动监测升级包,并在启动时候进行提示,但是不会自己升级。

$ vagrant up
Bringing machine 'admin' up with 'virtualbox' provider...
==> admin: Importing base box 'ubuntu/trusty64'...
==> admin: Matching MAC address for NAT networking...
==> admin: Checking if box 'ubuntu/trusty64' is up to date...
==> admin: A newer version of the box 'ubuntu/trusty64' is available! You currently
==> admin: have version '20160826.0.0'. The latest is version '20160826.0.1'. Run

看到对于ubuntu/trusty64 box镜像,当前版本为20160826.0.0,有新的版本20160826.0.1。
我们也可以手动检查是否有新版本:

$ vagrant box -h
Usage: vagrant box <subcommand> [<args>]
Available subcommands:
     add
     list
     outdated
     remove
     repackage
     update
For help on any individual subcommand run `vagrant box <subcommand> -h`
cityre@cityre-pc /cygdrive/d/vagrant
$ vagrant box outdated
Checking if box 'ubuntu/trusty64' is up to date...
A newer version of the box 'ubuntu/trusty64' is available! You currently
have version '20160826.0.0'. The latest is version '20160826.0.1'. Run
`vagrant box update` to update.

**注意:**box镜像升级,需要重新配置虚拟机,原来的老虚拟机的数据会丢失,因此升级需再三考虑。

打包分发并导入

1.打包

vagrant package --output ubuntu.box

不用output参数,默认的打包名称为package.box。打包完成后在vagrant目录下生成了ubuntu.box

2.导入

vagrant box add ubuntu-test ubuntu.box
vagrant init ubuntu-test
vagrant up 

由于实在vagrant目录下导入并初始化的,因此需要将之前生成的Vagrantfile删除或备份,否则报错。
如有其他改动请修改Vagrantfile配置文件并重启。

总结

  vagrant的使用其实还有很多需要我们自己摸索的地方,以上只能满足我们日常的使用。另外,vagrant的多种providers,如vmware等都大同小异,但是其中一个docker,想必是比较吸引人的地方,可以值得我们研究一番。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值