如何在Linux上的Libvirt中使用Vagrant

时间:2020-02-23 14:31:29  来源:igfitidea点击:

在本教程中,我们将研究如何在Linux上将Vagrant与Libvirt一起使用。
Vagrant是用Ruby编写的开源软件,可构建和维护可移植的虚拟软件开发环境,例如适用于VirtualBox,Hyper-V,Docker容器,VMware,Libvirt和AWS。
Vagrant使创建,停止和销毁虚拟机变得容易。

本地计算机上最常用的Vagrant是使用VirtualBox提供程序。
如果我们是KVM和QEMU用户,那么本文适合我们。
与Virtualbox相比,已知KVM具有更好的性能和较小的资源利用率。

在Linux上与Libvirt一起Vagrant 准备工作:

已安装Vagrant已安装Libvirt和QEMU-KVM已为Vagrant安装libvirt插件

为了在Linux上安装KVM,我们已经为我们准备了一些文章。
如何在RHEL/CentOS 8,Fedora,Arch Linux,CentOS,Ubuntu/Debian,SLES上安装KVM

对于Vagrant安装,我们有:如何在Fedora上安装Vagrant和Virtualbox在Ubuntu 18.04/Debian 9和Kali Linux上安装最新的Vagrant

为Vagrant安装Vagrant插件

一旦安装了Vagrant和KVM,就应该准备安装libvirt插件,以便可以开始使用Vagrant管理KVM虚拟机。

$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...
Installed the plugin 'vagrant-libvirt (0.0.45)'!

如果遇到如下错误:

ERROR: Failed to build gem native extension.
current directory: /home/jmutai/.vagrant.d/gems/2.5.1/gems/nokogiri-1.8.4/ext/nokogiri
/usr/bin/ruby -r ./siteconf20160704-25314-14hvlbq.rb extconf.rb
checking if the C compiler accepts ... yes
Building nokogiri using system libraries.
pkg-config could not be used to find libxml-2.0
Please install either `pkg-config` or the pkg-config gem per
gem install pkg-config -v "~> 1.1"

然后运行:

$gem install nokogiri
$vagrant plugin install pkg-config

然后重试安装插件。

$vagrant plugin install vagrant-libvirt

安装完成后,我们可以使用以下命令确认已安装插件:

$vagrant plugin list
vagrant-libvirt (0.0.45)

下载Vagrant

Libvirt的Vagrant框是一个tar存档,其中包含3个文件。
基本的VagrantFileTh元数据.json文件QCOW2图像Repo。
它可以轻松入门。
在本示例中,我们将使用现成的模板。
让我们添加CentOS 7和CentOS 6框。

$vagrant box add centos/7 --provider=libvirt
==> box: Loading metadata for box 'centos/7'
 box: URL: https://vagrantcloud.com/centos/7
==> box: Adding box 'centos/7' (v1902.01) for provider: libvirt
$vagrant box add centos/6 --provider=libvirt

添加Ubuntu 18.04 Vagrant框:

$vagrant box add generic/ubuntu1804 --provider=libvirt

检查本地显示的框列表。

$vagrant box list   
centos/7             (libvirt, 1902.01)
fedora/29-cloud-base (libvirt, 29.20161024.1)
generic/ubuntu1804   (libvirt, 1.9.8)

创建虚拟机Vagrantfile

Vagrant需要一个配置文件来获取要创建的VM的详细信息和设置。
让我们创建一个VM Vagrantfile。

$mkdir ~/vagrant-vms
$cd ~/vagrant-vms

创建一个Vagrantfile,其内容类似于以下内容:

# -*- mode: ruby -*
# vi: set ft=ruby :
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'libvirt'

Vagrant.configure("2") do |config|
  ##### DEFINE VM #####
  config.vm.define "cent-01" do |config|
  config.vm.hostname = "cent-01"
  config.vm.box = "centos/7"
  config.vm.box_check_update = false
  config.vm.network "private_network", ip: "192.168.18.9"
  config.vm.provider :libvirt do |v|
    v.memory = 1024
    end
  end
end

要启动虚拟机,请运行:

$vagrant up     
Bringing machine 'cent-01' up with 'libvirt' provider...
==> cent-01: Creating image (snapshot of base box volume).
==> cent-01: Creating domain with the following settings...
==> cent-01:  -- Name:              centos-01_cent-01
==> cent-01:  -- Domain type:       kvm
==> cent-01:  -- Cpus:              1
==> cent-01:  -- Feature:           acpi
==> cent-01:  -- Feature:           apic
==> cent-01:  -- Feature:           pae
==> cent-01:  -- Memory:            1024M
==> cent-01:  -- Management MAC:    
==> cent-01:  -- Loader:            
==> cent-01:  -- Nvram:             
==> cent-01:  -- Base box:          centos/7
==> cent-01:  -- Storage pool:      default
==> cent-01:  -- Image:             /var/lib/libvirt/images/centos-01_cent-01.img (41G)
==> cent-01:  -- Volume Cache:      default
==> cent-01:  -- Kernel:            
==> cent-01:  -- Initrd:            
==> cent-01:  -- Graphics Type:     vnc
==> cent-01:  -- Graphics Port:     -1
==> cent-01:  -- Graphics IP:       127.0.0.1
==> cent-01:  -- Graphics Password: Not defined
==> cent-01:  -- Video Type:        cirrus
==> cent-01:  -- Video VRAM:        9216
==> cent-01:  -- Sound Type:	
==> cent-01:  -- Keymap:            en-us
==> cent-01:  -- TPM Path:          
==> cent-01:  -- INPUT:             type=mouse, bus=ps2
==> cent-01: Creating shared folders metadata...
==> cent-01: Starting domain.
==> cent-01: Waiting for domain to get an IP address...
==> cent-01: Waiting for SSH to become available...
    cent-01: 
    cent-01: Vagrant insecure key detected. Vagrant will automatically replace
    cent-01: this with a newly generated keypair for better security.
    cent-01: 
    cent-01: Inserting generated public key within guest...
    cent-01: Removing insecure key from the guest if it's present...
    cent-01: Key inserted! Disconnecting and reconnecting using new SSH key...
==> cent-01: Setting hostname...
==> cent-01: Configuring and enabling network interfaces...
    cent-01: SSH address: 192.168.121.159:22
    cent-01: SSH username: vagrant
    cent-01: SSH auth method: private key
==> cent-01: Rsyncing folder: /home/jmutai/hacks/vagrant/labs/centos-01/=> /vagrant

Vagrant将在主机系统上创建一个Linux桥。

$brctl show virbr1
bridge name	bridge id		STP enabled	interfaces
virbr1		8000.5254005351c7	yes		virbr1-nic
							vnet0
$ip addr show dev virbr1
8: virbr1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 52:54:00:53:51:c7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.121.1/24 brd 192.168.121.255 scope global virbr1
       valid_lft forever preferred_lft forever

运行virsh list来查看是否可获得VM列表。

$virsh list          
 Id   Name                State
----------------------------------
 3    centos-01_cent-01   running

要将SSH SSH到VM,请使用vagrant ssh命令。

$vagrant ssh
Last login: Fri Apr 19 07:40:17 2019 from 192.168.121.1
[Hyman@theitroad ~]$cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core)

要输出.ssh/config通过ssh连接到此环境的有效语法,请运行ssh-config命令。
我们需要将提供的输出放在~/.ssh/config目录下的ssh中。

$vagrant ssh-config
Host cent-01
  HostName 192.168.121.159
  User vagrant
  Port 22
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/jmutai/hacks/vagrant/labs/centos-01/.vagrant/machines/cent-01/libvirt/private_key
  IdentitiesOnly yes
  LogLevel FATAL

要将输出重定向到ssh配置文件,请使用:

vagrant ssh-config >>~/.ssh/config

然后使用ssh命令以上面配置的名称登录:

$ssh cent-01       
Last login: Fri Apr 19 07:40:42 2019 from 192.168.121.1
[Hyman@theitroad ~]$

要关闭VM,请运行:

$vagrant halt
 ==> cent-01: Halting domain…

要通过清除所有数据将VM设置为初始状态,请使用vagrant destroy:

$vagrant destroy
    cent-01: Are you sure you want to destroy the 'cent-01' VM? [y/N] y
 ==> cent-01: Removing domain…

建立自己的Vagrant box

我们需要安装打包程序才能正常工作。
检查:在Linux/FreeBSD/macOS/Windows上安装最新的Packer,然后克隆Bento Github存储库。

$cd ~/
$git clone https://github.com/chef/bento
$cd bento
$cd centos
$packer build -only qemu -var "headless=true" centos-7.6-x86_64.json
==> qemu: Gracefully halting virtual machine...
==> qemu: Converting hard drive...
==> qemu: Running post-processor: vagrant
==> qemu (vagrant): Creating Vagrant box for 'libvirt' provider
 qemu (vagrant): Copying from artifact: ../builds/packer-centos-7.6-x86_64-qemu/centos-7.6-x86_64
 qemu (vagrant): Compressing: Vagrantfile
 qemu (vagrant): Compressing: box.img
 qemu (vagrant): Compressing: metadata.json
Build 'qemu' finished.
==> Builds finished. The artifacts of successful builds are:
--> qemu: 'libvirt' provider box: ../builds/centos-7.6.libvirt.box

如果构建成功,则准备导入盒文件将位于存储库根目录的builds目录中。

$vagrant box add builds/centos-7.6.libvirt.box --name "centos-7.6"
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos-7.6' (v0) for provider: 
 box: Unpacking necessary files from: file:///home/jmutai/hacks/vagrant/labs/packer/bento/builds/centos-7.5.libvirt.box
==> box: Successfully added box 'centos-7.6' (v0) for 'libvirt'!

确认已安装box。

$vagrant box list
centos-7.6 (libvirt, 0)
centos/6 (libvirt, 1803.01)
centos/7 (libvirt, 1803.01)