kvm虛擬化linux
安裝kvm管理工具web
安裝:
yum install libvirt virt-install qemu-kvm -yshell
介紹:
libvirt服務:管理kvm虛擬機的生命週期
virt-install工具:建立安裝虛擬機
qemu-kvm工具:使用qemu-img爲虛擬機提供硬盤vim
安裝kvm虛擬機準備條件
在window上安裝TightVNC
tightvnc官網:http://www.tightvnc.comcentos
vnc是一個跨平臺的遠程桌面軟件,待會安裝kvm虛擬機系統的時候使用網絡
啓動libvirtd服務
systemctl start libvirtd.service
systemctl status libvirtd.servicedom
安裝kvm虛擬機
建議虛擬機內存不要低於1024M,不然安裝系統特別慢!工具
virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos7 --memory 1024 測試
--vcpus 1 --disk /opt/centos2.raw,format=raw,size=10 --cdrom /opt/CentOS-7-x86_64-DVD-1708.iso ui
--network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
執行添加虛擬機後立刻用tightvnc 鏈接到虛擬機進行系統安裝 10.0.0.11:5900默認端口 5900
環境準備
查看虛擬機列表
virsh list 運行的列表
virsh list --all 全部的虛擬機列表
啓動虛擬機
virsh start centos7 虛擬機名字centos7
關閉虛擬機
virsh shutdown centos7
備份配置文件
virsh dumpxml centos7 >centos7.xml 重定向到文件保存
刪除虛擬機
virsh undefine centos7
恢復虛擬機
virsh define centos7.xml
當磁盤名字或路徑改變時 不能正常啓動虛擬機,須要修改配置文件 使用新的路徑
不推薦使用vim修改 它不帶語法提示,推薦使用自帶的編輯命令
virsh edit centos7
搜索磁盤配置 修改 /disk
更改虛擬機名字
virsh domrename centos7 web01
掛起虛擬機
virsh suspend web01
恢復掛起狀態
virsh resume web01
查詢虛擬機vnc端口號
virsh vncdisplay web01
返回的是 :0 它是從5900一直遞增的 也能夠使用10.0.0.11:0 進行鏈接
開機啓動虛擬機
virsh autostart web01
須要保證libvirtd服務是開機啓動的
取消開機啓動
virsh autostart --disable web01
kvm虛擬機console登錄
在centos7的kvm虛擬機中執行:
grubby --update-kernel=ALL --args="console=ttyS0,115200n8"
reboot重啓生效
驗證宿主機鏈接虛擬機
virsh console web01 按回車 回到宿主機 Ctrl+] 中括號
思考
centos6的虛擬機如何實現console鏈接呢?
kvm虛擬磁盤格式轉換和快照管理
qemu-img的經常使用命令
建立虛擬磁盤
qemu-img create test.raw 10G
qemu-img create -f qcow2 test.qcow2 10G
查看虛擬磁盤信息
qemu-img info test.raw
調整虛擬磁盤容量大小
qemu-img resize test.raw +5G
磁盤格式轉換
qemu-img convert -f raw -O qcow2 test.raw oldboy.qcow2
快照管理
建立快照
virsh snapshot-create centos7
查看快照
virsh snapshot-list centos7
還原快照
virsh snapshot-revert centos7 --snapshotname 1516574134
刪除快照
virsh snapshot-delete centos7 --snapshotname 1516636570
kvm虛擬機的克隆
完整克隆
實現方法:
virt-clone -o web01 --auto-clone
連接克隆
實現方法:
shell腳本
克隆的步驟:
1:克隆虛擬磁盤文件
cp centos7.qcow2 web03.qcow2
2:生成新的虛擬機配置文件
name修改
uuid刪掉
disk路徑/opt/web03.qcow2
mac地址刪除
3:測試啓動
virsh define web01.xml
連接克隆實戰
qemu-img create -f qcow2 -b cetnos7.qcow2 web04.qcow2
virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web02
--memory 1024 --vcpus 1 --disk /opt/web04.qcow2,format=qcow2,size=10
--boot hd --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
kvm虛擬機的橋接網絡
建立橋接網絡
1:virsh iface-bridge eth0 br0
基於橋接網絡建立虛擬機
2:virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web04
--memory 1024 --vcpus 1 --disk /opt/web04.qcow2,format=qcow2,size=10 --boot hd
--network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole
將已有的虛機修改成橋接網絡
1:virsh edit centos7
<interface type='bridge'>
<mac address='52:54:00:55:aa:fa'/>
<source bridge='br0’/>
2:修改虛擬機ip地址
/etc/sysconfig/network-scripts/ifcfg-eth0
kvm虛擬機的熱添加技術
熱添加硬盤
建立硬盤
qemu-img create -f qcow2 centos7-add01.qcow2 5G
熱添加硬盤
virsh attach-disk web01 /opt/centos7-add01.qcow2 vdb --live --cache=none --subdriver=qcow2
添加網卡
virsh attach-interface web04 --type bridge --model virtio --source br0
熱添加cpu
安裝參數:
virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web04
--memory 1024 --vcpus 1,maxvcpus=4 --disk /opt/web04.qcow2,format=qcow2,size=10
--boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole
熱添加cpu
virsh setvcpus web04 --count=2
熱添加內存
安裝參數
virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web04
--memory 512,maxmemory=2048 --vcpus=1,maxvcpus=2
--disk /opt/web04.qcow2,format=qcow2,size=10 --boot hd --network bridge=br0
--graphics vnc,listen=0.0.0.0 --noautoconsole
熱添加內存
virsh setmem web04 1G
kvm虛擬機的熱遷移