如何通过 Libvirt KVM Provider 使用 Vagrant

默认情况下,Vagrant 使用 Oracle VirtualBox 作为提供者。 如果您的系统中安装了多个提供程序(例如 Virtualbox 和 Libvirt),除非您明确提供特定的提供程序,否则它将始终使用 Virtualbox 启动 VM。 在这个简短的指南中,让我们看看如何在 Linux 上将 Vagrant 与 Libvirt KVM 提供程序一起使用。

流浪汉 不是独立的虚拟化提供商。 它依赖于其他虚拟化提供商,如 Virtualbox、Libvirt/KVM、Docker、VMWare 来创建和运行虚拟机。

Vagrant 的一大特点是用户永远不会被绑定到特定的虚拟化平台。 用户可以创建与多个虚拟化提供商一起工作的工作流。

目前,Vagrant 支持 30 多个提供商。 您可以查看支持的提供商的完整列表 这里.

1.在Linux上安装KVM

首先,您需要在 Linux 系统上安装 KVM。 我们已经记录了基于 DEB 的系统和基于 RPM 的系统的 KVM 安装步骤。 只需按照链接在您首选的 Linux 发行版上安装 KVM。

  • 在 CentOS 8 服务器上安装和配置 KVM
  • 在 Ubuntu 20.04 Headless Server 中安装和配置 KVM
  • 在 OpenSUSE Tumbleweed 中安装和配置 KVM

2.在Linux中安装vagrant-libvirt插件

为了在 KVM 上运行 Vagrant 虚拟机,您需要安装 vagrant-libvirt 插入。 该插件将 Libvirt 提供程序添加到 Vagrant,并允许 Vagrant 通过 Libvirt 控制和配置机器。

安装必要的依赖项 vagrant-libvirt 插入。

在 Ubuntu 上:

$ sudo apt install qemu libvirt-daemon-system libvirt-clients libxslt-dev libxml2-dev libvirt-dev zlib1g-dev ruby-dev ruby-libvirt ebtables dnsmasq-base

在 CentOS 上, Fedora:

$ sudo dnf install gcc libvirt libvirt-devel libxml2-devel make ruby-devel

现在,安装 vagrant-libvirt 插件使用命令:

$ vagrant plugin install vagrant-libvirt

你还需要安装 vagrant-mutate 将 vagrant box 转换为与不同的提供者一起工作的插件。

$ vagrant plugin install vagrant-mutate

3. 将 Vagrant 与 Libvirt KVM Provider 一起使用

3.1。 确保您要使用的 Vagrant 框支持 libvirt 提供程序。 去探索 libvirt 支持的 vagrant box,只需选择 “libvirt” Vagrant 中的选项 Cloud 存储库。

发现 libvirt 支持的 Vagrant 盒子

就本指南而言,我将使用 CentOS 7 机器。

3.2. 进入你的 Vagrant 项目目录并初始化 Vagrant 环境:

$ vagrant init centos/7

3.3. 接下来,运行以下命令来启动虚拟机:

$ vagrant up --provider=libvirt

这里, --provider=libvirt 选项明确告诉 Vagrant 使用 libvirt KVM 来运行虚拟机。 含义 – KVM 在这里充当默认提供程序。

样本输出:

Bringing machine 'default' up with 'libvirt' provider...
==> default: Box 'centos/7' could not be found. Attempting to find and install...
    default: Box Provider: libvirt
    default: Box Version: >= 0
==> default: Loading metadata for box 'centos/7'
    default: URL: https://vagrantcloud.com/centos/7
==> default: Adding box 'centos/7' (v2004.01) for provider: libvirt
    default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/2004.01/providers/libvirt.box
Download redirected to host: cloud.centos.org
    default: Calculating and comparing box checksum...
==> default: Successfully added box 'centos/7' (v2004.01) for 'libvirt'!
==> default: Uploading base box image as volume into Libvirt storage...
==> default: Creating image (snapshot of base box volume).
==> default: Creating domain with the following settings...
==> default:  -- Name:              myvagrants_default
==> default:  -- Domain type:       kvm
==> default:  -- Cpus:              1
==> default:  -- Feature:           acpi
==> default:  -- Feature:           apic
==> default:  -- Feature:           pae
==> default:  -- Memory:            512M
==> default:  -- Management MAC:    
==> default:  -- Loader:            
==> default:  -- Nvram:             
==> default:  -- Base box:          centos/7
==> default:  -- Storage pool:      default
==> default:  -- Image:             /var/lib/libvirt/images/myvagrants_default.img (41G)
==> default:  -- Volume Cache:      default
==> default:  -- Kernel:            
==> default:  -- Initrd:            
==> default:  -- Graphics Type:     vnc
==> default:  -- Graphics Port:     -1
==> default:  -- Graphics IP:       127.0.0.1
==> default:  -- Graphics Password: Not defined
==> default:  -- Video Type:        cirrus
==> default:  -- Video VRAM:        9216
==> default:  -- Sound Type:	
==> default:  -- Keymap:            en-us
==> default:  -- TPM Path:          
==> default:  -- INPUT:             type=mouse, bus=ps2
==> default: Creating shared folders metadata...
==> default: Starting domain.
==> default: Waiting for domain to get an IP address...
==> default: Waiting for SSH to become available...
    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: Rsyncing folder: /home/sk/myvagrants/ => /vagrant
在 Linux 上将 Vagrant 与 Libvirt KVM 提供程序一起使用

正如您在上述输出的第一行所见,Vagrant 使用“libvirt”提供程序来启动 CentOS 7 虚拟机。

3.4. 或者,您可以通过添加以下环境变量来告诉 Vagrant 永久使用 libvirt 作为默认提供程序。

export VAGRANT_DEFAULT_PROVIDER=libvirt

4.验证VM是否在Libvirt KVM中运行

4.1。 您可以从以下位置验证 CentOS 7 VM 是否真的在 Libvirt KVM 提供程序中运行 维尔什 命令行界面。

$ virsh list

样本输出:

 Id   Name                 State
------------------------------------
 2    myvagrants_default   running
使用 virsh 验证 VM 是否在 libvirt kvm 中运行使用 virsh 验证 VM 是否在 libvirt kvm 中运行

或者,使用 vagrant status 命令:

$ vagrant status

4.2. 您还可以从 KVM 管理 GUI 应用程序中验证它,例如 虚拟管理器.

使用 virt-manager 验证 VM 是否在 libvirt kvm 中运行使用 virt-manager 验证 VM 是否在 libvirt kvm 中运行

5. 故障排除

如果另一个 Vagrant box 已经在使用不同的提供者(例如 virtualbox),当你尝试启动新的 VM 时,你将看到以下错误消息:

$ vagrant up --provider libvirt
An active machine was found with a different provider. Vagrant
currently allows each machine to be brought up with only a single
provider at a time. A future version will remove this limitation.
Until then, please destroy the existing machine to up with a new
provider.

Machine name: default
Active provider: virtualbox
Requested provider: libvirt

如上面的输出中所述,Vagrant 可以一次仅使用一个提供程序运行 VM。 这个限制可能会在未来的版本中解决。

在使用不同的提供程序运行另一个 VM 之前,首先使用以下命令删除现有的活动机器:

$ vagrant destroy 

键入“y”并按 ENTER 删除默认的活动机器:

    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Destroying VM and associated drives...

现在使用新的提供程序启动 VM,即在这种情况下为 KVM:

$ vagrant up --provider libvirt

六,结论

在本指南中,我们学习了如何在 Linux 中将 Vagrant 与 libvirt KVM 提供程序一起使用。 我们还研究了如何验证虚拟机是否在 Libvirt KVM 中运行。

资源: