Linux libvirt目录,在Linux系统上使用Vagrant和Libvirt的方法
本文我們將介紹如何在Linux操作系統上使用Vagrant和Libvirt,要完成本文需要先安裝Vagrant,然后安裝Libvirt和QEMU-KVM及為Vagrant安裝libvirt插件。
參考文章
1、在Linux上安裝Vagrant請參考:
2、在Linux上安裝KVM請參考:
為Vagrant安裝Vagrant插件
安裝Vagrant和KVM后,你應該已準備好安裝libvirt插件,以便可以使用Vagrant開始管理KVM虛擬機:
$ 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)'!
如果遇到如下錯誤:
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"
然后運行:
$ 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)'!
安裝完成后,可以使用以下命令確認已安裝插件:
$ vagrant plugin list
vagrant-libvirt (0.0.43)
正在下載Vagrant boxes:
Libvirt的Vagrant box是一個包含3個文件的tar存檔:基礎VagrantFile、metadata.json文件、QCOW2圖片。
在這個例子中,我們將使用一個準備好的模板,讓我們添加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)
創建VM Vagrantfile
Vagrant需要配置文件來獲取要創建的VM的詳細信息和設置,讓我們創建一個VM Vagrantfile:
$ mkdir ~/vagrant-vms
$ cd ~/vagrant-vms
創建一個內容類似于以下內容的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
要啟動VM,請運行:
$ vagrant up
運行virsh列表以查看是否會獲得VM列表:
$ virsh list
如Vagrantfile中所定義,將創建一個新橋,其中定義了子網的.1 IP地址,默認掩碼為/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命令查看網橋:
$ brctl show
要ssh到VM,請使用vagrant ssh命令:
要輸出.ssh/config有效語法以通過ssh連接到此環境,請運行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 ~]$
要關閉VM,請運行:
$ 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
要通過清除所有數據將VM設置為其初始狀態,請使用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
如果構建成功,則準備導入框文件將位于存儲庫根目錄的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'!
確認已安裝box:
$ vagrant box list
centos-7.5 (libvirt, 0)
centos/6 (libvirt, 1803.01)
centos/7 (libvirt, 1803.01)
相關主題
總結
以上是生活随笔為你收集整理的Linux libvirt目录,在Linux系统上使用Vagrant和Libvirt的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Eureka自我保护机制、健康检查的作用
- 下一篇: python 从菜鸟到高手 .pdf 下