CentOS7配置KVM

CentOS7配置KVM

准备

系统环境

inet1inet2主机名系统配置
192.169.72.201202.207.240.201kvm-01CentOS7.5.1804MEM:4G CPU:2C/2P DISK:15G

CentOS7镜像

CentOS-7-x86_64-DVD-1804.iso

创建相关目录

mkdir -p /server/tools		 # 存放CentOS-7-x86_64-DVD-1804.iso
mkdir -p /server/scripts	 # 存放自定义脚本
mkdir /data					# 存放磁盘文件
开启VMware的CPU虚拟化功能

在这里插入图片描述

安装命令补全工具并重启

yum install bash-completion -y

reboot

安装

yum install libvirt virt-install qemu-kvm -y

systemctl start libvirtd
systemctl enable libvirtd
  • libvirt: 虚拟机管理软件(kvm,xen,qemu,lxc)
  • virt: virt-install virt-clone 虚拟机的安装工具和克隆工具
  • qemu-kvm qemu-img(qcow2,raw) 管理虚拟机的虚拟磁盘

KVM创建

从镜像创建

  • 创建虚拟机
virt-install --virt-type kvm --os-type=linux --os-variant rhel7 \
--name centos7 --memory 1024 --vcpus 1 --disk /data/centos.qcow2,format=qcow2,size=6 \
--cdrom /server/tools/CentOS-7-x86_64-DVD-1804.iso --network network=default \
--graphics vnc,listen=0.0.0.0 --noautoconsole

在这里插入图片描述

  • 参数说明
--virt-type kvm		# 指定虚拟化类型(qemu/kvm)
--os-type=linux		# 系统类型
--os-variant rhel7	# 系统版本
--name centos7		# 虚拟机的名字
--memory 1024		# 内存大小
--vcpus 1			# 处理器数量
--disk /opt/centos.raw,format=raw,size=6	# 硬盘类型(qcow2,raw),存放位置,以及大小(G)
--cdrom /opt/CentOS-7-x86_64-DVD-1804.iso	# 指定光驱文件
--network network=default	# 指定网络类型,默认是nat模式
--graphics vnc,listen=0.0.0.0	# 指定终端输出设备
--noautoconsole
  • vnc软件连接 安装系统

注意:(1)云主机一般不需要交换分区、(2)网卡设置开机自启动、(3)关闭kdump和sercurity plicy等、(4)修改时区

在这里插入图片描述

从克隆文件创建

前提,有完整克隆或者链接克隆带有系统的磁盘文件(通过qemu-img创建)

  • 创建带有系统的磁盘文件
qemu-img create -f qcow2 -b /data/centos.qcow2 /data/centos-vm.qcow2
  • 从克隆磁盘文件创建KVM
virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos-vm \
--memory 512,maxmemory=2048 --vcpus 1,maxvcpus=5 --disk /data/centos-vm.qcow2 --boot hd \
--network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole
--memory 512,maxmemory=2048					# 指定最小内存为512M,最大内存为2048M
--vcpus 1,maxvcpus=5						# 指定CPU最小个数为1,最大个数为5
--disk /data/centos-vm.qcow2				# 指定虚拟机的硬盘文件
--boot hd									# 指定从硬盘加载KVM

KVM相关命令

虚拟机管理命令

virsh list --all				# 查看所有的虚拟机
virsh start centos7				# 启动虚拟机
virsh shutdown centos7			# 正常关机虚拟机
virsh destroy centos7			# 强制关机(相当于拔电源)
virsh reboot centos7	  		# 重启
virsh domrename centos7 nginx-01		# 虚拟机重命名(注意:必须关机重命名,开机状态下重命名会失败)
virsh suspend centos7			# 挂起虚拟机
virsh resume centos7			# 从挂起状态恢复
virsh autostart centos7			# 设置kvm虚拟机开机自启动(本质就是给.xml文件创建软连接 /etc/libvirt/qemu/autostart/)
virsh autostart --disable centos7	# 取消虚拟机开机自启动(删除创建的软连接)

虚拟机配置文件管理命令

virsh dumpxml centos7 >/data/centos7.xml			 # 导出虚拟机配置文件
virsh define /data/centos7.xml					    # 导入配置文件(虚拟机文件迁移后,需要使用该命令导入虚拟机)
virsh undefine centos7 							    # 删除虚拟机(此时使用virsh list --all 查看已经找不到了)
virsh edit centos7								   # 修改虚拟机的配置文件(比如备份磁盘文件后需要修改路径等)

虚拟机运行时终端端口使用查询

 virsh vncdisplay centos7
:0

# :0表示 5900
# :1表示 5901
# 。。。 

配置console登录

  • 进入kvm虚拟机,修改内核参数,并重启

    grubby --update-kernel=ALL --args="console=ttyS0,115200n8"
    
    reboot
    
  • 终端测试

    virsh console centos7		# 如果没有出现登录页面,多进行几次回车
    

    在这里插入图片描述

KVM磁盘管理

磁盘格式

  • qcow2: (cow copy in write)占用空间小,支持快照,性能比raw差一些,方便传输
  • raw: 裸格式,占用空间比较大,不支持快照功能,性能好,不方便传输

查看磁盘

[root@kvm-01 ~]# qemu-img info /data/centos.qcow2 
image: /data/centos.qcow2
file format: qcow2
virtual size: 6.0G (6442450944 bytes)
disk size: 1.5G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true

创建磁盘

# 创建一个名字为test.qcow2的硬盘,格式为qcow2,容量2G
[root@kvm-01 ~]# qemu-img create -f qcow2 /data/test.qcow2 2G
Formatting '/data/test.qcow2', fmt=qcow2 size=2147483648 encryption=off cluster_size=65536 lazy_refcounts=off
  • -f 指定创建的磁盘格式

磁盘扩容

# 将以上容量为2G的test.qocw2硬盘容量增加3G
[root@kvm-01 ~]# qemu-img resize /data/test.qcow2 +3G
Image resized.
[root@kvm-01 ~]# qemu-img info /data/test.qcow2 
image: /data/test.qcow2
file format: qcow2
virtual size: 5.0G (5368709120 bytes)
disk size: 260K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false

注意:

  • qcow2格式只能进行+扩容,raw格式可以进行+扩容和-缩容
  • 不管什么格式的硬盘,都要遵守只扩容,不缩容;避免数据丢失

磁盘格式转化

  1. 转磁盘格式
# 将test.qcow2硬盘从格式qcow2转为raw格式
[root@kvm-01 ~]# qemu-img convert -f qcow2 -O raw /data/test.qcow2 /data/test.raw
[root@kvm-01 ~]# qemu-img info /data/test.raw 
image: /data/test.raw
file format: raw
virtual size: 5.0G (5368709120 bytes)
disk size: 0
  1. 修改虚拟机配置文件
<disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/data/centos7.qcow2'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>

KVM快照管理

  • raw不支持快照,qcow2支持快照
  • 快照保存在qcow2的磁盘文件中,占用一定的磁盘空间(方便迁移)

创建快照

  1. 默认方式(创建的快照以unix的时间戳命名)

    # 给centos7创建默认快照,生成的名字是1657883658
    [root@kvm-01 ~]# virsh snapshot-create centos7
    Domain snapshot 1657883658 created
    
  2. 自定义快照名称方式

    # 给centos7虚拟机创建一个名为snap-01的快照
    [root@kvm-01 ~]# virsh snapshot-create-as --name snap-01 centos7
    Domain snapshot snap-01 created
    

    –name 指定快照的名称

查看快照

[root@kvm-01 ~]# virsh snapshot-list centos7 
 Name                 Creation Time             State
------------------------------------------------------------
 1657883658           2022-07-15 19:14:18 +0800 running
 snap-01              2022-07-15 19:16:02 +0800 running

删除快照

[root@kvm-01 ~]# virsh snapshot-delete centos7 --snapshotname 1657883658
Domain snapshot 1657883658 deleted

snapshot-delete centos7 指定删除centos7虚拟机的快照

–snapshotname 1657883658 指定删除快照的名称

还原快照

[root@kvm-01 ~]# virsh snapshot-revert centos7 --snapshotname snap-01 
[root@kvm-01 ~]# echo $?
0

snapshot-revert centos7 指定对centos7虚拟机进行还原快照

–snapshotname snap-01 指定还原的快照名称是snap-01

KVM克隆管理

完整克隆

  • 完整克隆是对源KVM的完整拷贝,克隆后与源KVM无关
  • KVM的完整克隆分为自动克隆和手动克隆
  • KVM克隆时虚拟机必须关机或挂起
  1. 自动完整克隆
virt-clone --auto-clone -o centos7 -n centos7-clone01

在这里插入图片描述

  1. 手动完整克隆(可以编写完整克隆的脚本)
  • 拷贝虚拟机磁盘文件
[root@kvm-01 ~]# cp /data/centos.qcow2 /data/centos-clone02.qcow2
  • 导出现有KVM配置文件,并修改
virsh dumpxml centos7 >/data/centos7-clone02.xml
sed -ri "s|(<name>)(.*)(</name>)|\1centos7-clone02\3|g" /data/centos7-clone02.xml
sed -i "/<uuid>/d" /data/centos7-clone02.xml
sed -i "/<mac address/d" /data/centos7-clone02.xml
sed -ri "s|(<source file=')(.*)('/>)|\1/data/centos-clone02.qcow2\3|g" /data/centos7-clone02.xml 
  • 导入配置文件(克隆的KVM)
virsh define /data/centos7-clone02.xml 

在这里插入图片描述

  • 启动测试
virsh start centos7-clone02

在这里插入图片描述

链接克隆

  • 链接克隆是基于源虚拟机的增量拷贝,其中基础时源虚拟机,克隆后的变化存放在新创建的链接磁盘文件中
  • kvm默认没有创建链接克隆的命令,需要后动实现链接克隆
  1. 生成链接克隆磁盘文件

    qemu-img create -f qcow2 -b /data/centos.qcow2 /data/centos7-linkclone03.qcow2
    

在这里插入图片描述

  1. 导出源虚拟机配置文件,并修改

    virsh dumpxml centos7 >/data/centos7-linkclone03.xml
    sed -ri "s|(<name>)(.*)(</name>)|\1centos7-linkclone03\3|g" /data/centos7-linkclone03.xml 
    sed -i "/<uuid>/d" /data/centos7-linkclone03.xml
    sed -i "/<mac address/d" /data/centos7-linkclone03.xml
    sed -ri "s|(<source file=')(.*)('/>)|\1/data/centos7-linkclone03.qcow2\3|g" /data/centos7-linkclone03.xml
    
  2. 导入配置文件(导入虚拟机)

    virsh define /data/centos7-linkclone03.xml
    

    在这里插入图片描述

  3. 启动测试

    virsh start centos7-linkclone03
    

    在这里插入图片描述

  • 编写链接克隆的脚本
#!/bin/bash
#Author:Wjz
#Email:1072002783@qq.com
#Blog:https://blog.csdn.net/weixin_51720652
#Time:2022-07-14 15:11:09
#Name:virsh-linkcolne.sh
#Description:create the linkclone kvm virtual machine

[ -f /etc/init.d/functions ] && source /etc/init.d/functions

# define vars
SOURCE_NAME=$1
LINKCLONE_NAME=$2

# judege the params nums
if [ $# -ne 2 ];then
    echo "USAGE: $0 source-vm-name linkclone-vm-name"
    exit 6
fi

# judge the source vm is exist?
RET_VAL1=`/usr/bin/virsh list --all|grep -w $SOURCE_NAME|wc -l`
if [ $RET_VAL1 -ne 1 ];then
    action "source vm is not exist!" /bin/false 
    exit 1
fi

# get the absulate path about the source img
SOURCE_DISK=`/usr/bin/virsh dumpxml $SOURCE_NAME |grep "source file"|awk -F"'" '{print $2}'`
DISK_DIR=`dirname $SOURCE_DISK`  #get the disk dirname

# create the virtual disk file of linkclone kvm virtual machine
/usr/bin/qemu-img create -f qcow2 -b $SOURCE_DISK ${DISK_DIR}/${LINKCLONE_NAME}.qcow2 &>/dev/null
if [ $? -ne 0 ];then
    action "create the linkclone vm disk" /bin/false
    exit 2
else
    action "create the linkclone vm disk" /bin/true
fi

# generate the virtual machine configure file
/usr/bin/virsh dumpxml $SOURCE_NAME >/tmp/${LINKCLONE_NAME}.xml
if [ $? -ne 0 ];then
    action "generate the linkclone vm configure file" /bin/false
    exit 3
else
    action "generate the linkclone vm configure file" /bin/true
fi

# modify the file
sed -ri "s|(<name>)(.*)(</name>)|\1${LINKCLONE_NAME}\3|g" /tmp/${LINKCLONE_NAME}.xml
sed -i '/<uuid>/d' /tmp/${LINKCLONE_NAME}.xml
sed -i '/<mac address/d' /tmp/${LINKCLONE_NAME}.xml
sed -ri "s|(<source file=')(.*)('/>)|\1${DISK_DIR}/${LINKCLONE_NAME}.qcow2\3|g" /tmp/${LINKCLONE_NAME}.xml
if [ $? -ne 0 ];then
    action "edit the linkclone vm configure file" /bin/false
    exit 4
else
    action "edit the linkclone vm configure file" /bin/true
fi

# define the config file
/usr/bin/virsh define /tmp/${LINKCLONE_NAME}.xml &>/dev/null
/usr/bin/virsh start ${LINKCLONE_NAME} &>/dev/null
if [ $? -ne 0 ];then
    action "start the linkclone vm" /bin/false
    exit 5
else
    action "start the linkclone vm" /bin/true
fi
exit 0

KVM网络管理

桥接网络

原理图:

在这里插入图片描述

  1. 创建桥接网卡

    virsh iface-bridge ens33 br0			# 创建桥接网卡,指定桥接的宿主机网卡是ens33,指定桥接后的名称的br0
    systemctl restart network				# 桥接后重启网络服务(如果以上桥接网卡不生效)
    
    virsh iface-unbridge br0				# 取消桥接网卡
    

    在这里插入图片描述

  2. 利用克隆磁盘文件创建桥接网络类型VM

    # 创建带有系统的克隆磁盘文件
    qemu-img create -f qcow2 -b /data/centos.qcow2 /data/centos-bridge.qcow2
    
    # 创建桥接KVM
    virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos-bridge \
    --memory 1024 --vcpus 1 --disk /data/centos-bridge.qcow2 --boot hd --network bridge=br0 \
    --graphics vnc,listen=0.0.0.0 --noautoconsole
    

    ​ --network bridge=br0 指定网络为桥接类型并指定使用的桥接网卡是br0

在这里插入图片描述

  1. 查看VM网络

    在这里插入图片描述

  2. 利用VM配置文件修改网络类型

# virsh edit xxx
<interface type='network'>			# 将network 该为 bridge
      <mac address='52:54:00:dc:40:13'/>
      <source network='default'/>		# 改为 <source bridge='br0'>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>

 <interface type='bridge'>
 <source bridge='br0'>

NAT网络

原理图:

在这里插入图片描述

  1. NAT模式是KVM默认的网络模式

查看默认的iptables

[root@kvm-01 ~]# iptables -t nat -nL
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
RETURN     all  --  192.168.122.0/24     224.0.0.0/24        
RETURN     all  --  192.168.122.0/24     255.255.255.255     
MASQUERADE  tcp  --  192.168.122.0/24    !192.168.122.0/24     masq ports: 1024-65535
MASQUERADE  udp  --  192.168.122.0/24    !192.168.122.0/24     masq ports: 1024-65535
MASQUERADE  all  --  192.168.122.0/24    !192.168.122.0/24    

iptables发现nat模式配置了转发规则

NAT模式和Bridge模式共存

原理图:

在这里插入图片描述

在创建完了bridge网络基础上,需要添加内核参数,实现NAT模式的外网访问

echo 'net.ipv4.ip_forward = 1' >>/etc/sysctl.conf

sysctl -p

KVM热添加技术

热添加硬盘

  1. 创建虚拟硬盘

    qemu-img create -f qcow2 /data/centos-add.qcow2 5G
    
  2. 将虚拟硬盘附加到指定主机

    virsh attach-disk centos7 --subdriver qcow2 /data/centos-add.qcow2 vdb		# 临时附加,立马生效
    virsh attach-disk centos7 --subdriver qcow2 /data/centos-add.qcow2 vdb --config		# 永久附加,下一次开机生效
    

    –subdriver qcow2 指定附加硬盘的格式

    在这里插入图片描述

  3. 进入KVM进行分区、格式化、挂载、更新硬盘块

    • 进入KVM查看硬盘添加情况

      在这里插入图片描述

    • 格式化硬盘(这里省略分区)

      mkfs.ext4 /dev/vdb		# 将整个硬盘格式化为ext4类型
      

      在这里插入图片描述

    • 挂载,查看挂载情况

      mkdir /data
      mount /dev/vdb /data
      df -h
      

      在这里插入图片描述

    • 更新块

      resize2fs /dev/vdb		#(ext4格式)
      xfs_growfs /dev/vdb		#(xfs格式)
      

      在这里插入图片描述

以下步骤是对虚拟硬盘再次调整容量后的操作步骤,这里省略

  1. 进入KVM进行卸载因公安,进入终端剥离硬盘

    virsh detach-disk centos7 vdb				# 临时剥离,立即生效
    virsh detach-disk centos7 vdb --config		# 永久剥离,下一次开机生效
    
  2. 终端调整虚拟硬盘容量,重新附加硬盘

  3. 进入KVM重新挂载并更新硬盘块

热添加网卡

virsh attach-interface centos7 --type bridge --source br0 --model virtio				#临时添加网卡,桥接模式
virsh attach-interface centos7 --type bridge --source br0 --model virtio --config		# 永久添加网卡,桥接模式
virsh detach-interface centos7 --type bridge --mac 52:54:00:84:f0:b4 --config		# 永久删除网卡
  • –type bridge 指定添加的网卡类型为bridge
  • –source br0 指定要桥接的网桥是br0
  • –mode virtio 指定VM要呈现的网络设备模式/模型
  • –config 永久生效

热添加内存

热添加内存默认是不支持的,需要在创建KVM的时候指定特定的参数

–memory 1024,maxmemory=2048 指定最大内存和最小内存

  1. 创建可调整内存大小的KVM
qemu-img create -f qcow2 -b /data/centos.qcow2 /data/centos-resize-mem.qcow2

virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos-resize-mem \
--memory 512,maxmemory=2048 --vcpus 1 --disk /data/centos-resize-mem.qcow2 --boot hd --network bridge=br0 \
--graphics vnc,listen=0.0.0.0 --noautoconsole

Starting install...
Domain creation completed.
[root@kvm-01 data]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 4     centos7-linkclone03            running
 5     centos-bridge                  running
 6     centos7                        running
 7     centos-resize-mem              running
  1. 查看配置文件,检查当前内存和最大内存大小
[root@kvm-01 data]# virsh dumpxml centos-resize-mem |head -5|tail -2
  <memory unit='KiB'>2097152</memory>
  <currentMemory unit='KiB'>524288</currentMemory>
  1. 调整内存大小,查看配置文件,检查调整内存后的当前内存和最大内存信息
virsh setmem centos-resize-mem 1024					# 临时调整内存大小
virsh setmem centos-resize-mem 1024 --config		# 永久调整内存大小(不能超过预设的最大值)

[root@kvm-01 data]# virsh dumpxml centos-resize-mem |head -5|tail -2
  <memory unit='KiB'>2097152</memory>
  <currentMemory unit='KiB'>174932</currentMemory>

发现内存有所变化.

热添加CPU

默认不支持,需要在创建KVM的时候指定特定的参数

–vcpus 1,maxvcpus=10 设置cpu的最小个数和最大个数

  1. 创建可调整cpu数量的KVM
qemu-img create -f qcow2 -b /data/centos.qcow2 /data/centos-resize-vcpus.qcow2

virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos-resize-vcpus \
--memory 1024 --vcpus 1,maxvcpus=5 --disk /data/centos-resize-vcpus.qcow2 --boot hd --network bridge=br0 \
--graphics vnc,listen=0.0.0.0 --noautoconsole

Starting install...
Domain creation completed.


[root@kvm-01 data]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 4     centos7-linkclone03            running
 5     centos-bridge                  running
 6     centos7                        running
 8     centos-resize-mem              running
 9     centos-resize-vcpus            running
  1. 查看配置文件,检查当前cpu
[root@kvm-01 data]# virsh dumpxml centos-resize-vcpus |head -6|tail -1
  <vcpu placement='static' current='1'>5</vcpu>

发现当前cpu的格式是1,最大数量是5

  1. 调整cpu个数,查看配置文件,检查cpu信息
virsh setvcpus centos-resize-vcpus 3 
virsh setvcpus centos-resize-vcpus 3 --config

[root@kvm-01 data]# virsh dumpxml centos-resize-vcpus |head -6|tail -1
  <vcpu placement='static' current='3'>5</vcpu>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值