什么是Vagrant?

Vagrant 是一个免费的开源软件,用于轻松配置和编排虚拟软件开发环境。

这个虚拟环境被称为 Vagrant Box,它是可移植的,因此它可以在许多平台上使用而无需任何更改。
Vagrant 由 HashiCorp 开发并在 MIT 许可下分发。

Vagrant 使用一个名为 Vagrantfile 的声明性配置文件,它描述了所有的软件需求、包、操作系统配置、用户等。

Vagrant 最初与 Oracle VirtualBox 捆绑在一起。

但后来 Vagrant 增加了对其他虚拟化平台的支持,如 VMWare 和 KVM。

在技术术语中,这些虚拟化平台(VirtualBox、KVM、VMWare 等)被称为 Providers ,而 Vagrant 本身被称为 Provisioner 。

在 CentOS 8 上安装 Ruby

Vagrant 是用 Ruby 编写的,它需要 Ruby 开发工具来编译 Vagrant 扩展和插件。
因此,在安装 Vagrant 之前,我们需要在 CentOS 8 上安装 Ruby。

Ruby 2.5.5 在 AppStream yum 存储库中可用,我们可以使用 dnf 命令安装它。

[root@vagrant-02 ~]# dnf install -y ruby ruby-devel

检查已安装的 ruby 命令的版本。

[root@vagrant-02 ~]# ruby -v
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]
欢迎 on it road

在 CentOS 8 上安装 Nokogiri

Ruby 需要 Nokogiri 库来解析 HTML 和 XML,同时编译 Vagrant 扩展。

我们可以从 EPEL(企业 Linux 的另外包)或者使用 RubyGem 包管理器安装 nokogiri。

因为,我们已经在安装 Ruby 时安装了 RubyGem 包管理器,因此,我们可以使用它来安装 nokogiri gem。

但首先我们必须按如下方式安装 nokogiri 依赖项。

[root@vagrant-02 ~]# dnf install -y make gcc rpm-build ruby-devel zlib-devel

现在,使用 gem 命令安装 nokogiri 库。

[root@vagrant-02 ~]# gem install nokogiri
Fetching: mini_portile2-2.4.0.gem (100%)
Successfully installed mini_portile2-2.4.0
Fetching: nokogiri-1.10.9.gem (100%)
Building native extensions. This could take a while...
Successfully installed nokogiri-1.10.9
2 gems installed

使用 Vagrant Boxes 创建 KVM 虚拟机

Boxes 是 Vagrant 环境的包格式。
任何人都可以在 Vagrant 支持的任何平台上使用 Box 来创建相同的工作环境。

我们可以在 Vagrant Cloud 上搜索所需的框。

我们正在使用 vagrant 命令下载 centos/7 盒子。

[root@vagrant-02 ~]# 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' (v1905.1) for provider: libvirt
    box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1905.1/providers/libvirt.box
==> box: Box download is resuming from prior download progress
Download redirected to host: cloud.centos.org
==> box: Successfully added box 'centos/7' (v1905.1) for 'libvirt'!

Vagrant 还需要 rsync 实用程序,因此使用 dnf 命令安装它。

[root@vagrant-02 centos7]# dnf install -y rsync
...
Installed:
  rsync-3.1.3-6.el8.x86_64
Complete!

为我们的 CentOS 7 虚拟机创建一个项目目录。

[root@vagrant-02 ~]# mkdir centos7
[root@vagrant-02 ~]# cd centos7

使用 vagrant 命令初始化我们的 CentOS 7 项目。
此命令将在我们的项目目录中创建一个 Vagrantfile,可用于创建所需的虚拟机。
我们可以根据要求自定义此 Vagrantfile。

[root@vagrant-02 centos7]# vagrant init centos/7
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.

如下创建并启动 CentOS 7 虚拟机。

[root@vagrant-02 centos7]# vagrant up
Bringing machine 'default' up with 'libvirt' provider...
==> default: Checking if box 'centos/7' version '1905.1' is up to date...
==> default: Creating image (snapshot of base box volume).
==> default: Creating domain with the following settings...
==> default:  -- Name:              centos7_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/centos7_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: Configuring and enabling network interfaces...
    default: SSH address: 192.168.121.18:22
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Rsyncing folder: /root/centos7/ => /vagrant

检查我们的 CentOS 7 虚拟机的状态。

[root@vagrant-02 centos7]# virsh list
 Id    Name                           State
---------------------------------------------------
 2     centos7_default                running

我们已经使用 Vagrant box 和 KVM provider 成功创建了我们的第一个虚拟机。

在 CentOS 8 上安装带有 KVM Provider 的 Vagrant

什么是KVM

KVM(基于内核的虚拟机)是 Linux 内核中的一个虚拟化模块,它允许内核作为 Hypervisor 运行。

在 CentOS 7 或者更高版本中,KVM 已经取代了传统的 Xen Kernel,现在大多数 Linux 发行版都使用 KVM 进行虚拟化。

KVM 是免费和开源的,我们可以使用它来配置高性能和功能丰富的 CentOS 虚拟化服务器。

为 KVM Provider 安装 Vagrant 插件

我们需要安装一个 Vagrant 插件才能使用 KVM 提供程序。

安装 vagrant-libvirt 插件需要 libvirt-devel 包,我们使用 dnf 命令安装它。

[root@vagrant-02 ~]# dnf install -y libvirt-devel
...
Installed:
  libvirt-devel-4.5.0-35.3.module_el8.1.0+297+df420408.x86_64
Complete!

现在,安装 vagrant-libvirt 插件如下。

[root@vagrant-02 ~]# 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.0.45.gem
Installed the plugin 'vagrant-libvirt (0.0.45)'!

我们已经在 CentOS 8 上成功安装了带有 KVM 的 Vagrant。

在 CentOS 8 上安装 Libvirt 和 KVM/QEMU

由于我们计划将 Vagrant 与 KVM 提供程序一起使用,因此,我们需要在 CentOS 8 服务器上安装 libvirt 和 KVM/QEMU 软件包。

[root@vagrant-02 ~]# dnf install -y @virt virt-install

在这里,我们已经在 CentOS 8 上安装了 libvirt 和 KVM/QEMU,但有关详细的分步教程,我们可以参考我们之前的文章:安装基于 KVM 的 CentOS 虚拟化服务器。

在 CentOS 8 上安装 Vagrant

在撰写本文时,最新的稳定版本是 Vagrant 2.2.9 。
我们可以从 Vagrant 网站下载适用于我们各自操作系统的 Vagrant 2.2.9.

复制所需的 Vagrant RPM 的 URL 并使用 wget 命令下载它。

[root@vagrant-02 ~]# wget https://releases.hashicorp.com/vagrant/2.2.9/vagrant_2.2.9_x86_64.rpm

使用 dnf 命令在 CentOS 8 上安装 Vagrant RPM。

[root@vagrant-02 ~]# dnf install -y vagrant_2.2.9_x86_64.rpm

检查已安装的 vagrant 命令的版本。

[root@vagrant-02 ~]# vagrant --version
Vagrant 2.2.9

Vagrant 已经安装在 CentOS 8 服务器上。

日期:2020-09-17 00:16:30 来源:oir作者:oir