一、Vagrant介绍
Vagrant 是一个创建虚拟机的技术,可以把它认为是一个 Vmware,它让我们可以通过代码的方式快速地、可重复地创建针对不同虚拟环境的虚拟机,包括 Virtualbox、AWS、Docker 等。它使得我们可以一次性地、自动创建多个环境相同的虚拟机,对于软件开发和测试尤其有用。
二、Vagrant 安装
-
下载并安装virtualbox
-
下载并安装 vagrant
三、Vagrant命令
3.1 显示当前版本:
# vagrant --version
Vagrant 2.2.9
3.2 列出所有Box
#vagrant box list
centos/7 (virtualbox, 2004.01)
3.3 添加一个Box
#vagrant box add [options] <name, url, or path>
1)可以从官网 下载各种 Vagrant映像文件
#vagrant box add centos/7
2) 通过指定的URL添加远程box
#vagrant box add https://cloud.centos.org/centos/7/vagrant/x86_64/images/CentOS-7-x86_64-Vagrant-1804_02.VirtualBox.box
3) 添加一个本地box
命令格式如下
# vagrant box add {box_name} {file_path}
示例如下:
vagrant box add CentOS7 E:\Vagrant\boxs\CentOS-7-x86_64-Vagrant-1804_02.VirtualBox.box
3.4 初始化一个新VM
# vagrant init centos7
此命令会在当前目录创建一个名为 Vagrantfile 的配置文件,内容大致如下:
Vagrant.configure("2") do |config|
config.vm.box = "centos/7
end
当在此目录启动 Vagrant 后,Vagrant 会从互联网下载centos/7 这个 box到本地,并使用它作为 **VM **的映像。
3.5 初始化一个新VM
# vagrant up
如果我们想启动任意 VM,首先进入有 Vagrantfile 配置文件的目录,然后执行上面的命令。控制台的输出通常如下:
E:\Vagrant\centos7>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'centos/7' version '2004.01' is up to date...
==> 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: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
==> default: Rsyncing folder: /cygdrive/e/Vagrant/centos7/ => /vagrant
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
3.6 启用SSH登陆VM
进入 Vagrantfile 配置文件所在的目录,执行以下命令:
# vagrant ssh
如果需要从虚拟机中退出,直接在虚拟机中的命令行输入exit
命令即可
3.7 查看VM当前的状态
进入 Vagrantfile 配置文件所在的目录,执行以下命令:
# vagrant status
如果VM正在运行,你可以在控制台中看到如下信息:
Current machine states:
default running (virtualbox)
The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
3.8 关闭VM
进入 Vagrantfile 配置文件所在的目录,执行以下命令:
# vagrant halt
3.9 销毁VM
命令格式如下:
vagrant destory [name|id]
示例如下:
# vagrant destroy ceonts7
如果我们是使用了默认的虚拟机名"default",那其实我们可以直接输入如下命令销毁VM
# vagrant desotry
运行结果如下:
default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...
需要注意的是,我们要慎重使用该命令,该命令会停止VM的运行,并销毁所有在VM中创建的资源