原網(wǎng)址:http://blog.yfwz100.cn/archives/2016/10/30/cloud-kvm-setup.html
KVM 是一種全虛擬化技術(shù),由 Linux 內(nèi)核自身集成,市面上很多云服務(wù)提供商都是用該技術(shù)進(jìn)行資源的虛擬化,也是 OpenStack 等云計(jì)算架構(gòu)的虛擬化基礎(chǔ)。很不博客列出了 KVM 的安裝和使用過程谭梗,但是都不夠具體止潮,本博客在總結(jié)網(wǎng)絡(luò)博客的基礎(chǔ)上凶掰,收集整理自己遇到的坑把敢,以方便大家做參考寄摆。
安裝準(zhǔn)備
確定物理服務(wù)器支持虛擬化技術(shù):
grep vmx /proc/cpuinfo # Intel 系列
grep svm /proc/cpuinfo # AMD 系列
需要安裝 Qemu、KVM 等組件:
sudo apt-get install kvm qemu qemu-kvm libvirt-bin
如果需要安裝圖形界面技竟,還可以安裝:
sudo apt-get install virt-manager
安裝
進(jìn)行以下操作時(shí)冰肴,請(qǐng)注意當(dāng)前用戶擁有高級(jí)的讀寫權(quán)限。用 virsh 創(chuàng)建的虛擬機(jī)榔组,一般會(huì)賦予 kvm 用戶組讀寫的權(quán)限熙尉,因此,可以把當(dāng)前操作用戶加入到 kvm 組里搓扯。更簡單的辦法是使用 root 來執(zhí)行以下操作检痰。
-
新建硬盤鏡像:
qemu-img create -f qcow2 /var/lib/libvirt/images/test.qcow2 20G
在服務(wù)器上準(zhǔn)備好 OS 的鏡像文件,例如從 http://mirrors.ustc.edu.cn 上下載锨推。
-
使用 virt-instal 或 virsh 進(jìn)行遠(yuǎn)程安裝
-
使用命令行的安裝方式
virt-install --virt-type kvm --name=test--ram=4096 --vcpus=2 \ --os-type=linux \ --location=/root/rhel-server-7.0-x86_64-dvd.iso \ --disk path=/var/lib/libvirt/images/test.qcow2,format=qcow2 \ --network bridge:virbr0 \ --graphics none \ --extra-args='console=tty0 console=ttyS0,115200n8 serial'
-
使用 VNC 的方式進(jìn)行安裝:
virt-install --virt-type kvm --name=test --ram=1024 --vcpus=1 \ --os-type=linux \ --location=/root/rhel-server-7.0-x86_64-dvd.iso \ --disk /var/lib/libvirt/images/test.qcow2,format=qcow2 \ --network bridge:brx \ --graphics vnc,password=123456
顯示 VNC 端口
virsh vncdisplay test
網(wǎng)上也有人提到 /etc/libvirt/qemu.conf 中的需要解鎖
# vnc_listen="0.0.0.0"
然后重啟 libvirtd 服務(wù)
systemctl restart libvirtd
-
使用 virsh 來創(chuàng)建虛擬機(jī):
創(chuàng)建虛擬機(jī)描述文件铅歼,例如 ubuntu.xml ,內(nèi)容如下:
<domain type='kvm'> <name>ubuntu2</name> <memory>1048576</memory> <vcpu>1</vcpu> <os> <type arch='x86_64' machine='pc'>hvm</type> <boot dev='cdrom'/> <boot dev='hd'/> </os> <features> <acpi/> <apic/> <pae/> </features> <clock offset = 'localtime'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>destroy</on_crash> <devices> <emulator>/usr/bin/kvm</emulator> <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/home/zhi/qemu/ubuntu2.img'/> <target dev='hda' bus='ide'/> </disk> <disk type='file' device='cdrom'> <source file='/home/zhi/img/ubuntu-16.10-server-amd64.iso'/> <target dev='hdb' bus='ide'/> </disk> <interface type='bridge'> <source bridge='virbr0'/> </interface> <input type='tablet' bus='usb'/> <input type='mouse' bus='ps2'/> <graphics type ='vnc' port='-1' listen='0.0.0.0' keymap='en-us'/> </devices> </domain>
其中需要注意編輯 device='cdrom'/device='disk'/interface 這幾個(gè)標(biāo)簽的內(nèi)容换可。然后執(zhí)行
virsh define ubuntu.xml
即啟動(dòng)安裝過程椎椰,可以用 VNC Viewer 進(jìn)行遠(yuǎn)程安裝。
注:以上 3 個(gè)步驟選擇一種進(jìn)行操作即可沾鳄,其作用是等價(jià)的慨飘。其中前兩種需要安裝 virtinst 工具。
-
-
如果是通過 virbr0 這個(gè)網(wǎng)卡進(jìn)行操作的話(默認(rèn) virbr0 是 NAT 并使用 dnsmaq 來分配 IP 地址)译荞,可以通過以下命令查看生成的虛擬機(jī)的 IP 地址:
cat /var/lib/libvirt/dnsmasq/virbr0.status
注意其中的 hostname 對(duì)應(yīng)的 IP 地址瓤的。
啟動(dòng)
可以使用 virsh 來管理虛擬機(jī),其中比較常見的命令有
virsh start VM_ID # 啟動(dòng)虛擬機(jī)
shutdown VM_ID # 關(guān)閉虛擬機(jī)
destroy VM_ID # 強(qiáng)制關(guān)閉虛擬機(jī)
edit VM_ID # 更改虛擬機(jī)的配置
另外一個(gè)值得一提的功能是在線遷移(live migration)吞歼。
virsh migrate VM_ID DEST_URI --live
需要注意圈膏,在線遷移需要對(duì)方 QEMU 支持,最好在兩個(gè)相同版本的 QEMU 服務(wù)器之間遷移篙骡,否則容易出錯(cuò)稽坤。其中 DEST_URI 的寫法是 qemu+ssh://IP_ADDR/system ,詳細(xì)文檔見 virsh migrate --help
糯俗。
常見問題
-
error: internal error Attempt to migrate guest to the same host 00020003-0004-0005-0006-000700080009
應(yīng)該是服務(wù)器提供商的問題尿褪,重新生成一下 UUID :
sed -i "/#host_uuid/ahost_uuid = \"`uuidgen`\"" /etc/libvirt/libvirtd.conf
然后重啟 libvirtd 服務(wù):
service libvirt-bin restart
-
error: internal error: process exited while connecting to monitor: qemu-system-x86_64: -machine pc-i440fx-2.2,accel=kvm,usb=off: Unsupported machine type
這個(gè)錯(cuò)誤在動(dòng)態(tài)遷移(在線遷移,live migration)的時(shí)候會(huì)遇到叶骨,接收方的 QEMU 版本較低茫多,不支持該版本的虛擬機(jī)。
-
Cannot recv data: Value too large for defined data type
很可能是因?yàn)檎{(diào)用的某個(gè)程序忽刽、某個(gè)庫出現(xiàn)錯(cuò)誤了天揖,因?yàn)檫@個(gè)錯(cuò)誤很廣泛夺欲。我當(dāng)時(shí)遇到這個(gè)問題是因?yàn)?ssh 的鑰匙沒配置好。建議重新生成秘鑰今膊。