在KVM/VirtualBox/VMWare/Parallels上使用Vagrant运行CentOS 8 VM

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

如何使用Vagrant创建CentOS 8 VM?如何使用Vagrant在KVM/VirtualBox/VMWare上运行CentOS 8?可以使用Vagrant管理CentOS 8虚拟机吗?通过Vagrant,我们可以使用称为Boxes的虚拟机镜像来创建和配置可复制的可移植开发环境。

使用Vagrant,我们可以在几秒钟内在各种虚拟化平台上设置开发环境,例如VirtualBox,KVM,VMware,Hyper-V等。 CentOS 8已有一段时间了。如果我们喜欢使用新技术并且已经从CentOS 8开始构建应用程序,则可以使用本教程来设置Vagrant Lab。

安装Vagrant

查看下面的教程:

如何在Fedora上安装Vagrant和VirtualBox

在Debian/Ubuntu/Kali Linux上安装Vagrant

第2步:下载CentOS 8 Vagrant框

使用以下命令之一为虚拟化环境下载Vagrant框:

对于KVM用户:

$vagrant box add centos/8 --provider=libvirt

==> box: Loading metadata for box 'centos/8'
    box: URL: https://vagrantcloud.com/centos/8
==> box: Adding box 'centos/8' (v1905.1) for provider: libvirt
    box: Downloading: https://vagrantcloud.com/centos/boxes/8/versions/1905.1/providers/libvirt.box
    box: Download redirected to host: cloud.centos.org
    box: Progress: 48% (Rate: 1305k/s, Estimated time remaining: 0:04:05)^    box: Calculating and comparing box checksum...
==> box: Successfully added box 'centos/8' (v1905.1) for 'libvirt'!

对于VirtualBox用户:

$vagrant box add centos/8 --provider=virtualbox
==> box: Loading metadata for box 'centos/8'
    box: URL: https://vagrantcloud.com/centos/8
==> box: Adding box 'centos/8' (v1905.1) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/centos/boxes/8/versions/1905.1/providers/virtualbox.box
    box: Download redirected to host: cloud.centos.org
    box: Calculating and comparing box checksum...
==> box: Successfully added box 'centos/8' (v1905.1) for 'virtualbox'!

对于Parallels:

$vagrant box add generic/centos8 --provider=parallels
==> box: Loading metadata for box 'generic/centos8'
    box: URL: https://vagrantcloud.com/generic/centos8
==> box: Adding box 'generic/centos8' (v2.0.6) for provider: parallels
    box: Downloading: https://vagrantcloud.com/generic/boxes/centos8/versions/2.0.6/providers/parallels.box
    box: Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com
==> box: Successfully added box 'generic/centos8' (v2.0.6) for 'parallels'!

VMware:

$vagrant box add generic/centos8 --provider=vmware_desktop
==> box: Loading metadata for box 'generic/centos8'
    box: URL: https://vagrantcloud.com/generic/centos8
==> box: Adding box 'generic/centos8' (v2.0.6) for provider: vmware_desktop
    box: Downloading: https://vagrantcloud.com/generic/boxes/centos8/versions/2.0.6/providers/vmware_desktop.box
    box: Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com
==> box: Successfully added box 'generic/centos8' (v2.0.6) for 'vmware_desktop'!

Docker:

$vagrant box add generic/centos8 --provider=docker
==> box: Loading metadata for box 'generic/centos8'
    box: URL: https://vagrantcloud.com/generic/centos8
==> box: Adding box 'generic/centos8' (v2.0.6) for provider: docker
    box: Downloading: https://vagrantcloud.com/generic/boxes/centos8/versions/2.0.6/providers/docker.box
    box: Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com
==> box: Successfully added box 'generic/centos8' (v2.0.6) for 'docker'!

列出可用的CentOS 8框:

$vagrant box list
centos/8        (libvirt, 1905.1)
centos/8        (virtualbox, 1905.1)
generic/centos8 (parallels, 2.0.6)
generic/centos8 (vmware_desktop, 2.0.6)
generic/centos8 (docker, 2.0.6)

为CentOS 8创建Vagrantfile

首先创建一个Vagrant文件。

$mkdir -p ~/vagrant/centos8 && cd ~/vagrant/centos8
$vim Vagrantfile

Vagrantfile的基本内容如下所示:KVM

# -*- mode: ruby -*
# vi: set ft=ruby :

ENV['VAGRANT_DEFAULT_PROVIDER'] = 'libvirt'

Vagrant.configure("2") do |config|

  ##### DEFINE VMS #####
  config.vm.define "centos8" do |config|
  config.vm.hostname = "centos8"
  config.vm.box = "centos/8"
  config.vm.box_check_update = false
  end
  config.vm.provider :libvirt do |v|
    v.memory = 1024
    v.cpus = 2
  end
end

虚拟盒子

# -*- mode: ruby -*
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "centos/8"
  config.vm.box_check_update = false
  config.vm.hostname = "centos8"
  config.vm.provider "virtualbox" do |vb|
  # Display the VirtualBox GUI when booting the machine
     vb.gui = false
     vb.memory = "2048"
     vb.cpus = 2
  end
end

对于其他提供商,请访问官方的Vagrant文档。创建文件后,通过运行以下命令启动VM:

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

尝试ssh:

vagrant ssh
[theitroad@localhost ~]$cat /etc/os-release 
NAME="CentOS Linux"
VERSION="8 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="8"

[theitroad@localhost ~]$cat /etc/redhat-release 
CentOS Linux release 8.0.1905 (Core) 

[theitroad@localhost ~]$
logout
Connection to 192.168.122.2 closed.

如果要销毁VM,请使用:

$vagrant destroy   
    centos8: Are you sure you want to destroy the 'centos8' VM? [y/N] y
==> centos8: Removing domain...

每当我们对Vagrantfile进行更改时,请重新启动计算机以使更改生效。

$vagrant reload

要停止实例,请使用:

$vagrant halt

如果要在停止时保存虚拟机的当前状态,请使用:

$vagrant suspend

这样,我们将在以后启动VM时返回完全相同的状态。

运行时销毁Vagrant机器:

$vagrant destroy