KVM - Create Linux Virtual Machines

1. Introduction to QEMU

QEMU is an open source emulator and Virtual Machine Monitor (VMM). QEMU mainly provides two functions for users to use. One is as a user-mode simulator, using the dynamic code translation mechanism to execute code that is different from the host architecture. The second is to act as a virtual machine supervisor, simulate the whole system, and use other VMMs (Xen, KVM, etc) to use the virtualization support provided by the hardware to create virtual machines with performance close to the host.



2016/12/17

1.kvm vmware
2.openstack
3.puppet saltstack ansble
4.docker
KVM introduces
KVM and QEMU
QEMU virtualization software, user-level virtualization technology.
KVM kernel-level virtualization can virtualize CPU memory, but cannot virtualize disk network cards, PCI devices, and use QEMU to virtualize.
Virtual virtual machines are nested virtual in vmware workstation.
Older servers need to enable CPU virtualization in the BIOS.


################################################## ################################################## ################################################## ###########
First,
install and use KVM
qemu-kvm to simulate a virtual machine virt-install creates a virtual machine libvirt manages a virtual machine
[root@linux-node1 ~]# yum install -y qemu-kvm qemu- kvm-tools libvirt
virt-install
[root@linux-node1 ~]# yum install -y virt-install net-tools 
Start libvirt
[root@linux-node1 ~]# systemctl enable libvirtd
[root@linux-node1 ~]# systemctl start libvirtd  
2.
After libvirt is started, the virbr network card type will be automatically created in the virtual machine to
create a virtual machine Conditions
1. Need a hard disk
2. Need an image
3. Need a VNC tool to connect to the virtual machine The machine
officially starts to create a virtual machine. Steps:
Introduction to the disk format
Raw raw disk does not support snapshot
qcow2. If the original image is not saved, it will only record the operation and the original image and make a snapshot. A copy-on-write technique that records operations done in a file and mirrors only that file. The method used by Openstack recommends using this
difference:
The basic unit of qcow2 data is that the cluster 
raw performance is faster than that of qcow2.
How much space is created by raw is allocated directly, and how much space is occupied by qcow2 dynamically.
Create a disk
to create a virtual machine image Raw raw disk Disk name How small the disk is 10G Note that this disk can only be used by one machine, which is equivalent to a 10G physical hard disk, and the disk needs to be recreated when creating other machines.
qemu-img create -f raw /opt/CentOS-7-x86_64.raw 10G
View the format of the disk image
qemu-img info /opt/CentOS-7-x86_64.raw
Pay attention to the image permissions.
安装虚拟机                             虚拟机名称自定义  内存大小      镜像路径                                 指定刚才创建的磁盘。                     网络默认使用桥接网卡   监听VNC使用VNC工具连接直接操作                                            
virt-install --virt-type kvm --name CentOS-7-x86_64 --ram 1024 --cdrom=/root/CentOS-7.2-x86_64-DVD-1511.iso --disk path=/opt/CentOS-7-x86_64.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
执行完之后
在VNC中连接
192.168.56.11:5900


安装Centos6 
创建10G raw硬盘,创建虚拟机时使用这个10G硬盘,一个硬盘只能使用在一台机器上面。
qemu-img create -f raw /opt/CentOS-6-x86_64.raw 10G
virt-install --virt-type kvm --name CentOS-6-x86_64 --ram 1024 --cdrom=/opt/rhel-server-6.4-x86_64-dvd.iso  --disk path=/opt/CentOS-6-x86_64.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
在虚拟机世界里面越简单越好,方便扩展。
如果多次执行安装Centos6
rm -rf /opt/CentOS-6-x86_64.raw
qemu-img create -f raw /opt/CentOS-6.6-x86_64.raw 10G
virt-install --virt-type kvm --name CentOS-6-x86_64 --ram 1024 --cdrom=/opt/rhel-server-6.4-x86_64-dvd.iso  --disk path=/opt/CentOS-6.6-x86_64.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole


3.TightVNC 连接到192.168.56.11:5900
KVM虚拟机就是一个进程,只要是进程都会受到CPU的调度。




###############################################################################################################################################################
管理KVM虚拟机  
一、
常用命令
(0)查看有哪些虚拟机
[root@linux-node1 ~]# virsh list --all 
 Id    Name                           State
 [root@localhost ~]# virsh list --all
[root@linux-node1 ~]# virt-install   #建立kvm虚拟机
[root@linux-node1 ~]# virsh list   #查看正在运行的KVM虚拟机
[root@linux-node1 ~]# virsh list --all  #查看所有KVM虚拟机
[root@linux-node1 ~]# virsh start name    #启动KVM虚拟机
[root@linux-node1 ~]# virsh shutdown name  #正常关闭KVM虚拟机
[root@linux-node1 ~]# virsh destroy name   #强制关闭KVM虚拟机(类似于直接断电)
[root@linux-node1 ~]# virsh suspend name   #挂起KVM虚拟机
[root@linux-node1 ~]# virsh resume name   #恢复挂起的KVM虚拟机
[root@linux-node1 ~]# virsh dumpxml name   #查看KVM虚拟机配置文件
[root@linux-node1 ~]# virsh edit name   #编辑KVM虚拟机的xml配置文件
[root@linux-node1 ~]# virsh undefine name    #彻底删除KVM虚拟机,不可逆,如果想找回来,需要备份/etc/libvirt/qemu的xml文件)


----------------------------------------------------
 -     CentOS-7-x86_64             shut off
(1)启动kvm虚拟机
[root@linux-node1 ~]#  virsh start  CentOS-7-x86_64
(2)关闭KVM虚拟机
[root@linux-node1 ~]# virsh shutdown  CentOS-7-x86_64 
(3)挂起KVM虚拟机
[root@linux-node1 ~]# virsh resume CentOS-7-x86_64
Domain CentOS-7-x86_64 resumed
(4)创建快照
查看创建过哪些快照 
[root@linux-node1 ~]# virsh   snapshot-list CentOS-7-x86_64


[root@linux-node1 ~]# virsh snapshot-create CentOS-7-x86_64 #注意创建快照的命令,raw格式磁盘不支持创建快照 
error: unsupported configuration: internal snapshot for disk vda unsupported for storage type ra
[root@linux-node1 ~]# virsh snapshot-delete                  #删除快照


三、
通过VNC连接到KVM虚拟机设置IP地址
创建完虚拟机默认是没有IP地址,需要修改机器的网卡配置文件。
vim /etc/resolv.conf  添加nameserver
vim /etc/sysconfig/network-scripts/ifcfg-eth0  修改网卡内容 ONBOOT=yes 注释掉UUID 
/etc/init.d/network  restart   重启网卡 


四、
这个就是刚才创建虚拟机生成的xml文件,这个文件记录了虚拟机硬件和软件内容。
[root@linux-node1 ~]# ls /etc/libvirt/qemu/
CentOS-7-x86_64.xml  networks
[root@linux-node1 ~]#  virsh edit CentOS-7-x86_64.xml     编辑虚拟机的XML文件。
virtio半虚拟化的驱动    


五、
测试虚拟机删除和恢复:
虚拟机是通过xml来管理的,不小心把xml删除掉的话可以通过xml来进行恢复,前提是硬盘还在。 
(0)备份xml文件
[root@linux-node1 ~]# virsh dumpxml CentOS-7-x86_64 >CentOS-7-x86_64-bakcup.xml 
(1)彻底删除kvm虚拟机做测试
[root@linux-node1 ~]# virsh undefine CentOS-7-x86_64
(2)关闭虚拟机
[root@linux-node1 ~]# virsh  shutdown CentOS-7-x86_64
(3)查看虚拟机,刚才创建的一台虚拟机没有了。
[root@linux-node1 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
(4)恢复虚拟机
[root@linux-node1 ~]# virsh define CentOS-7-x86_64-bakcup.xml 
(5)启动虚拟机
[root@linux-node1 ~]# virsh start CentOS-7-x86_64


#################################################################################################################################################################
虚拟机调整
CPU 内存 网络  三部分进行调整


一、CPU参数修改
1.编辑虚拟机的XML文件,动态修改CPU个数,不是动态修改CPU个数,修改完需要重新启动KVM虚拟机。
[root@linux-node1 ~]# virsh edit CentOS-7-x86_64
将<vcpu placement='static'>1</vcpu>
修改为<vcpu placement='auto' current="1">4</vcpu> CPU个数允许添加到4个。
重新启动KVM虚拟机
[root@linux-node1 ~]# virsh shutdown CentOS-7-x86_64
[root@linux-node1 ~]# virsh start CentOS-7-x86_64
使用命令修改CPU个数,不用重启自动生效。
[root@linux-node1 ~]# virsh setvcpus CentOS-7777-x86_64 2 --live


二、内存参数设置
修改不了KVM xml配置文件
修改内存只能调小,不能增大内存,可以再创建KVM虚拟机时将内存设置大一些这样就可以自由向小的调整了。
[root@linux-node1 ~]# virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd balloon 1021
查看内存
[root@linux-node1 ~]# virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd info balloon
balloon: actual=1021


三、网络
KVM内网走的是nat转换。
1.查看桥接网卡
[root@linux-node1 ~]# brctl show
bridge name bridge id STP enabled interfaces
virbr0 8000.fe540052e4a8 yes vnet0
2.将KVM网卡IP地址桥接到物理网卡上
brctl addbr br0
3.创建br0桥接网卡
[root@linux-node1 ~]#  brctl addif br0 eth0
4.将br0的关联到eth0上面,这一步会断网。最佳实践是使用脚本。
[root@linux-node1 ~]# ip addr del dev eth0 192.168.56.11/24 
进入到虚拟机中操作
5.[root@linux-node1 ~]# ifconfig br0 192.168.56.11/24 up 
6.添加路由要不然不能上网。
[root@linux-node1 ~]# route add default gw 192.168.56.2 
7.修改xml文件修改网卡
[root@linux-node1 ~]# virsh edit CentOS-7777-x86_64

<interface type='network'>
修改为
<interface type='bridge'>

<source bridge='br0'/>
修改为
保存退出
8.重启虚拟机
[root@linux-node1 ~]# virsh shutdown CentOS-7-x86_64 
Domain CentOS-7-x86_64 is being shutdown
[root@linux-node1 ~]# virsh start CentOS-7-x86_64 
Domain CentOS-7-x86_64 started
使用VNC登录上去修改网络配置文件
192.168.56.11:5900


vim /etc/sysconfig/network-scripts/ifcfg-eth0 
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
NAME=eth0
#UUID=a800d4a6-9b1a-4f4c-95e9-242cdccdf372
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.56.18      #自己设置的网卡IP地址,桥接网卡和物理机在同一个网端中。
NETMASK=255.255.255.0
GATEWAY=192.168.56.2


创建桥接网卡脚本
#!/bin/bash
brctl addbr br0
brctl addif br0 eth0
ip addr del dev eth0 192.168.56.11/24
ifconfig br0 192.168.56.11/24 up 

route add default gw 192.168.56.2 


出处:https://blog.csdn.net/fanhonooo/article/details/53898991

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325572849&siteId=291194637