日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

Linux libvirt目录,在Linux系统上使用Vagrant和Libvirt的方法

發(fā)布時(shí)間:2023/12/20 linux 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux libvirt目录,在Linux系统上使用Vagrant和Libvirt的方法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文我們將介紹如何在Linux操作系統(tǒng)上使用Vagrant和Libvirt,要完成本文需要先安裝Vagrant,然后安裝Libvirt和QEMU-KVM及為Vagrant安裝libvirt插件。

參考文章

1、在Linux上安裝Vagrant請(qǐng)參考:

2、在Linux上安裝KVM請(qǐng)參考:

為Vagrant安裝Vagrant插件

安裝Vagrant和KVM后,你應(yīng)該已準(zhǔn)備好安裝libvirt插件,以便可以使用Vagrant開始管理KVM虛擬機(jī):

$ 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.43)'!

如果遇到如下錯(cuò)誤:

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.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"

然后運(yùn)行:

$ gem install nokogiri

$ vagrant plugin install pkg-config

然后重試安裝插件:

$ vagrant plugin install vagrant-libvirt

Installing the 'vagrant-libvirt' plugin. This can take a few minutes...

Fetching: excon-0.62.0.gem (100%)

Fetching: formatador-0.2.5.gem (100%)

Fetching: fog-core-1.43.0.gem (100%)

Fetching: fog-json-1.2.0.gem (100%)

Fetching: mini_portile2-2.3.0.gem (100%)

Building native extensions. This could take a while...

Fetching: fog-xml-0.1.3.gem (100%)

Fetching: ruby-libvirt-0.7.1.gem (100%)

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 (0.0.43)'!

安裝完成后,可以使用以下命令確認(rèn)已安裝插件:

$ vagrant plugin list

vagrant-libvirt (0.0.43)

正在下載Vagrant boxes:

Libvirt的Vagrant box是一個(gè)包含3個(gè)文件的tar存檔:基礎(chǔ)VagrantFile、metadata.json文件、QCOW2圖片。

在這個(gè)例子中,我們將使用一個(gè)準(zhǔn)備好的模板,讓我們添加CentOS 7和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

檢查本地boxes presents:

$ vagrant box list

centos/6 (libvirt, 1803.01)

centos/7 (libvirt, 1803.01)

generic/ubuntu1604 (libvirt, 1.5.0)

創(chuàng)建VM Vagrantfile

Vagrant需要配置文件來(lái)獲取要?jiǎng)?chuàng)建的VM的詳細(xì)信息和設(shè)置,讓我們創(chuàng)建一個(gè)VM Vagrantfile:

$ mkdir ~/vagrant-vms

$ cd ~/vagrant-vms

創(chuàng)建一個(gè)內(nèi)容類似于以下內(nèi)容的Vagrantfile:

# -*- mode: ruby -*-

# vi: set ft=ruby :

ENV['VAGRANT_DEFAULT_PROVIDER'] = 'libvirt'

# Check required plugins

REQUIRED_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

)

end

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

# Rabbitmq VM

config.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 => true

node.vm.network "private_network", ip: "192.168.18.15"

node.vm.provider :libvirt do |domain|

domain.memory = 512

domain.nested = true

end

end

end

要啟動(dòng)VM,請(qǐng)運(yùn)行:

$ vagrant up

運(yùn)行virsh列表以查看是否會(huì)獲得VM列表:

$ virsh list

如Vagrantfile中所定義,將創(chuàng)建一個(gè)新橋,其中定義了子網(wǎng)的.1 IP地址,默認(rèn)掩碼為/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

你也可以使用brctl命令查看網(wǎng)橋:

$ brctl show

要ssh到VM,請(qǐng)使用vagrant ssh命令:

要輸出.ssh/config有效語(yǔ)法以通過(guò)ssh連接到此環(huán)境,請(qǐng)運(yùn)行ssh-config命令,需要將~/.ssh/config目錄下提供的輸出放到ssh中:

$ vagrant ssh-config

Host rabbitmq-server-01

HostName 192.168.121.122

User vagrant

Port 22

UserKnownHostsFile /dev/null

StrictHostKeyChecking no

PasswordAuthentication no

IdentityFile /home/jmutai/hacks/vagrant/labs/rabbitmq-server/.vagrant/machines/rabbitmq-server-01/libvirt/private_key

IdentitiesOnly yes

LogLevel FATAL

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

$ ssh rabbitmq-server-01

Last login: Mon Feb 25 10:03:23 2019 from 192.168.121.1

[vagrant@rabbitmq-server-01 ~]$

要關(guān)閉VM,請(qǐng)運(yùn)行:

$ 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

要通過(guò)清除所有數(shù)據(jù)將VM設(shè)置為其初始狀態(tài),請(qǐng)使用vagrant destroy:

$ vagrant destroy

rabbitmq-centos6: Are you sure you want to destroy the 'rabbitmq-centos6' VM? [y/N] y

==> rabbitmq-centos6: Removing domain...

$ virsh list --all

建立自己的Vagrant box

你需要安裝packer才能工作:

$ wget https://releases.hashicorp.com/packer/1.3.4/packer_1.3.4_linux_amd64.zip

$ unzip packer_1.3.4_linux_amd64.zip

Archive: packer_1.3.4_linux_amd64.zip

inflating: packer

$ sudo cp packer /usr/local/bin

$ type packer

packer is /usr/local/bin/packer

然后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: 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.5-x86_64-qemu/centos-7.5-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.5.libvirt.box

如果構(gòu)建成功,則準(zhǔn)備導(dǎo)入框文件將位于存儲(chǔ)庫(kù)根目錄的builds目錄中:

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

確認(rèn)已安裝box:

$ vagrant box list

centos-7.5 (libvirt, 0)

centos/6 (libvirt, 1803.01)

centos/7 (libvirt, 1803.01)

相關(guān)主題

總結(jié)

以上是生活随笔為你收集整理的Linux libvirt目录,在Linux系统上使用Vagrant和Libvirt的方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。