Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of using Vagrant and Libvirt on Linux systems

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article shows you what the method of using Vagrant and Libvirt on Linux system is, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

We will show you how to use Vagrant and Libvirt on the Linux operating system. To complete this article, you need to install Vagrant, then install Libvirt and QEMU-KVM, and install the libvirt plug-in for Vagrant.

Install the Vagrant plug-in for Vagrant

After installing Vagrant and KVM, you should be ready to install the libvirt plug-in so that you can start managing KVM virtual machines using Vagrant:

$vagrant plugin install vagrant-libvirtInstalling 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.43)'!

If you encounter the following error:

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. / siteconf20190225-25314-14hvlbq.rb extconf.rbchecking if the C compiler accepts. YesBuilding nokogiri using system libraries.pkg-config could not be used to find libxml-2.0Please install either `pkg- config`or the pkg-config gem pergem install pkg-config-v'~ > 1.1'

Then run:

$gem install nokogiri$ vagrant plugin install pkg-config then try to install the plug-in again: $vagrant plugin install vagrant-libvirtInstalling the 'vagrant-libvirt' plugin. This can take a few minutes...Fetching: excon-0.62.0.gem Fetching: formatador-0.2.5.gem Fetching: fog-core-1.43.0.gem Fetching: fog-json-1.2.0.gem Fetching: mini_portile2-2.3.0.gem Building native extensions. This could take a while...Fetching: fog-xml-0.1.3.gem Fetching: ruby-libvirt-0.7.1.gem Building native extensions. This could take a while...Fetching: fog-libvirt-0.5.0.gem (100%) Fetching: vagrant-libvirt-0.0.43.gem (100%) Installed the plugin 'vagrant-libvirt'!

After the installation is complete, you can confirm that the plug-in is installed using the following command:

$vagrant plugin list

Vagrant-libvirt (0.0.43)

Downloading Vagrant boxes:

Libvirt's Vagrant box is a tar archive with 3 files: basic VagrantFile, metadata.json file, and QCOW2 image.

In this example, we will use a prepared template to add CentOS 7 and CentOS 6 boxes:

$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' (v1803.01) for provider: libvirt$ vagrant box add centos/6-- provider=libvirt

Check the local boxes presents:

$vagrant box listcentos/6 (libvirt, 1803.01) centos/7 (libvirt, 1803.01) generic/ubuntu1604 (libvirt, 1.5.0)

Create VM Vagrantfile

Vagrant needs a configuration file to get the details and settings of the VM to be created. Let's create a VM Vagrantfile:

$mkdir ~ / vagrant-vms

$cd ~ / vagrant-vms

Create a Vagrantfile with content similar to the following:

#-*-mode: ruby-*-# vi: set ft=ruby: ENV ['VAGRANT_DEFAULT_PROVIDER'] =' libvirt'# Check required pluginsREQUIRED_PLUGINS_LIBVIRT =% w (vagrant-libvirt) exit unless REQUIRED_PLUGINS_LIBVIRT.all? Do | plugin | Vagrant.has_plugin? (plugin) | (puts' The # {plugin} plugin is required. Please install it with:'puts'$vagrant plugin install # {plugin} 'false) endVagrant.configure (' 2') do | config | # Rabbitmq VMconfig.vm.define 'rabbitmq-centos6' do | node | node.vm.hostname =' rabbitmq-server-01'node.vm.box = 'centos/6'node.vm.box_check_update = false#node.vm.synced_folder'.','/ vagrant',: disabled = > truenode.vm.network 'private_network' Ip: '192.168.18.15'node.vm.provider: libvirt do | domain | domain.memory = 512domain.nested = trueendendend

To start VM, run:

$vagrant up

Run the virsh list to see if you get the VM list:

$virsh list

As defined in Vagrantfile, a new bridge is created with the .1 IP address of the subnet defined, with a default mask of / 24:

$ip ad show dev virbr3

6: virbr3: mtu 1500 qdisc noqueue state UP group default qlen 1000

Link/ether 52:54:00:a6:63:05 brd ff:ff:ff:ff:ff:ff

Inet 192.168.18.1/24 brd 192.168.18.255 scope global virbr3

Valid_lft forever preferred_lft forever

You can also use the brctl command to view the bridge:

$brctl show

To ssh to VM, use the vagrant ssh command:

To output valid .ssh / config syntax to connect to this environment through ssh, run the ssh-config command and put the output provided in the ~ / .ssh / config directory into ssh:

$vagrant ssh-configHost rabbitmq-server-01HostName 192.168.121.122User vagrantPort 22UserKnownHostsFile / dev/nullStrictHostKeyChecking noPasswordAuthentication noIdentityFile / home/jmutai/hacks/vagrant/labs/rabbitmq-server/.vagrant/machines/rabbitmq-server-01/libvirt/private_keyIdentitiesOnly yesLogLevel FATAL

Then use the ssh command to log in with the name configured above:

Ssh rabbitmq-server-01Last login: Mon Feb 25 10:03:23 2019 from 192.168.121.1 [vagrant@rabbitmq-server-01 ~] $

To turn off VM, run:

$vagrant halt== > rabbitmq-centos6: Halting domain...$ virsh list-- all- centos_mysql-server-01 shut off- rabbitmq-server_rabbitmq-centos6 shut off- ubuntu-terraform shut off

To set VM to its initial state by clearing all data, use vagrant destroy:

$vagrant destroyrabbitmq-centos6: Are you sure you want to destroy the 'rabbitmq-centos6' VM? Rabbitmq-centos6: Removing domain...$ virsh list-- all

Set up your own Vagrant box

You need to install packer to work:

$wget https://releases.hashicorp.com/packer/1.3.4/packer_1.3.4_linux_amd64.zip$ unzip packer_1.3.4_linux_amd64.zipArchive: packer_1.3.4_linux_amd64.zipinflating: packer$ sudo cp packer / usr/local/bin$ type packerpacker is / usr/local/bin/packer

Then clone bento Github repo:

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

If the build is successful, the prepare import box file will be located in the builds directory of the repository root:

$vagrant box add builds/centos-7.5.libvirt.box-- name 'centos-7.5'== > box: Box file was not detected as metadata. Adding it directly...== > box: Adding box 'centos-7.5' (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.5' (v0) for'libvirtkeeper!

Confirm that box is installed:

Vagrant box listcentos-7.5 (libvirt, 0) centos/6 (libvirt, 1803.01) centos/7 (libvirt, 1803.01) what is a Linux system Linux is a free-to-use and free transmission UNIX-like operating system, is a POSIX-based multi-user, multi-tasking, multi-threading and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

The above content is what is the method of using Vagrant and Libvirt on Linux system. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report