[Reprint] the KVM virtualization configuration

Configuring the KVM virtualization

[TOC]

Virtualization: Virtualization means the cpu, memory, hard disk to simulate real hardware resources into fewer virtual hardware resources. The benefits of virtualization is to maximize the use of hardware resources. Also the foundation of cloud computing. The following figure shows the virtual model:

mark

Virtualization Category:

Software level, all hardware simulated (qemu) by simulator

Hardware level, through the VMM (virtual machine monitor), the allocation and management of hardware, operating system running on (xen, VMware [ESX, workstation], kvm, virtualbox) establish good vm

The operating system level, the virtual machine operating system and host operating systems to share, you can simulate thousands of virtual machines out (openvz)

Paravirtualization and full virtualization

Paravirtualization: paravirtualization, the host and client need to change the operating system kernel, the client aware of the existence of the host, the two systems need to cooperate. (Xen virtualization), better performance paravirtualization.

Full virtualization: full virtualization client provides a complete virtual hardware resources, clients do not need to make any change that does not know the existence of the host (kvm belong to full virtualization, VMware is fully virtualized)

Xen and compare kvm

1. Whether the CPU supports does not support virtualization, xen can be installed, and kvm CPU must support virtualization job

2.xen support paravirtualization will support full virtualization, kvm only support full virtualization

3. After installing xen, xen kernel will replace the original system to take over the management, but only a kvm module of the Linux kernel, the kernel is still the original management system

Time 4.xen occurred earlier than kvm, kvm do not need to take over because the kernel, the kernel of love and receive the most use

5. xen architecture, the hardware running on the hypervisor xen, and the system resources virtualization, a virtual resource allocation to the upper layer user virtual machine (vm), and then run by the virtual machine corresponding customer vm operating system

mark

VMware centos7 64 Wei kvm virtualization configurations

First, before the experiment preparation

Kvm virtualization requires us to configure the CPU supports virtualization, some machines support but also need to open the BIOS.

We've created using a virtual machine as a host, configuration is as follows

1. Memory 4G

2. there is a disk of 50G

3. Configure the virtual machine to support virtualization

mark

4. Turn off and remove selinux iptables rules

Second, install kvm

2.1, check whether your system supports virtualization

1
grep -E 'VMX | svm' / proc / cpuinfo

If you have content output is supported. Intel's CPU supports vmx, AMD's CPU support svm

2.2, install kvm

When installing, we also need to install dependencies

1
yum install -y bridge-utils virt-* kvm qemu-img libvirt

2.3, check kvm is loaded

1
lsmod | grep sq.m.

mark

  • If not, you need to perform the followingmodprobe kvm-intel
  • If not, we need to restart the host

2.4, configure the network card

(1) Configuration ens33

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
cd /etc/sysconfig/network-scripts/
cp ifcfg-eth0 ifcfg-br0
# Edit ifcfg-ens33, configuration is as follows
vim ifcfg-ens33
 
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE = yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE = yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=br0
  • BRIDGE represents a bridge configuration

(2) Configuration br0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
vim /etc/sysconfig/network-scripts/ifcfg-br0
# Configuration is as follows
 
TYPE=Bridge
BOOTPROTO=static
DEFROUTE = yes
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.205.109
NETMASK=255.255.255.0
GATEWAY=192.168.205.2
DNS1 = 192.168.1.1
DNS 2 = 119.29.29.29
DNS3=192.168.2.1
PREFIX=24
NM_CONTROLLED=yes
  • We need to type TYPE is set to Bridge

(3) to see if success

Need to restart the network service after configuration is complete

1
2
systemctl restart network
ifconfig

mark

2.5, start libvirtd and messagebus

1
2
systemctl start libvirtd
systemctl start messagebus

View list of network interfaces

1
brctl show

mark

Third, create a virtual machine

3.1, create a virtual machine storage directory

(1) will mount the system disk 50G

1
2
mkfs.xfs -f /dev/sdb
mount /dev/sdb /mnt/

(2) create a virtual machine storage directory

The partition of the directory must be large enough

1
mkdir /mnt/kvm

3.2, create a virtual machine

3.2.1, using the local image file

(1) First, upload the CD files centos-7 to the host

mark

(2) command to create a virtual machine

1
virt-install --name=test01 --memory 512,maxmemory=1024 --vcpus 1,maxvcpus=2 --os-type=linux --os-variant rhel7 --location=/tmp/CentOS-7-x86_64-DVD-1611.iso --disk path=/mnt/kvm/test01.img,size=10 --bridge=br0 --graphics none --console pty,target_type=serial --extra-args="console=tty0 console=ttyS0"
  • –name,指定虚拟机的名字
  • –memory,指定内存大小,maxmemory限制最大内存
  • –vcpus,指定分配CPU数量,maxvcpus限制最大CPU数量
  • –os-type,指定安装系统类型
  • –os-variant,指定系统版本
  • –location,指定安装介质地址,可以是网络地址,也可以是本地的绝对路径,这里为镜像文件路径
  • –disk path,指定虚拟磁盘位置,size指定磁盘大小。默认创建的磁盘文件为qcow2格式。在centos6中默认创建的磁盘文件为raw格式,不能拍摄快照。可以进行先创建qcow2文件,示例:qemu-img create -f qcow2 -o preallocation=metadata /kvm/test001.img 10G; –disk path=/mnt/kvm/test002.qcow2,format=qcow2,size=10,bus=virtio
  • –bridge,指定网络连接类型
  • –graphics none,指定安装通过那种类型安装,我们不需要使用图形安装
  • –console pty,指定控制台类型
  • –extra-args,设定内核参数

(3)配置安装选项

mark

  • 1、设置语言
  • 2、设置时间时区
  • 3、设置安装源
  • 4、设置安装方式(默认为最小化安装)
  • 5、设置安装位置
  • 6、kdump是在系统崩溃、死锁或者死机的时候用来转储内存运行参数的一个工具和服务(默认不安装)
  • 7、设置网卡
  • 8、设置root密码
  • 9、创建用户
  • “q”为退出,”b”为开始安装,”r”为刷新配置

我们需要设置一下选项:

a.设置时区

1
"2"-"1"-"5"-"回车"-"62"

b.设置安装源

1
"3"-"2"-"c"

c.设置安装方式

1
"4"-"c"

d.设置安装位置

1
"5"-"c"-"c"-"c"

e.设置root密码

1
"8"-"输入passwd"-"再次输入passwd"-"yes"
  • 会自动检测密码,如果太弱需要我们确认

f.设置ip,可也可以不设置,等安装完成后再设置

1
"7"-"2"-"1"-"输入ip"-"2"-"输入子网掩码"-"3"-"输入网关"-"6"-"输入DNS"-"7"-"8"-"c"-"c"

g.开始安装

1
"b"
  • 安装时,可能安装源和安装方式还没有读取到。使用”r”刷新即可

  • 经过等待后,系统会自动重启让我们登录系统。

    mark

3.2.2、使用网络安装

1
virt-install --name=test02 --memory 512,maxmemory=1024 --vcpus 1,maxvcpus=2 --os-type=linux --os-variant rhel7 --location=http://mirrors.163.com/centos/7/os/x86_64/ --disk path=/mnt/kvm/test02.img,size=10 --bridge=br0 --graphics none --console pty,target_type=serial --extra-args="console=tty0 console=ttyS0"
  • 将location修改为163的源即可,不过安装时超级慢,因为需要下载软件包。所以建议使用本地镜像文件安装

四、virsh常用操作

libvirt是Linux上的虚拟化库,是长期稳定的C语言API,支持KVM/QEMU、Xen、LXC等主流虚拟化方案。链接:http://libvirt.org/,virsh是libvirt对应的shell命令。

(1)查看子机

1
virsh list --all
  • –all,可以查看到未开启的子机

mark

(2)开启子机

类似我们在VMware开启虚拟机

1
2
3
virsh start test01
#也可以开启后进入系统,使用ctrl+]可以回到宿主机
virsh start test01 --console

(3)关闭

类似我们在VMware关闭虚拟机

1
2
3
4
#需要在借助子机上的acpid服务
virsh shutdown test01
#不需要借助子机上的acpid服务,强制关闭子机
virsh destroy test01

mark

mark

(4)让子机随着宿主机开机自启动

在VMware中,我们还需要手动开启虚拟机。在kvm中,我们可以设置子机自启动

1
2
3
virsh autostart test01
#取消自启动
virsh autostart --disable test01

(5)删除子机

1
2
3
4
5
6
#我们需要先强制关闭子机
virsh destroy test01
#取消对子机的定义,会删除该子机的配置文件
virsh undefine test01
#如果要彻底删除,我们还需要删除虚拟磁盘文件
rm -rf /mnt/kvm/test01.qcow2
  • 子机配置文件位置:/etc/libvirt/qemu/*.xml

(6)恢复子机

如果我们使用virsh undefine test01误删除了子机,只要我们不删除子机配置文件,我们就可以恢复。

我们可以使用该配置文件创建新的子机

首先,我们拷贝其他子机的配置文件

1
virsh dumpxml test02 > /etc/libvirt/qemu/test01.xml

修改新的子机配置文件,修改UUID,修改source file=’/mnt/kvm/test01.qcow2’

重新定义子机

1
virsh define /etc/libvirt/qemu/test01.xml

现在就可以启动了

1
virsh start test01 --console

(7)挂起子机

1
virsh suspend test02

(8)恢复子机

1
2
#主要恢复的是挂起的主机
virsh resume test01

mark

五、克隆虚拟机

(1)克隆命令

1
virt-clone --original test01 --name template --file /mnt/kvm/clone1.qcow2
  • –original,指定克隆的源,源机器需要为关闭状态
  • –name,指定克隆后的子机名称
  • –file,指定克隆子机的虚拟磁盘文件位置

mark

  • 克隆完成后可以直接登录克隆虚拟机
  • virsh start template --console

mark

六、快照管理

6.1、创建快照

(1)创建命令

1
2
virsh snapshot-create test02
virsh snapshot-create test02
  • snapshot-create,后面跟要创建快照的虚拟机

mark

  • 快照文件存放位置为:

(2)创建快照的磁盘文件

在centos6中,默认创建的虚拟机磁盘文件为raw格式,我们可以使用以下命令查看磁盘文件格式

1
qemu-img info /mnt/kvm/test01.img

mark

如果是raw格式的磁盘文件,我们可以执行以下命令修改为qcow2格式

1
2
qemu-img convert -f raw -O qcow2 /mnt/kvm/raw.img /mnt/kvm/raw.qcow2
#然后在修改子机配置文件中的type=qcow2

(3)列出快照

1
virsh snapshot-list test02
  • snapshot-list,后面跟子机名称

mark

6.2、恢复和删除快照

(1)恢复快照

恢复快照的前提是子机需要关闭

1
2
virsh destroy test02
virsh snapshot-revert test02 1510234604
  • snapshot-revert,后面跟快照名称,即列表中的那一行数字

(2)删除快照

1
virsh snapshot-delete test02 1510234894
  • 后面跟子机,还有要删除的快照名称

mark

七、磁盘扩容

centos6创建快照后无法扩容

针对子机test01扩容

(1)查看现有磁盘容量

可以使用查询磁盘信息命令查询

1
qemu-img info /mnt/kvm/test01.qcow2

mark

(2)增加磁盘容量

1
qemu-img resize /mnt/kvm/test01.qcow2 +2G

mark

(3)查看是否增加磁盘

1
2
3
4
#如果子机为running状态,我们需要重启一下
virsh destroy test01
virsh start test01 --console
fdisk -l

mark

(3)挂载增加的磁盘空间

此时使用df -h是无法看到增加的磁盘空间的,所以需要我们分区并格式化

1
2
fdisk /dev/vda
#增加一个新的分区/dev/vda3,容量为我们添加的2G

添加完成后,需要我们将这个/dev/vd3添加到vm里面去

如果/dev/vda3不存在,需要我们重启一下系统

1
2
ls /dev/vda3
reboot

创建物理卷

1
pvcreate /dev/vda3

mark

列出物理卷

1
pvs

mark

  • cl为卷组

将/dev/vda3加入卷组cl

1
vgextend cl /dev/vda3

mark

查看卷组cl

mark

  • 还有2G剩余

查看逻辑卷

1
lvs

mark

将/dev/vda3的容量加入到root中

1
lvextend -l +100%FREE /dev/cl/root

重新获得逻辑卷分区大小

1
xfs_growfs /dev/cl/root
  • centos6使用resize2fs

mark

八、增加磁盘

(1)创建磁盘

1
qemu-img create -f qcow2 /mnt/kvm/test01_2.img 5G

(2)关闭虚拟机

1
virsh destroy test01

(3)编辑test01配置文件

1
2
3
4
5
6
7
8
virsh edit test01
#在原来的disk配置下增加以下配置
 
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/mnt/kvm/test01_2.qcow2'/>
<target dev='vdb' bus='virtio'/>
</disk>
  • dev需要与之前的不同,使用”vdb”

(4)启动虚拟机进行操作

1
2
virsh star tets01 --console
fdisk -l

mark

九、调整CPU内存,网卡

查看子机的硬件信息

1
virsh dominfo test01

mark

9.1、调整CPU

编辑子机配置文件

1
2
3
virsh edit test01
 
#找到vcpu,修改数量

mark

  • 1表示当前CPU数,2表示最大CPU数
  • 如果我们设置的CPU大于最大CPU数,会报错

mark

重新启动子机,使配置生效

1
2
virsh destroy test01
virsh start test01

9.2、调整内存

1
2
3
virsh edit test01
 
#找到currentMemory

mark

重新启动子机,使配置生效

1
2
virsh destroy test01
virsh start test01

9.3、添加网卡

查看子机网卡

1
virsh domiflist test01

mark

增加子机网卡

这种方法适合自己是running状态,并且为在线增加

1
virsh attach-interface test01 --type bridge --source br0

mark此时只存在于内存中,配置文件里添加了相关配置,但是我么已重启自己就没有了,所以我们只需要再执行以下命令即可

1
virsh dumpxml test01 >/etc/libvirt/qemu/test01.xml

也可以在子机不是running状态,我们修改自己配置文件添加网卡

十、kvm虚拟主机迁移

(1)确认配置文件和磁盘文件位置

该方式需要确保虚拟机是关机状态

1
2
3
4
5
virsh shutdown test01
#备份配置文件
virsh dumpxml test01 > /etc/libvirt/qemu/test001.xml
# View the location where the virtual machine disk
virsh domblklist test01

mark

(2) synchronization configuration and disk files

The disk configuration files and file synchronization to the remote machine

1
2
rsync -of /mnt/kvm/test01.* [email protected]: / data / sqm
rsync -av /etc/libvirt/qemu/test001.xml [email protected]:/etc/libvirt/qemu/test001.xml

If it is synchronized to the local, the synchronization also requires us to do the following

1
rsync -of /mnt/kvm/test01.* / data / sqm /
1
2
3
4
vim /etc/libvirt/qemu/test001.xml
Review # <name> </ name>
# Modify UUID
# Modify the disk path is /data/kvm/test01.qcow2,/data/kvm/test01_2.img

Use virsh list --allto view test001

Guess you like

Origin www.cnblogs.com/xuanbjut/p/10994427.html