如何在CentOS 8上使用LibVirt KVM的VAGRANT

时间:2020-02-23 14:40:39  来源:igfitidea点击:

基于内核的虚拟机(KVM)是一种开源虚拟化技术,由大虚拟化和云项目(如OpenStack,CloudStack,Somirt等许多)采用。
它内置于Linux中,以将Linux服务器转换为运行多个虚拟机(VM)的虚拟机管理程序。
创建本教程以帮助新用户在CentOS 8和KVM管理程序上使用Vagrant来旋转和管理虚拟机生命周期。

如果是我们第一次使用VAGRANT,它是Hashicorp创建的开源工具,以使开发人员在任何虚拟化平台中构建和维护便携式软件开发环境 - KVM,VirtualBox,VMware,Parallels甚至码头容器。
本教程的主要要求是运行的CentOS 8服务器或者工作站。
如果我们有Fedora工作站本教程也将为我们工作。

我们早些时候完成了一个单独的文章,使用Libiver与KVM在其他Linux发行版上使用LibiVir,该分布也应该适用于CentOS 8.我注意到的唯一差异是安装Vagrant插件的KVM。
在CentOS 8上安装安装时,本教程将覆盖的一些额外依赖项。

第1步:在CentOS 8上安装KVM

在我们可以安装和使用安装之前,我们需要KVM虚拟化软件堆栈。
在开始安装之前,请确保将包括内核的所有系统包更新为最新版本。

sudo dnf -y update

完成更新后,重新启动系统。

sudo systemctl reboot

等待系统重新启动,然后在CentOS 8 Linux机器上安装KVM虚拟化工具。

sudo dnf install -y @virt virt-install libvirt-devel vim bash-completion

启动并启用LibVirtd服务。

sudo systemctl enable --now libvirtd

将用户添加到libvirt组

sudo usermod -aG libvirt $USER
newgrp libvirt

第2步:在CentOS 8上安装Vagrant

下一个软件组件是流浪者。
我们将使用的一些依赖项是构建工具,我们可以轻松使用以下DNF命令安装。

sudo dnf groupinstall "Development Tools" -y 
sudo dnf -y install rsync gcc zlib-devel libvirt-devel

安装Ruby和Ruby开发包以使用VAGRANT。

sudo dnf install -y ruby ruby-devel

下载并安装最新版本的Vagrant 。

VERSION="2.2.9"
wget https://releases.hashicorp.com/vagrant/${VERSION}/vagrant_${VERSION}_x86_64.rpm
sudo dnf localinstall vagrant_${VERSION}_x86_64.rpm

点击Y键开始安装:

Dependencies resolved.
================================================================================
 Package         Architecture   Version              Repository            Size
================================================================================
Installing:
 vagrant         x86_64         1:2.2.9-1            @commandline          42 M
Transaction Summary
================================================================================
Install  1 Package
Total size: 42 M
Installed size: 122 M
Is this ok [y/N]: y

第3步:为libvirt安装Vagrant插件

安装KVM后,需要VAGRANT和工具,我们可以继续为KVM安装VAGRANT插件。
这将使我们能够将虚拟机从CentOS 8桌面或者服务器中的LibVirt提供程序中的特定图像进行旋转。

$CONFIGURE_ARGS="with-libvirt-include=/usr/include/libvirt with-libvirt-lib=/usr/lib64" vagrant plugin install vagrant-libvirt
Installing the 'vagrant-libvirt' plugin. This can take a few minutes...
Building native extensions. This could take a while...
Building native extensions. This could take a while...
Fetching fog-libvirt-0.7.0.gem
Fetching vagrant-libvirt-0.1.2.gem
Installed the plugin 'vagrant-libvirt (0.1.2)'!

如果由于此依赖项遇到故障安装,请使用GEM命令安装Nokogiri库。

$gem install nokogiri
Fetching: nokogiri-1.10.10.gem (100%)
Building native extensions. This could take a while...
Successfully installed nokogiri-1.10.10
Parsing documentation for nokogiri-1.10.10
Installing ri documentation for nokogiri-1.10.10
Done installing documentation for nokogiri after 2 seconds
1 gem installed

确保安装成功。
对于遇到的任何错误,我们可以在评论部分分享,然后我们了解我们如何。

第4步:添加LibVirt Vagrant Boxes

框是vagrant环境的包格式。
任何在任何平台上的任何平台上都可以使用一个盒子,这些平台支持促进相同的工作环境。

要使用我们刚刚安装的提供程序,我们需要为LibVirt提供程序构建的Vagrant Box。
我们可以探索Vagrant Cloud中提供的所有框以查找匹配项。

在我的用例中,我需要很少的框,我可以用创建的Vagrant Box实用程序提取,以提供管理框的所有功能。

--- Add CentOS 8 box --
$vagrant box add centos/8 --provider=libvirt
--- Add CentOS 7 box --
$vagrant box add centos/7 --provider=libvirt
--- Add Ubuntu 20.04 box --
$vagrant box add generic/ubuntu2004 --provider=libvirt
--- Add Ubuntu 18.04 box --
$vagrant box add generic/ubuntu1804 --provider=libvirt
--- Add Fedora 32 box --
$vagrant box add generic/fedora32 --provider=libvirt

我们可以使用以下命令获取所有可用的VAGRANT框的列表:

$vagrant box list
centos/7           (libvirt, 2004.01)
centos/8           (libvirt, 1905.1)
generic/ubuntu2004 (libvirt, 3.0.20)

使用Vagrant和Libvirt上CentOS 8运行虚拟机

VAGRANTFILE描述了项目所需的机器类型,以及如何配置和配置这些计算机。

我们将创建一个可用于运行的简单vagrantfile:

$vim Vagrantfile

要启动虚拟机运行:

$vagrant up

预期

Bringing machine 'default' up with 'libvirt' provider...
==> default: Registering VM image from the base box 'centos/8'...
==> default: Creating new virtual machine as a linked clone of the box image...
==> default: Unregistering the box VM image...
==> default: Setting the default configuration for VM...
==> default: Setting the name of the VM: cent8
==> default: Preparing network interfaces based on configuration...
    default: Adapter 0: shared
==> default: Clearing any previously set network interfaces...
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This Jan take a few minutes...
==> default: Waiting for machine to boot. This Jan take a few minutes...
    default: SSH address: 192.168.122.20:22
    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: Mounting shared folders...
    default: /vagrant => /Users/jmutai/vagrant/cent8

要启动ssh shell,请运行:

$vagrant ssh
This system is built by the Bento project by Chef Software
More information can be found at https://github.com/chef/bento
Last login: Fri Aug  7 00:29:28 2017
[Hyman@theitroad ~]$cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)
[Hyman@theitroad ~]$exit
logout

要在运行后停止服务器,请使用VAGRANT HALT命令。

$vagrant halt
==> default: Attempting graceful shutdown of VM...

要销毁虚拟机,请运行:

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

所有可用的VAGRANT命令选项包括:

--version      -- Prints the Vagrant version information
box            -- Box commands
connect        -- Connects to a shared, remote Vagrant environment
destroy        -- Destroys the vagrant environment
docker-logs    -- Shows Docker logs
docker-run     -- Run one-off commands against a Docker container
global-status  -- Reports the status of all active Vagrant environments on the system
halt           -- Halts the currently running vagrant environment
help           -- [TASK] Describe available tasks or one specific task
init           -- [box_name] [box_url] Initializes current folder for Vagrant usage
list-commands  -- Outputs all available Vagrant subcommands
login          -- Authenticates against a Vagrant Cloud server to access protected boxes
package        -- Packages a vagrant environment for distribution
plugin         -- Manage plugins
provision      -- Run the provisioner
push           -- Deploys code in this environment to a configured destination
rdp            -- Connects to machine via RDP
reload         -- Reload the vagrant environment
resume         -- Resumes a suspend vagrant environment
rsync          -- Syncs rsync synced folders to remote machine
rsync-auto     -- Syncs rsync synced folders automatically when files change
share          -- Shares the Vagrant environment and allows remote access
ssh            -- SSH into the currently running environment
ssh-config     -- Outputs .ssh/config valid syntax for connecting to this environment via ssh
status         -- Shows the status of the current Vagrant environment
suspend        -- Suspends the currently running vagrant environment
up             -- Creates the vagrant environment
version        -- Prints the currently installed Vagrant version and checks for new updates