KVM安装测试

一、系统centos7.4 X64 ,虚拟机4G内存以上
yum install -y qemu-kvm qemu-kvm-tools virt-manager libvirt virt-install virt-viewer
 
Installed:
libvirt.x86_64 0:3.2.0-14.el7_4.9 qemu-kvm.x86_64 10:1.5.3-141.el7_4.6 qemu-kvm-tools.x86_64 10:1.5.3-141.el7_4.6 virt-install.noarch 0:1.4.1-7.el7
virt-manager.noarch 0:1.4.1-7.el7
 
创建虚拟机
[root@kvm ~]# qemu-img create -f raw /opt/CentOD-7.1-X86_64.raw 10G
Formatting '/opt/CentOD-7.1-X86_64.raw', fmt=raw size=10737418240
[root@kvm ~]# lsmod |grep kvm
kvm_intel 170086 0
kvm 566340 1 kvm_intel
irqbypass 13503 1 kvm
 
[root@kvm ~]# systemctl enable libvirtd.service
[root@kvm ~]# systemctl start libvirtd.service
[root@kvm ~]# systemctl status libvirtd.service
● libvirtd.service - Virtualization daemon
Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2018-03-31 12:40:57 CST; 4s ago
Docs: man:libvirtd(8)
http://libvirt.org
Main PID: 7220 (libvirtd)
CGroup: /system.slice/libvirtd.service
├─7220 /usr/sbin/libvirtd
├─7297 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
└─7299 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
 
Mar 31 12:40:57 kvm systemd[1]: Started Virtualization daemon.
Mar 31 12:40:58 kvm dnsmasq[7297]: started, version 2.76 cachesize 150
Mar 31 12:40:58 kvm dnsmasq[7297]: compile time options: IPv6 GNU-getopt DBus no-i18n IDN DHCP DHCPv6 no-Lua TFTP no-conntrack ipset auth no-DNSSEC loo...ct inotify
Mar 31 12:40:58 kvm dnsmasq-dhcp[7297]: DHCP, IP range 192.168.122.2 -- 192.168.122.254, lease time 1h
Mar 31 12:40:58 kvm dnsmasq-dhcp[7297]: DHCP, sockets bound exclusively to interface virbr0
Mar 31 12:40:58 kvm dnsmasq[7297]: reading /etc/resolv.conf
Mar 31 12:40:58 kvm dnsmasq[7297]: using nameserver 192.168.119.2#53
Mar 31 12:40:58 kvm dnsmasq[7297]: read /etc/hosts - 2 addresses
Mar 31 12:40:58 kvm dnsmasq[7297]: read /var/lib/libvirt/dnsmasq/default.addnhosts - 0 addresses
Mar 31 12:40:58 kvm dnsmasq-dhcp[7297]: read /var/lib/libvirt/dnsmasq/default.hostsfile
Hint: Some lines were ellipsized, use -l to show in full.
 
拷贝要安装的系统ISO镜像
[root@kvm ~]# dd if=/dev/cdrom of=/opt/CentOS-7.4.ISO
8830976+0 records in
8830976+0 records out
4521459712 bytes (4.5 GB) copied, 254.81 s, 17.7 MB/s
安装KVM虚拟机
[root@kvm ~]# virt-install --help
***/^Husage: virt-install --name NAME --memory MB STORAGE INSTALL [options]
 
Create a new virtual machine from specified install media.
 
optional arguments:
-h, --help show this help message and exit
--version show program's version number and exit
--connect URI Connect to hypervisor with libvirt URI
 
General Options:
-n NAME, --name NAME Name of the guest instance
--memory MEMORY Configure guest memory allocation. Ex:
--memory 1024 (in MiB)
--memory 512,maxmemory=1024
--memory 512,maxmemory=1024,hotplugmemorymax=2048,hotplugmemoryslots=2
--vcpus VCPUS Number of vcpus to configure for your guest. Ex:
--vcpus 5
--vcpus 5,maxcpus=10,cpuset=1-4,6,8
--vcpus sockets=2,cores=4,threads=2,
--cpu CPU CPU model and features. Ex:
--cpu coreduo,+x2apic
--cpu host
--metadata METADATA Configure guest metadata. Ex:
--metadata name=foo,title="My pretty title",uuid=...
--metadata description="My nice long description"
 
Installation Method Options:
--cdrom CDROM CD-ROM installation media
-l LOCATION, --location LOCATION
Installation source (eg, nfs:host:/path,
http://host/path, ftp://host/path)
--pxe Boot from the network using the PXE protocol
--import Build guest around an existing disk image
--livecd Treat the CD-ROM media as a Live CD
-x EXTRA_ARGS, --extra-args EXTRA_ARGS
Additional arguments to pass to the install kernel
booted from --location
--initrd-inject INITRD_INJECT
Add given file to root of initrd from --location
--os-variant DISTRO_VARIANT
The OS variant being installed guests, e.g.
'fedora18', 'rhel6', 'winxp', etc.
--boot BOOT Configure guest boot settings. Ex:
--boot hd,cdrom,menu=on
--boot init=/sbin/init (for containers)
--idmap IDMAP Enable user namespace for LXC container. Ex:
--idmap uid_start=0,uid_target=1000,uid_count=10
 
Device Options:
--disk DISK Specify storage with various options. Ex.
--disk size=10 (new 10GiB image in default location)
--disk /my/existing/disk,cache=none
--disk device=cdrom,bus=scsi
--disk=?
-w NETWORK, --network NETWORK
Configure a guest network interface. Ex:
--network bridge=mybr0
--network network=my_libvirt_virtual_net
--network network=mynet,model=virtio,mac=00:11...
--network none
--network help
--graphics GRAPHICS Configure guest display settings. Ex:
--graphics vnc
--graphics spice,port=5901,tlsport=5902
--graphics none
--graphics vnc,password=foobar,port=5910,keymap=ja
--controller CONTROLLER
Configure a guest controller device. Ex:
--controller type=usb,model=ich9-ehci1
--input INPUT Configure a guest input device. Ex:
--input tablet
--input keyboard,bus=usb
--serial SERIAL Configure a guest serial device
--parallel PARALLEL Configure a guest parallel device
--channel CHANNEL Configure a guest communication channel
--console CONSOLE Configure a text console connection between the guest
and host
--hostdev HOSTDEV Configure physical USB/PCI/etc host devices to be
shared with the guest
--filesystem FILESYSTEM
Pass host directory to the guest. Ex:
--filesystem /my/source/dir,/dir/in/guest
--filesystem template_name,/,type=template
--sound [SOUND] Configure guest sound device emulation
--watchdog WATCHDOG Configure a guest watchdog device
--video VIDEO Configure guest video hardware.
--smartcard SMARTCARD
Configure a guest smartcard device. Ex:
--smartcard mode=passthrough
--redirdev REDIRDEV Configure a guest redirection device. Ex:
--redirdev usb,type=tcp,server=192.168.1.1:4000
--memballoon MEMBALLOON
Configure a guest memballoon device. Ex:
--memballoon model=virtio
--tpm TPM Configure a guest TPM device. Ex:
--tpm /dev/tpm
--rng RNG Configure a guest RNG device. Ex:
--rng /dev/random
--panic PANIC Configure a guest panic device. Ex:
--panic default
--memdev MEMDEV Configure a guest memory device. Ex:
--memdev dimm,target_size=1024
 
Guest Configuration Options:
--security SECURITY Set domain security driver configuration.
--numatune NUMATUNE Tune NUMA policy for the domain process.
--memtune MEMTUNE Tune memory policy for the domain process.
--blkiotune BLKIOTUNE
Tune blkio policy for the domain process.
--memorybacking MEMORYBACKING
Set memory backing policy for the domain process. Ex:
--memorybacking hugepages=on
--features FEATURES Set domain <features> XML. Ex:
--features acpi=off
--features apic=on,eoi=on
--clock CLOCK Set domain <clock> XML. Ex:
--clock offset=localtime,rtc_tickpolicy=catchup
--pm PM Configure VM power management features
--events EVENTS Configure VM lifecycle management policy
--resource RESOURCE Configure VM resource partitioning (cgroups)
--sysinfo SYSINFO Configure SMBIOS System Information. Ex:
--sysinfo emulate
--sysinfo host
--sysinfo bios_vendor=Vendor_Inc.,bios_version=1.2.3-abc,...
--sysinfo system_manufacturer=System_Corp.,system_product=Computer,...
--sysinfo baseBoard_manufacturer=Baseboard_Corp.,baseBoard_product=Motherboard,...
--qemu-commandline QEMU_COMMANDLINE
Pass arguments directly to the qemu emulator. Ex:
--qemu-commandline='-display gtk,gl=on'
--qemu-commandline env=DISPLAY=:0.1
 
Virtualization Platform Options:
-v, --hvm This guest should be a fully virtualized guest
-p, --paravirt This guest should be a paravirtualized guest
--container This guest should be a container guest
--virt-type HV_TYPE Hypervisor name to use (kvm, qemu, xen, ...)
--arch ARCH The CPU architecture to simulate
--machine MACHINE The machine type to emulate
 
Miscellaneous Options:
--autostart Have domain autostart on host boot up.
--transient Create a transient domain.
--wait WAIT Minutes to wait for install to complete.
--noautoconsole Don't automatically try to connect to the guest
console
--noreboot Don't boot guest after completing install.
--print-xml [XMLONLY]
Print the generated domain XML rather than create the
guest.
--dry-run Run through install process, but do not create devices
or define the guest.
--check CHECK Enable or disable validation checks. Example:
--check path_in_use=off
--check all=off
-q, --quiet Suppress non-error output
-d, --debug Print debugging information
 
Use '--option=?' or '--option help' to see available suboptions
See man page for examples and full option syntax.
 
virt-install --name CentOS7.4 --virt-type kvm --memory 1024 --cdrom=/opt/CentOS-7.4.ISO --disk=/opt/CentOD-7.1-X86_64.raw --network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --check path_in_use=off
 
使用tightvnc连接母机5900端口:
按tab键修改内核参数定义网口为eth0:
net.ifnames=0 boisdevname=0
完成开始安装,进入图形化安装选项。
virsh 进入管理控制台:
help打印所有命令选项
管理KVM:
1、CPU
virsh edit kvm01
修改
<vcpu palcement='auto' current="1">4</vcpu>
 
116 virsh shutdown kvm01
117 virsh list --all
118 virsh start kvm01
重启后完成CPU热添加
virsh setvcpus kvm01 --live 2
添加成功
CENTOS7.4自动激活不用配
CPU开始工作
[root@kvm opt]# virsh setvcpus kvm01 --live 1
error: unsupported configuration: failed to find appropriate hotpluggable vcpus to reach the desired target vcpu count
只能加不能删除
2、内存热添加,可加可减
virsh edit kvm01
<memory unit='KiB'> 1048576</memory>
<memory unit='KiB'> 4048576</memory>
保存退出重启KVM01
<memory unit='KiB'>4048576</memory>最大内存
<currentMemory unit='KiB'>1048576</currentMemory>当前内存
root@kvm opt]# virsh shutdown kvm01
Domain kvm01 is being shutdown
 
[root@kvm opt]# virsh list --all
Id Name State
----------------------------------------------------
- kvm01 shut off
 
[root@kvm opt]# virsh start kvm01
Domain kvm01 started
 
virsh # qemu-monitor-command kvm01 --hmp --cmd info balloon
balloon: actual=1024
virsh # qemu-monitor-command kvm01 --hmp --cmd balloon 2048
virsh # qemu-monitor-command kvm01 --hmp --cmd balloon 768
virsh # qemu-monitor-command kvm01 --hmp --cmd balloon 1024
 
3、磁盘管理
qemu-img --list
Supported formats: vvfat vpc vmdk vhdx vdi ssh sheepdog rbd raw host_cdrom host_floppy host_device file qed qcow2 qcow parallels nbd iscsi gluster dmg tftp ftps ftp https http cloop bochs blkverify blkdebug
全镜像模式(直接分配完)RAW格式 和稀疏模式qcows格式,支持压缩快照镜像
 
4、网络管理,生产中用网桥模式和主机一个网段
[root@kvm opt]# brctl show
bridge name bridge id STP enabled interfaces
virbr0 8000.5254009fde2a yes virbr0-nic
vnet0
[root@kvm opt]# yum install bridge-utils -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* extras: mirrors.shu.edu.cn
* updates: mirrors.cn99.com
Package bridge-utils-1.5-9.el7.x86_64 already installed and latest version
Nothing to do
 
添加一个网桥
[root@kvm opt]# brctl addbr br0
[root@kvm opt]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000000000000 no
virbr0 8000.5254009fde2a yes virbr0-nic
vnet0
[root@kvm opt]# brctl addif br0 ens33(把eth0加到桥接网络中)回车后网络断开
 
在主机上重新给配IP地址
brctl addif br0 ens33 && ip addr del dev ens33 192.168.119.120/24 && ifconfig br0 192.168.119.120/24 up && route add default gw 192.168.119.2 && iptables -F
在母机下执行检查IP检查
[root@kvm ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c29d8d8eb no ens33
virbr0 8000.5254009fde2a yes virbr0-nic
vnet0
[root@kvm ~]# ifconfig -a
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.119.120 netmask 255.255.255.0 broadcast 192.168.119.255
inet6 fe80::20c:29ff:fed8:d8eb prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:d8:d8:eb txqueuelen 1000 (Ethernet)
RX packets 99 bytes 11520 (11.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 51 bytes 7691 (7.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::20c:29ff:fed8:d8eb prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:d8:d8:eb txqueuelen 1000 (Ethernet)
RX packets 130845 bytes 94720962 (90.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 102586 bytes 21987897 (20.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
 
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 12 bytes 1136 (1.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 12 bytes 1136 (1.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
 
virbr0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:9f:de:2a txqueuelen 1000 (Ethernet)
RX packets 220 bytes 17614 (17.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 49 bytes 5987 (5.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
 
virbr0-nic: flags=4098<BROADCAST,MULTICAST> mtu 1500
ether 52:54:00:9f:de:2a txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
另外一种创建办法:
1.安装网桥工具
yum install bridge-utils -y
2.ifconfig virbr0     //确认有类似下面的输出
virbr0    Link encap:Ethernet  HWaddr 52:54:00:A0:83:9A  
         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:0 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0
         RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
3.设置bridge       //bridge模式有个好处,无论是想从vm访问外网还是从外网访问vm都可以,所以建议设置为这种模式
1)修改/etc/sysconfig/network-scripts/ifcfg-eth0,并且在同目录下新建ifcfg-br0,两文件内容如下:
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BRIDGE=br0
 
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
TYPE=Bridge
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=Static     //如果想使用DHCP方式,把这里换成dhcp,下面的都删掉就行了
IPADDR=192.168.1.80
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
2)sysctl -p     //查看如下三项是否都为0,我这里默认就是0,官网说这是为了disabled netfilter
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
 
使用virsh undefine kvm01删除虚拟机
删除前可以先备份/etc/libvirt/qemu/kvm01.xml可用于恢复虚拟机
virsh destory相当于关电源
 
4\KVM优化
CPU使用 taskset -cp 0,2,5-11 9865命令表示将进程9864绑定KVM进程到#0、#2、#5~#11号核上面。
内存优化:开启大页内存
[root@kvm transparent_hugepage]# pwd
/sys/kernel/mm/transparent_hugepage
[root@kvm transparent_hugepage]# cat enabled
[always] madvise never
 
镜像制作注意事项
 
 
ovirt 安装完后
engine-setup

猜你喜欢

转载自www.cnblogs.com/icerain0/p/9115800.html