CentOS下KVM虛擬機器的建立、管理與遷移
kvm虛擬機器管理
一、環境
role hostname ip OS
kvm_server target 192.168.32.40/24 rhel6.0_x64
vir_guest1 node4 192.168.32.34/24 rhel5.5_i386
vir_guest2 node5 192.168.32.35/24 rhel5.5_i386
manager manager 192.168.32.33/24 rhel5.5_i386
ESXi ESXi 192.168.2.251/24 ESXi 3.5
二、安裝kvm
[root@target ~]# yum install -y qemu-kvm.x86_64 qemu-kvm-tools.x86_64 #安裝kvm核心
[root@target ~]# yum install libvirt.x86_64 libvirt-cim.x86_64 libvirt-client.x86_64 libvirt-java.noarch libvirt-python.x86_64 #安裝virt管理工具
[root@target ~]# modprobe kvm #載入kvm核心
[root@target ~]# modprobe kvm-intel #intel cpu載入kvm-intel核心,要支援全虛擬化,必須開啟;要求cpu支援,通過bios可設定。
[root@target ~]# modprobe kvm-amd #amd cpu載入kvm-intel
[root@target ~]# modprobe -ls | grep kvm #查看核心是否開啟
kernel/arch/x86/kvm/kvm.ko
kernel/arch/x86/kvm/kvm-intel.ko
kernel/arch/x86/kvm/kvm-amd.ko
[root@target ~]# modprobe -ls | grep kvm-intel
kernel/arch/x86/kvm/kvm-intel.ko
三、安裝guest虛擬機器
1、直接通過virt-manager安裝、管理虛擬機器(略)
2、通過命令列安裝guest虛擬機器
[root@target ~]# yum install virt-viewer #開啟圖形控制台安裝虛擬客戶機需要,
[root@target ~]# virt-install \ #安裝選項可用virt-install --help查看
--name node4 \ #虛擬機器名
--ram=1024 \ #分配記憶體大小,MB
--arch=x86_64 \ #類比的CPU 構架
--vcpus=1 \ #配置虛擬機器的vcpu 數目
--check-cpu \ #檢查確定vcpu是否超過物理 CPU數目,如果超過則發出警告。
--os-type=linux \ #要安裝的作業系統類型,例如:'linux'、'unix'、'windows'
--os-variant=rhel5 \ #作業系統版本,如:'fedora6', 'rhel5', 'solaris10', 'win2k'
--disk path=/virhost/node7.img,device=disk,bus=virtio,size=20,sparse=true \ #虛擬機器所用磁碟或鏡像檔案,size大小G
--bridge=br0 \ #指定網路,採用透明橋接器
--noautoconsole \ #不自動開啟控制台
--pxe #網路安裝
四、利用virsh對虛擬機器管理
1、開關機
[root@target ~]# virsh start node4 #開機
[root@target ~]# virsh create /etc/libvirt/qemu/node4.xml #直接通過主機配置文檔啟動主機
[root@target ~]# virsh shutdown node4 #關機
[root@target ~]# virsh destroy node4 #強制關閉電源
[root@target ~]# virsh list --all #查看虛擬機器狀態
Id 名稱 狀態
----------------------------------
18 node4 running
- node5 關閉
- win8 關閉
2、添加刪除虛擬機器
[root@target ~]# virsh define /etc/libvirt/qemu/node5.xml #根據主機配置文檔添加虛擬機器
[root@target ~]# virsh list --all #node5已經添加
Id 名稱 狀態
----------------------------------
18 node4 running
- node5 關閉
- win8 關閉
[root@target ~]# virsh undefine node5 #移除虛擬機器
[root@target ~]# ls /etc/libvirt/qemu
networks node4.xml win8.xml
[root@target ~]# virsh list --all #node5已經被移除
Id 名稱 狀態
----------------------------------
18 node4 running
- win8 關閉
3、遠端管理虛擬機器(qemu+ssh串連)
[root@target ~]# yum install virt-viewer
[root@target ~]# export DISPLAY=192.168.40.18:0.0
[root@target ~]# virt-viewer -c qemu///system node4 #本地管理虛擬機器,system:擷取system許可權,注意qemu後是三個/
[root@manager ~]# virt-viewer -c qemu+ssh://root@192.168.32.40/system node4 #遠程linux通過virt-viewer+ssh管理虛擬機器
Xlib: extension "RANDR" missing on display "192.168.40.18:0.0".
root@192.168.32.40's password:
root@192.168.32.40's password:
#會彈出virt-viwer的gtk管理介面
4、使用已存在的虛擬機器設定文檔安裝新的虛擬機器
[root@target ~]# qemu-img create -f qcow2 /virhost/kvm_node/node6.img 20G
#為建立虛擬機器生產磁碟鏡像檔案
[root@target ~]# virsh list
Id 名稱 狀態
----------------------------------
18 node4 running
[root@target ~]# virsh dumpxml node4 >/etc/libvirt/qemu/node6.xml
#匯出虛擬機器node6的硬體設定資訊為/etc/libvirt/qemu/node6.xml
[root@target ~]# vim /etc/libvirt/qemu/node6.xml
<domain type='kvm' id='20'> #修改node6的id號
<name>node6</name> #虛擬機器node6的name
<uuid>4b7e91eb-6521-c2c6-cc64-c1ba72707fc7</uuid> #uuid必須修改,否則會和node4的衝突
<memory>524288</memory>
<currentMemory>524288</currentMemory>
<vcpu>1</vcpu>
<os>
<type arch='x86_64' machine='rhel5.4.0'>hvm</type>
<boot dev='network'/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/>
<source file='/virhost/node4.img'/> #指定新虛擬機器的硬碟檔案
<target dev='vda' bus='virtio'/>
</disk>
<interface type='bridge'>
<mac address='54:52:00:69:d5:c7'/>
<source bridge='br0'/>
<target dev='vnet0'/>
<model type='virtio'/>
</interface>
<interface type='bridge'>
<mac address='54:52:00:69:d5:d7'/>
<source bridge='br0'/>
<target dev='vnet1'/>
<model type='virtio'/>
</interface>
<serial type='pty'>
<source path='/dev/pts/4'/>
<target port='0'/>
</serial>
<console type='pty' tty='/dev/pts/4'>
<source path='/dev/pts/4'/>
<target port='0'/>
</console>
<input type='mouse' bus='ps2'/>
<graphics type='vnc' port='5900' autoport='yes' keymap='en-us'/>
</devices>
</domain>
[root@target ~]# virsh define /etc/libvirt/qemu/node6.xml
#使用虛擬描述文檔建立虛擬機器,可用virsh edit node6修改node6的設定檔
[root@target ~]# virsh start node6
#啟動虛擬機器
5 為虛擬機器開啟vnc
[root@target ~]# virsh edit node4 #編輯node4的設定檔;不建議直接通過vim node4.xml修改。
<graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1' keymap='en-us'/>
#port='-1' autoport='yes':port自動分配,監聽迴環網路(virt-manager管理需要listen='127.0.0.1'),無密碼
改為
<graphics type='vnc' port='5904' autoport='no' listen='0.0.0.0' keymap='en-us' passwd='xiaobai'/>
#固定vnc管理連接埠5904,不自動分配,vnc密碼xiaobai,監聽所有網路
2. 遠程vnc訪問地址:192.168.32.40:5904
五、儲存池和儲存卷的管理
1.建立 KVM主機儲存池
1).建立基於檔案夾(目錄)的儲存池
[root@target virhost]# virsh pool-define-as vmware_pool --type dir --target /virhost/vmware
#定義儲存池vmware_pool
或
[root@target virhost]# virsh pool-create-as --name vmware_pool --type dir --target /virhost/vmware
#建立儲存池vmware_pool,類型為檔案目錄,/virhost/vmware,與pool-define-as結果一樣
2).建立基於檔案系統的儲存池
[root@target virhost]#virsh pool-define-as --name vmware_pool --type fs --source-dev /dev/vg_target/LogVol02 --source-format ext4 --target /virhost/vmware
或
[root@target virhost]#virsh pool-create-as --name vmware_pool --type fs --source-dev /dev/vg_target/LogVol02 --source-format ext4 --target /virhost/vmware
3).查看儲存池資訊
[root@target virhost]# virsh pool-info vmware_pool #查看儲存域(池)資訊
名稱: vmware_pool
UUID: 2e9ff708-241f-fd7b-3b57-25df273a55db
狀態: running
Persistent: no
自動啟動: no
容量: 98.40 GB
分配: 18.39 GB
可用: 80.01 GB
4).啟動儲存池
[root@target virhost]# virsh pool-start vmware_pool #啟動儲存池
[root@target virhost]# virsh pool-list
名稱 狀態 自動開始
-----------------------------------------
default 活動 yes
virhost 活動 yes
vmware_pool 活動 no
5)銷毀儲存域,取消儲存池
[root@target virhost]# virsh pool-destroy vmware_pool #銷毀儲存池
[root@target virhost]# virsh pool-list --all
名稱 狀態 自動開始
-----------------------------------------
default 活動 yes
virhost 活動 yes
vmware_pool 不活躍 no
[root@target virhost]# virsh pool-undefine vmware_pool #取消儲存池的定義
[root@target virhost]# virsh pool-list --all
名稱 狀態 自動開始
-----------------------------------------
default 活動 yes
virhost 活動 yes
2.建立了儲存池後,就可以建立一個卷,這個卷是用來做虛擬機器的硬碟
[root@target virhost]# virsh vol-create-as --pool vmware_pool --name node6.img --capacity 10G --allocation 1G --format qcow2
#建立卷 node6.img,所在儲存池為vmware_pool,容量10G,初始分配1G,檔案格式類型qcow2
[root@target virhost]# virsh vol-info /virhost/vmware/node6.img #查看卷資訊
名稱: node6.img
類型: 檔案
容量: 10.00 GB
分配: 136.00 KB
3.在儲存卷上安裝虛擬機器主機
[root@target virhost]#virt-install --connect qemu:///system \
-n node7 \
-r 512 \
-f /virhost/vmware/node7.img \
--vnc \
--os-type=linux \
--os-variant=rhel6 \
--vcpus=1 \
--network bridge=br0 \
-c /mnt/rhel-server-6.0-x86_64-dvd.iso
六、虛擬機器的遷移(vmware 2 kvm)
1.安裝軟體
[root@target ~]# yum install -y virt-v2v.x86_64
[root@target ~]# rpm -ivh libguestfs-winsupport-1.0-7.el6.x86_64.rpm virtio-win-1.2.0-1.el6.noarch.rpm
#虛擬機器為windows時需要libguestfs-winsupport 包來支援 NTFS 檔案系統和 virtio-win 包來支援 Windows para-virtualized 儲存和網路裝置驅動
2.建立 KVM 主機儲存域(略)
virt-v2v 在遷移虛擬機器的過程中,需要拷貝被遷移虛擬機器到 KVM 主機預先定義的儲存池中。
3.建立 KVM 主機網路介面(略)
虛擬機器在遷移之後將串連 KVM 主機網路, 因此主機必須有一個與之相匹配的網路介面,比如說橋接器。
4.在 KVM 主機上建立或修改 $HOME/.netrc 檔案,添加 VMware ESXi 伺服器的使用者名稱和密碼。
[root@target ~]# cat ~/.netrc
machine 192.168.2.251 login root password xxxxxx
[root@target ~]# chmod 0600 ~/.netrc
5.Vmware ESXi到KVM的遷移
[root@target ~]# virt-v2v -ic esx://192.168.2.251/?no_verify=1 -op virhost -b br0 ipserver
** HEAD https://192.168.2.251/folder/tserver21/RHEL4.6-flat.vmdk?dcPath=ha-datacenter&dsName=ESX35-bak%3Astorage1 ==> 401 Unauthorized
** HEAD https://192.168.2.251/folder/tserver21/RHEL4.6-flat.vmdk?dcPath=ha-datacenter&dsName=ESX35-bak%3Astorage1 ==> 200 OK
** GET https://192.168.2.251/folder/tserver21/RHEL4.6-flat.vmdk?dcPath=ha-datacenter&dsName=ESX35-bak%3Astorage1 ==> 200 OK (2084s)
unknown filesystem /dev/hda
unknown filesystem /dev/fd0
virt-v2v: Installation failed because the following files referenced in the configuration file are required, but missing: rhel/4/kernel-smp-2.6.9-89.EL.i686.rpm
virt-v2v: 非 virtio 驅動程式配置的 tserver21
#所有選項可通過設定檔/etc/virt-v2v.conf來指定
#-op:指定用於轉換後的儲存域(pool),virhost
#-b:指定網路為橋接器br0
#-ic:指定要轉換的源地址
[root@target kvm_node]# virsh list --all
Id 名稱 狀態
----------------------------------
1 node4 running
- node5 關閉
- tserver21 關閉
- win8 關閉
[root@target kvm_node]# virsh start tserver21
6.KVM到KVM遷移
[root@target kvm_node]# virt-v2v -ic qemu+ssh://192.168.32.179/system -op virhost -b br0 node6
root@192.168.32.179's password:
root@192.168.32.179's password:
unknown filesystem label SWAP-vda3
virt-v2v: The connected hypervisor does not support a machine type of rhel5.4.0. It will be set to the current default.
virt-v2v: virtio 驅動程式配置的 node6
[root@target kvm_node]# virsh list --all
Id 名稱 狀態
----------------------------------
1 node4 running
- node5 關閉
- node6 關閉
- tserver21 關閉
- win8 關閉
[root@target kvm_node]# virsh start node6