Vagrant教程
在本教程中,我们将了解Vagrant,vagrant框,vagrantfile,配置工具,如何在Linux操作系统上安装vagrant,并最终学习一些基本的vagrant命令,以从命令行创建和管理虚拟机。
Vagrant简介
Vagrant是用于构建和维护虚拟软件开发环境的开源软件。
它提供了一个干净,易于配置,可复制且可移植的开发环境。
换句话说,Vagrant使我们能够轻松快速地部署可在任何地方使用的通用软件开发环境。
Vagrant 只是消除了“在我的机器上工作”的借口。
因为,每个人都在具有相同配置集的相同环境中工作。
他们使用什么操作系统都没有关系。
Vagrant通常用于为开发人员建立一个相同的协作工作环境,以使其从事一个共同的项目。
它在单个一次性环境中隔离了所有必需的依赖项及其配置设置。
当其他开发人员使用相同的配置文件创建开发环境时,他们将获得具有相同设置的相同环境。
Vagrant是用Ruby语言编写的跨平台应用程序。
它支持GNU/Linux,Mac OS和Microsoft Windows。
它是由Hashicorp开发并在MIT许可下发布的。
vagrant 盒子
“盒子box”是Vagrant环境的格式和扩展名。
vagrant 的盒子只是基础图像。
它们可以是Virtualbox镜像或者VMware镜像,也可以是云提供商的镜像,例如Amazon Machine Image(AMI)。
我们可以将无用信息框复制到任何其他系统,并设置当前开发的精确副本。
在Vagrant Cloud公共存储库中可以下载许多预配置的Vagrant框。
如果我们不想使用预先配置的框,则可以根据自己的喜好创建一个,然后通过此存储库将其分发给所有人。
Vagrant文件
操作系统和软件要求在名为“ vagrantfile”的配置文件中定义。
该文件与“ Vagrant”框一起分发。
当我们使用Vagrant初始化VM时,它将读取该文件并相应地设置开发环境。
因此,vagrantfile的实际目的是描述虚拟机的类型以及如何配置和配置VM。
在典型的vagrantfile中定义了以下详细信息:
- 操作系统版本,例如Ubuntu仿生。
- 启用/禁用自动更新框检查。
- 网络配置;
- 转发端口,
- 配置专用网络(仅主机访问计算机),
- 配置公共网络(桥接网络)。
- 将文件夹共享到来宾VM。
- 设置首选提供商virtualbox,kvm。
- 定义VM的内存总量。
- 使用shell脚本或者配置管理工具(如Ansible)启用配置。
调配工具
Vagrant不是像KVM或者Virtualbox这样的独立虚拟化平台。
它只是位于虚拟化软件和虚拟机之间的包装程序和前端。
Vagrant使用各种服务提供商和供应工具来创建和管理开发环境。
虚拟机建立在流行的虚拟化应用程序(例如VirtualBox,KVM,Docker,VMware等)以及云服务提供商(例如AWS,Azure,GCE等)的基础上。
我们可以在此处查看受支持提供商的完整列表。
Vagrant开箱即用,支持VirtualBox,Hyper-V和Docker。
VirtualBox是Vagrant的默认提供程序。
构建虚拟机后,我们可以使用简单的Shell脚本和行业标准的配置管理工具(例如Ansible,CFEngine,Chef,Docker,Podman,Puppet和Salt等)在其上安装软件。
用户还可以自定义虚拟环境,使用供应工具按其要求进行操作。
简而言之,将提供程序(例如VirtualBox,AWS)用于创建虚拟环境,将配置程序(例如Ansible,Puppet)用于自定义虚拟环境。
在Linux上安装Vagrant
无家可归的安装令人难以置信的简单明了。
只需从Vagrant下载页面下载最新版本,然后使用适用于操作系统的标准步骤进行安装即可。
要了解如何在各种Linux平台上安装Vagrant,请参考以下链接。
- 如何在Linux上安装Vagrant
Vagrant教程-创建和管理虚拟机的基本Vagrant命令
其中我仅给出了设置和管理虚拟化开发所需的基本Vagrant命令的示例。
要了解完整的Vagrant用法,请参阅最后提供的官方文档。
1.创建Vagrant项目目录
首先,我们需要创建一个项目目录。
我将为我的虚拟环境创建一个名为“ myvagrants”的目录:
$mkdir myvagrants
将CD放入该目录以构建和存储虚拟机:
$cd myvagrants
2.初始化Vagrant环境
使用以下命令初始化Vagrant环境:
$vagrant init hashicorp/bionic64
输出示例:
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.
上面的命令将当前目录初始化为默认的Vagrant环境,并创建基本Vagrantfile。
我们可以使用“ cat”命令查看新创建的vagrantfile的内容,并知道要构建的虚拟机的详细信息:
$cat Vagrantfile | less
在后续步骤中启动VM时,它将下载HashiCorp发布的Ubuntu仿生盒。
流动团队还建议使用便当盒。
它们是开源的,并为诸如Virtualbox,VMWare和Parallels之类的流行提供程序构建。
HashiCorp和Bento是仅有的两个官方推荐的包装盒。
3.启动虚拟机
现在,使用以下命令根据vagrantfile(在上一步中创建的)创建并启动虚拟机:
$vagrant up
输出示例:
Bringing machine 'default' up with 'virtualbox' provider... ==> default: Box 'hashicorp/bionic64' could not be found. Attempting to find and install... default: Box Provider: virtualbox default: Box Version: >= 0 ==> default: Loading metadata for box 'hashicorp/bionic64' default: URL: https://vagrantcloud.com/hashicorp/bionic64 ==> default: Adding box 'hashicorp/bionic64' (v1.0.282) for provider: virtualbox default: Downloading: https://vagrantcloud.com/hashicorp/boxes/bionic64/versions/1.0.282/providers/virtualbox.box Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com ==> default: Successfully added box 'hashicorp/bionic64' (v1.0.282) for 'virtualbox'! ==> default: Importing base box 'hashicorp/bionic64'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'hashicorp/bionic64' version '1.0.282' is up to date... ==> default: Setting the name of the VM: myvagrants_default_1597837509450_67666 Vagrant is currently configured to create VirtualBox synced folders with the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant guest is not trusted, you Jan want to disable this option. For more information on this option, please refer to the VirtualBox manual: https://www.virtualbox.org/manual/ch04.html#sharedfolders This option can be disabled globally with an environment variable: VAGRANT_DISABLE_VBOXSYMLINKCREATE=1 or on a per folder basis within the Vagrantfile: config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 (guest) => 2222 (host) (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This Jan take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key default: Warning: Connection reset. Retrying... default: Warning: Remote connection disconnect. Retrying... default: Warning: Remote connection disconnect. Retrying... 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: 6.0.10 default: VirtualBox Version: 6.1 ==> default: Mounting shared folders... default: /vagrant => /home/sk/myvagrants
上面的命令将从Vagrant云中下载Ubuntu仿生盒子,创建一个名为“ myvagrants_default_1597837509450_67666”的新虚拟机,将其添加到Virtualbox并自动启动VM。
即使VM正在运行,我们也不会看到任何通知。
要验证VM是否正在运行,请打开虚拟化应用程序并进行检查。
由于Vagrant使用Virtualbox作为其默认提供程序,因此我可以从virtualbox管理器中查看正在运行的VM。
如我们所见,Ubuntu仿生VM在Virtualbox中运行。
4.访问虚拟机
我们可以使用SSH连接和访问正在运行的VM。
要使用命令SSH进入正在运行的Vagrant框中,请运行:
$vagrant ssh
输出示例:
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-58-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage System information as of Wed Aug 24 11:56:42 UTC 2017 System load: 0.08 Processes: 88 Usage of /: 2.5% of 61.80GB Users logged in: 0 Memory usage: 11% IP address for eth0: 10.0.2.15 Swap usage: 0% * Are you ready for Kubernetes 1.19? It's nearly here! Try RC3 with sudo snap install microk8s --channel=1.19/candidate --classic https://microk8s.io/has docs and details. 0 packages can be updated. 0 updates are security updates. Hyman@theitroad:~$
5.同步本地和来宾文件
Vagrant支持文件同步开箱即用。
默认情况下,Vagrant与来宾虚拟机中的“/vagrant”目录共享项目目录(包含Vagrantfile的目录。
在我的情况下,它是~/myvagrants。
)。
我们可以通过列出虚拟机中/vagrant目录的内容来验证它:
Hyman@theitroad:~$ls /vagrant/ Vagrantfile
请注意,我们在虚拟机内部看到的Vagrantfile实际上与实际主机上的Vagrantfile相同。
要测试同步是否真正有效,请在来宾计算机的/vagrant目录中创建一个示例文件:
Hyman@theitroad:~$touch /vagrant/test.txt
退出VM的SSH会话:
Hyman@theitroad:~$logout
现在,检查主机系统中的本地项目目录。
我们还将看到在主机上也创建了相同的文件。
Hyman@theitroad:~/myvagrants$ls test.txt Vagrantfile
6.显示虚拟机的状态
要显示虚拟环境中虚拟机的状态,请运行:
$vagrant status
输出示例:
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`.
如我们在上面看到的,我只有一个VM,并且它现在正在运行。
如果虚拟机已关闭电源,则将显示以下输出:
Current machine states: default poweroff (virtualbox) The VM is powered off. To restart the VM, simply run `vagrant up`
7.显示所有虚拟环境的状态
我们可以使用以下命令显示有关系统上所有已知虚拟环境的信息:
$vagrant global-status
输出示例:
id name provider state directory ------------------------------------------------------------------------ 2086482 default virtualbox poweroff /home/sk/myvagrants The above shows information about all known Vagrant environments on this machine. This data is cached and Jan not be completely up-to-date (use "vagrant global-status --prune" to prune invalid entries). To interact with any of the machines, you can go to that directory and run Vagrant, or you can use the ID directly with Vagrant commands from any directory. For example: "vagrant destroy 1a2b3c4d"
上面的命令将列出VM的ID,名称,提供程序,VM的状态以及虚拟环境目录。
有时,输出可能会被缓存,并且可能不正确。
因此,运行以下命令以删除无效的条目并获取虚拟环境的最新状态:
$vagrant global-status --prune
8.挂起虚拟机
要挂起正在运行的VM,请运行:
$vagrant suspend
输出示例:
==> default: Saving VM state and suspending execution…
9.恢复虚拟机
要在当前虚拟环境中恢复挂起的VM,请运行:
$vagrant resume
输出示例:
==> default: Resuming suspended VM... ==> default: Booting VM... ==> default: Waiting for machine to boot. This Jan take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key ==> default: Machine booted and ready! ==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision` ==> default: flag to force provisioning. Provisioners marked to run always will still run.
10.重新启动虚拟机
要重新启动正在运行的VM,请执行以下操作:
$vagrant reload
此命令将正常关闭虚拟机,然后重新启动它。
11.停止/关闭虚拟机
要停止或者关闭正在运行的VM,请执行以下操作:
$vagrant halt
12.删除虚拟机
要停止和删除虚拟机的所有跟踪,请运行:
$vagrant destroy
键入“ y”,然后按Enter键以删除虚拟机。
如果要删除虚拟机而不进行任何确认,请运行:
$vagrant destroy -f
13.列出vagrant 的盒子
要列出与Vagrant一起安装的所有可用盒,请运行:
$vagrant box list
输出示例:
hashicorp/bionic64 (virtualbox, 1.0.282)
14.检查“vagrant ”框是否已过时
要检查我们在Vagrant环境中使用的框是否已过时,请转到Vagrant环境,例如:项目目录,
$cd myvagrants/
并运行:
$vagrant box outdated Checking if box 'hashicorp/bionic64' version '1.0.282' is up to date…
15.更新vagrant 框
如果有任何过时的框,我们可以像下面这样更新它们:
$vagrant box update
输出示例:
==> default: Checking for updates to 'hashicorp/bionic64' default: Latest installed version: 1.0.282 default: Version constraints: default: Provider: virtualbox ==> default: Box 'hashicorp/bionic64' (v1.0.282) is running the latest version.
16.下载vagrant 的盒子
除了使用“ vagrant init”命令外,我们还可以手动下载VM并编辑其vagrant文件,然后启动它。
要下载“vagrant ”盒子,请运行:
$vagrant box add bento/debian-10.5
上面的命令将下载Bento发布的Debian 10.5版本框。
系统将要求我们选择当前使用的提供程序(例如virtualbox或者vmware)。
只需选择一个,然后按Enter键即可下载该框。
输出示例:
==> box: Loading metadata for box 'bento/debian-10.5' box: URL: https://vagrantcloud.com/bento/debian-10.5 This box can work with multiple providers! The providers that it can work with are listed below. Please review the list and choose the provider you will be working with. 1) parallels 2) virtualbox 3) vmware_desktop Enter your choice: 2 ==> box: Adding box 'bento/debian-10.5' (v201708.16.0) for provider: virtualbox box: Downloading: https://vagrantcloud.com/bento/boxes/debian-10.5/versions/201708.16.0/providers/virtualbox.box Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com ==> box: Successfully added box 'bento/debian-10.5' (v201708.16.0) for 'virtualbox'
我们可以通过列出可用的框来进行验证:
$vagrant box list
输出示例:
bento/debian-10.5 (virtualbox, 201708.16.0) hashicorp/bionic64 (virtualbox, 1.0.282)
所有下载的Vagrant框都将在主机系统的“~/.vagrant.d/boxes”目录中提供。
17.使用游民箱
要使用新下载的框创建新的VM,请转至项目目录并初始化无所事事的环境:
$vagrant init
注意:在初始化新的Vagrant环境之前,请删除现有的Vagrantfile。
这将在当前目录中创建一个新的Vagrantfile。
编辑Vagrant文件并更新VM详细信息并替换以下行:
Vagrant.configure("2") do |config| [...] config.vm.box = "base" [...]
具有以下内容:
config.vm.box = "bento/debian-10.5"
我们可以通过添加以下几行来明确指定新VM的版本:
config.vm.box = "bento/debian-10.5" config.vm.box_version = "1.0"
我们甚至还可以指定下载URL:
config.vm.box = "bento/debian-10.5" config.vm.box_version = "1.0" config.vm.box_url = "https://app.vagrantup.com/bento/boxes/debian-10.5/"
现在,使用以下命令基于此Vagrantfile创建并启动VM:
$vagrant up
18.将Vagrant与Libvirt KVM提供程序一起使用
就像我已经说过的那样,Vagrant默认使用Virtualbox运行虚拟机。
也可以告诉Vagrant使用另一个提供程序(例如Libvirt KVM)来使用vagrant-libvirt插件运行VM。
该插件将Libvirt提供程序添加到Vagrant,并允许Vagrant通过Libvirt控制和配置计算机。
要了解如何将libvirt用作Vagrant提供程序,请参考以下教程:
- 如何在Libvirt KVM提供程序中使用Vagrant
19.在Vagrant中配置网络
为了提供来宾计算机和主机系统之间的网络访问,Vagrant提供了以下三个选项:
- 转发端口
- 专用网络(仅主机网络)
- 公共网络(桥接网络)
每个选项都有其自身的起伏。
我们可以按照以下教程中的说明配置任何或者全部的Vagrant网络选项:
- 如何在vagrant 中配置网络
20.删除“vagrant ”盒子
要删除一个无用的盒子,例如hashicorp/bionic64,请运行:
$vagrant box remove hashicorp/bionic64
键入“ y”,然后按Enter键以确认删除:
Box 'hashicorp/bionic64' (v1.0.282) with provider 'virtualbox' appears to still be in use by at least one Vagrant environment. Removing the box could corrupt the environment. We recommend destroying these environments first: default (ID: 20864823c72f45568d251070b5ce2661) Are you sure you want to remove this box? [y/N] y Removing box 'hashicorp/bionic64' (v1.0.282) with provider 'virtualbox'…
如果我们不知道盒的名称,只需运行以下命令以列出所有已安装的盒:
$vagrant box list
在这一阶段,我们将获得有关Vagrant及其用法的基本概念。