kvm学习篇
云计算:一种资源的使用模式
弹性,按需付费
资源
降低成本
公有云:亚马逊、阿里云
私有云:
混合云:
安装:
yum install qemu-kvm qemu-kvm-tools virt-manager libvirt
[root@yz3110 ~]# /etc/init.d/libvirtd start
Starting libvirtd daemon: [ OK ]
virbr0 Link encap:Ethernet HWaddr 52:54:00:EA:A5:53
inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:30201 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:1392016 (1.3 MiB)
[root@yz3110 networks]# brctl show
bridge name bridge id STP enabled interfaces
virbr0 8000.525400eaa553 yes virbr0-nic
创建虚拟机:
[root@yz3110 networks]# qemu-img create -f raw /data0/Centos-6.2-x86_64.raw 5G
Formatting '/data0/Centos-6.2-x86_64.raw', fmt=raw size=5368709120
[root@yz3110 networks]# file /data0/Centos-6.2-x86_64.raw
/data0/Centos-6.2-x86_64.raw: data
[root@yz3110 networks]# qemu-img info /data0/Centos-6.2-x86_64.raw
image: /data0/Centos-6.2-x86_64.raw
file format: raw
virtual size: 5.0G (5368709120 bytes)
disk size: 0
[root@yz3110 networks]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 4
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 2
Stepping: 3
CPU MHz: 2400.266
BogoMIPS: 4800.53
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 4096K
NUMA node0 CPU(s): 0-3
virt-install -virt-type kvm --name Cenntos-6.6-x86_64 --ram 512(内存) --cdrom=/data0/Centos-6.6-x86_64.iso --disk pathh=/data0/*.raw --network netrowrk=default
[root@yz3110 data0]# modprobe kvm
[root@yz3110 data0]# modprobe vhost-net
[root@yz3110 data0]# rpm -q virt-manager
virt-manager-0.9.0-19.el6.x86_64
virt-install --virt-type kvm --name Centos-6.2-x86_64 --ram 512 --cdrom=/data0/soft/CentOS-6.2-x86_64-minimal.iso --disk path=/data0/Centos-6.2-x86_64.raw --network network=default --graphics vnc,listem=0.0.0.0 --noautoconsole --os-type=linux --os-variant=rhel16
ERROR No domains available for virt type 'hvm', arch 'x86_64', domain type 'kvm'
解决:
In my case the problem is with hardware virtualization disabled in BIOS. Also I was in a state that I can't turn that on. Replacing 'kvm' with 'qemu' fixed my problem.
[root@yz3110 data0]# virt-install --virt-type qemu --name Centos-6.2-x86_64 --ram 512 --cdrom=/data0/soft/CentOS-6.2-x86_64-minimal.iso --disk path=/data0/Centos-6.2-x86_64.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --os-type=linux
WARNING KVM acceleration not available, using 'qemu'
Starting install...
Creating domain... | 0 B 00:00
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
监控:
yum install virt-top
或
zabbix
优化:
内存
[root@yz3110 qemu]# cat /sys/kernel/mm/redhat_transparent_hugepage/enabled
always [never]
[root@yz3110 qemu]# ps -ef |grep ksm
root 59 2 0 Feb01 ? 00:00:00 [ksmd]
root 1298 1 0 14:04 pts/0 00:00:00 /bin/bash /usr/sbin/ksmtuned
io优化:
virtio
/etc/udev/rules.d/70-persistent-net.rules
记录虚拟机操作的
virsh undefine Centos-6.2-x86_64 删除虚拟机
[root@yz3110 data0]# virsh undefine Centos-6.2-x86_64
error: Refusing to undefine while domain managed save image exists
[root@yz3110 data0]# virsh undefine Centos-6.2-x86_64 --managed-save
Domain Centos-6.2-x86_64 has been undefined
[root@yz3110 vmdata]# brctl addbr br0 创建网桥
[root@yz3110 vmdata]# brctl addif br0 eth1桥接到eth1上
kvm优势:
1)开源
2)性能好,同样的硬件条件下能够提供更好的虚拟机性能,主要是因为kvm架构简单,代码只有2W行,一开始就支持硬件虚拟化
3)免费方案多,稳定和兼容性也越来越好
4)广泛免费的技术支持
egrep '(vmx|svm)' /proc/cpuinfo 查看是否支持虚拟化
在KVM环境中,一下rpm包都是必需的
[yongsan@yz3110 ~]$ rpm -qa |grep -E 'qemu|libvirt|virt'
python-virtinst-0.600.0-18.el6.noarch
libvirt-java-devel-0.4.7-1.el6.noarch
libvirt-python-0.10.2-29.el6.x86_64
qemu-kvm-0.12.1.2-2.415.el6.x86_64
qemu-img-0.12.1.2-2.415.el6.x86_64
virt-top-1.0.4-3.11.el6.x86_64
virt-manager-0.9.0-19.el6.x86_64
qemu-kvm-tools-0.12.1.2-2.415.el6.x86_64
virt-what-1.11-1.1.el6.x86_64
libvirt-devel-0.10.2-29.el6.x86_64
libvirt-java-0.4.7-1.el6.noarch
libvirt-client-0.10.2-29.el6.x86_64
virt-viewer-0.4.1-7.el6.x86_64
libvirt-0.10.2-29.el6.x86_64
gpxe-roms-qemu-0.9.7-6.9.el6.noarch
查看kvm模块是否载入
[yongsan@yz3100 ~]$ lsmod |grep kvm
kvm_intel 41436 16
kvm 256599 1 kvm_intel
virt-manager的使用:图形化的管理工具
安装图形化界面
yum groupinstall -y "Desktop platform" "Desktop platform Development" "Fonts" "General Purpose Desktop" "Graphical Administration Tools" "Graphics Creation Tools" "Input Methods" "x Window System" "Chinese Suport [zh]" "Interenet Browser"
vnc配置:
安装vnc
yum install -y tigervnc
yum install -y tigervnc-server
vim /etc/sysconfig/vncseervers
vncpaasswd
问题:
[root@yz3110 ~]# service vncserver restart
Shutting down VNC server: [ OK ]
Starting VNC server: 2:root xauth: creating new authority file /root/.Xauthority
WARNING: The first attempt to start Xvnc failed, possibly because the font
catalog is not properly configured. Attempting to determine an appropriate
font path for this system and restart Xvnc using that font path ...
Could not start Xvnc.
/usr/bin/Xvnc: symbol lookup error: /usr/bin/Xvnc: undefined symbol: pixman_composite_trapezoids
/usr/bin/Xvnc: symbol lookup error: /usr/bin/Xvnc: undefined symbol: pixman_composite_trapezoids
[FAILED]
解决:
yum install pixman pixman-devel libXfont -y
[root@yz3110 ~]# service vncserver restart
Shutting down VNC server: [ OK ]
Starting VNC server: 2:root
New 'yz3110.hadoop.data.sina.com.cn:2 (root)' desktop is yz3110.hadoop.data.sina.com.cn:2
Creating default startup script /root/.vnc/xstartup
Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/yz3110.hadoop.data.sina.com.cn:2.log
[ OK ]
[yongsan@yz3110 ~]$ numactl --hardware查看当前CPU的硬件情况
[yongsan@yz3110 ~]$ numastat 查看每个节点的内存统计
[yongsan@yz3110 ksm]$ lspci
00:00.0 Host bridge: Intel Corporation 440FX - 82441FX PMC [Natoma] (rev 02)
00:01.0 ISA bridge: Intel Corporation 82371SB PIIX3 ISA [Natoma/Triton II]
00:01.1 IDE interface: Intel Corporation 82371SB PIIX3 IDE [Natoma/Triton II]
00:01.2 USB controller: Intel Corporation 82371SB PIIX3 USB [Natoma/Triton II] (rev 01)
00:01.3 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 03)
00:02.0 VGA compatible controller: Cirrus Logic GD 5446
00:03.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL-8139/8139C/8139C+ (rev 20)
00:04.0 SCSI storage controller: Red Hat, Inc Virtio block device
00:05.0 RAM memory: Red Hat, Inc Virtio memory balloon
virsh # qemu-monitor-command Centos-6.2-x86_64 --hmp --cmd info balloon
balloon: actual=512
virsh # qemu-monitor-command Centos-6.2-x86_64 --hmp --cmd balloon 1024
virsh # qemu-monitor-command Centos-6.2-x86_64 --hmp --cmd info balloon
balloon: actual=1024
虚拟机限速
virsh # memtune Centos-6.2-x86_64 --hard-limit 20148 --config