在KVM / VirtualBox / VMWare / Parallels上运行带有Vagrant的CentOS 8 VM

如何使用Vagrant创建CentOS 8 VM,如何使用Vagrant在KVM / VirtualBox / VMWare上运行CentOS 8,如何使用Vagrant使用Vagrant,Boxs管理CentOS 8虚拟机您可以使用称为的虚拟机映像来创建和配置可复制且可移植的开发环境。

借助Vagrant,您可以使用各种虚拟化平台(例如VirtualBox,KVM,VMware,Hyper-V和e.t.c)在几秒钟内设置开发环境。 CentOS 8已有一段时间了。如果您喜欢使用新技术并且已经开始从CentOS 8构建应用程序,则可以使用本指南来设置Vagrant Lab。

步骤1:安装Vagrant

请查看下面的指南。

如何在Fedora上安装Vagrant和VirtualBox

在Debian / Ubuntu / Kali Linux上安装Vagrant

步骤2:下载CentOS 8 Vagrant框

使用以下命令之一为您的虚拟环境下载“ Vagrant”框:

对于 虚拟机 使用者:

$ 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'!

对于 虚拟盒子 使用者:

$ 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'!

对于 平行线

$ 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'!

码头工人

$ 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)

步骤3:为CentOS 8创建Vagrantfile

首先创建一个Vagrant文​​件。

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

Vagrantfile的基本内容如下所示:

  • 虚拟机
# -*- 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
[[email protected] ~]$ 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"

[[email protected] ~]$ cat /etc/redhat-release 
CentOS Linux release 8.0.1905 (Core) 

[[email protected] ~]$ 
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

稍后启动虚拟机时,这将使您返回完全相同的状态。

执行后会破坏流浪者机器:

$ vagrant destroy

其他指南:

适用于初学者和专业人士的最佳Linux书籍

最佳CCNA R&S(200-125)认证准备

最佳LPIC-1和LPIC-2认证学习书

如何在OpenStack中创建CentOS 8 KVM映像模板

Sidebar