26.KVM
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了26.KVM相关的知识,希望对你有一定的参考价值。
1.搭建kvm1.1环境准备
10.0.0.11 centos7
优化:
kvm01在安装系统的时候,不要使用自动分区,自动分区使用的LVM分区,在使用kvm的时候,特别卡!
关闭selinux
关闭firewalld
1.2安装
打开虚拟机的虚拟化功能
设置主机名
[root@git ~]# hostnamectl set-hostname kvm01
安装:
yum install libvirt virt-install qemu-kvm -y
介绍:
libvirt服务:管理kvm虚拟机的生命周期
virt-install工具:创建安装虚拟机
qemu-kvm工具:使用qemu-img为虚拟机提供硬盘
Linux上几种虚拟化软件
qemu 软件纯模拟全虚拟化软件,特别慢!
xen 性能特别好,需要使用专门修改之后的内核,兼容性差
KVM 需要cpu支持虚拟化,基于内核,不需要使用专门的内核,兼容行好,性能较好
安装kvm虚拟机准备条件
在window上安装TightVNC
tightvnc官网:http://www.tightvnc.com
vnc是一个跨平台的远程桌面软件,待会安装kvm虚拟机系统的时候使用
启动libvirtd服务
systemctl start libvirtd.service
systemctl status libvirtd.service
Apr 13 12:50:13 git systemd[1]: Started Virtualization daemon.
Apr 13 12:50:16 git dnsmasq[1791]: started, version 2.76 cachesize 150
Apr 13 12:50:16 git dnsmasq[1791]: compile time options: IPv6 GNU-getopt DBus no-i18n IDN DHCP DHCPv6...otify
Apr 13 12:50:16 git dnsmasq-dhcp[1791]: DHCP, IP range 192.168.122.2 -- 192.168.122.254, lease time 1h
Apr 13 12:50:16 git dnsmasq-dhcp[1791]: DHCP, sockets bound exclusively to interface virbr0
Apr 13 12:50:16 git dnsmasq[1791]: reading /etc/resolv.conf
Apr 13 12:50:16 git dnsmasq[1791]: using nameserver 223.5.5.5#53
Apr 13 12:50:16 git dnsmasq[1791]: read /etc/hosts - 2 addresses
Apr 13 12:50:16 git dnsmasq[1791]: read /var/lib/libvirt/dnsmasq/default.addnhosts - 0 addresses
Apr 13 12:50:16 git dnsmasq-dhcp[1791]: read /var/lib/libvirt/dnsmasq/default.hostsfile
Hint: Some lines were ellipsized, use -l to show in full
安装kvm虚拟机
上传镜像
/opt/CentOS-7-x86_64-DVD-1708.iso
创建磁盘
[root@git ~]# qemu-img create -f raw /opt/centos2.raw 10G
Formatting ‘/opt/centos2.raw‘, fmt=raw size=10737418240
建议虚拟机内存不要低于1024M,否则安装系统特别慢!
[root@git ~]# virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos7 --memory 1024 --vcpus 1 --disk /opt/centos2.raw,format=raw,size=10 --cdrom /opt/CentOS-7-x86_64-DVD-1708.iso --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
Starting install...
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
创建好后,赶快连接tightVNC
接下来就是安装系统界面和相关操作
关闭这里,防止占用内存
一定要打开网络,否则还需要自己设置ip
安装好后,重启
1.3管理虚拟机
**列表list**
[root@git ~]# virsh list
Id Name State
[root@git ~]# virsh list --all
Id Name State
- centos7 shut off
**开机start**
[ root@git ~]# virsh start centos7
Domain centos7 started
[root@git ~]# virsh list
Id Name State
2 centos7 running
** 关机shutdown**
[root@git ~]# virsh shutdown centos7
Domain centos7 is being shutdown
[root@git ~]# virsh list --all
Id Name State
- centos7 shut off
**拔电源关机destroy**
[root@git ~]# virsh start centos7
Domain centos7 started
[root@git ~]# virsh list
Id Name State
3 centos7 running
[root@git ~]# virsh destroy centos7
Domain centos7 destroyed
[root@git ~]# virsh list --all
Id Name State
- centos7 shut off
**导出配置dumpxml**
配置文件路径
[root@git ~]# ll /etc/libvirt/qemu/centos7.xml
[root@git ~]# virsh dumpxml centos7>centos7.xml
**删除undefine**
推荐:先destroy,在undefine
[root@git ~]# virsh undefine centos7
Domain centos7 has been undefined
**导入配置define**
[root@git ~]# virsh define centos7.xml
Domain centos7 defined from centos7.xml
**修改配置edit**
带有语法检查功能
[root@git ~]# mv /opt/centos2.raw /opt/centos7.raw
[root@git ~]# virsh start centos7
error: Failed to start domain centos7
error: Cannot access storage file ‘/opt/centos2.raw‘: No such file or directory
[root@git ~]#
[root@git ~]# virsh edit centos7
Domain centos7 XML configuration edited.
[root@git ~]# virsh start centos7
Domain centos7 started
**重命名domrename**
注意:低版本不支持
[root@git ~]# virsh domrename centos7 centos7r
error: Requested operation is not valid: cannot rename active domain
[root@git ~]# virsh destroy centos7
Domain centos7 destroyed
[root@git ~]# virsh domrename centos7 centos7r
Domain successfully renamed
**挂起suspend**
[root@git ~]# netstat -antlp|grep 5900
tcp 0 0 0.0.0.0:5900 0.0.0.0:* LISTEN 12516/qemu-kvm
[root@git ~]#
[root@git ~]# virsh start centos7r
[root@git ~]# virsh suspend centos7r
Domain centos7r suspended
**恢复resume**
取消挂起
[root@git ~]# virsh resume centos7r
Domain centos7r resumed
**查询vnc端口号vncdisplay**
每次新加一个虚拟机,端口号会加1
第一个虚拟机端口号是5900
[root@git ~]# virsh vncdisplay centos7r
:0
**开机启动autostart**
操作系统起来了,上面的虚拟机就开机启动
[root@git ~]# virsh autostart centos7r
Domain centos7r marked as autostarted
[root@git ~]# ll /etc/libvirt/qemu/autostart/
total 0
lrwxrwxrwx 1 root root 30 Apr 13 14:50 centos7r.xml -> /etc/libvirt/qemu/centos7r.xml
[root@git ~]# virsh autostart --disable centos7r
Domain centos7r unmarked as autostarted
[root@git ~]# ll /etc/libvirt/qemu/autostart/
total 0
[root@git ~]#
1.4console登录
为什么要使用console连接?
1:常规情况下,安装完 KVM 之后,可能都会通过 VNC 连接到 KVM 虚拟机里面去修改 IP 等信息。但是一旦虚拟机比较多的话,打开过多的端口会造成安全问题
2:很多时候,我们是通过跳板机连接的宿主机,你的window和kvm宿主机没有直达的路由,这时候vnc都用不了,如何快速进入到 KVM 虚拟机里面去排查问题呢?
在centos7的kvm虚拟机中执行:
grubby --update-kernel=ALL --args="console=ttyS0,115200n8"
reboot重启生效
centos6的虚拟机如何实现console连接呢?
没那么简单了!
[root@kvm01 ~]# virsh console centos7r
Connected to domain centos7r
Escape character is ^]---提示退出虚拟机是按ctrl+]
按回车
1.5kvm虚拟机磁盘两种常用的格式
raw:裸格式,占用空间比较大,不适合远程传输,不支持快照功能,性能较好
qcow2:cow(copy on write)占用空间小,适合传输,支持快照,性能比raw差一点点
qemu-img的常用命令
创建虚拟磁盘
qemu-img create test.raw 10G
qemu-img create -f qcow2 test.qcow2 10G
查看虚拟磁盘信息
qemu-img info test.raw
调整虚拟磁盘容量大小
qemu-img resize test.raw +5G
磁盘格式转换
qemu-img convert -f raw -O qcow2 test.raw oldboy.qcow2
将现有的虚拟机的磁盘格式由raw转换为qcow2,并测试运行
[root@kvm01 ~]# qemu-img convert -f raw -O qcow2 /opt/centos7.raw /opt/centos7.qcow2
[root@kvm01 opt]# virsh edit centos7r
Domain centos7r XML configuration not changed.
<driver name=‘qemu‘ type=‘qcow2‘/>
<source file=‘/opt/centos7.qcow2‘/>
[root@kvm01 opt]# virsh destroy centos7r
Domain centos7r destroyed
[root@kvm01 opt]# virsh start centos7r
Domain centos7r started
[root@kvm01 opt]# virsh list
Id Name State
3 centos7r running
1.6快照管理
创建快照
virsh snapshot-create centos7
查看快照
virsh snapshot-list centos7
还原快照
virsh snapshot-revert centos7 --snapshotname 1516574134
删除快照
virsh snapshot-delete centos7 --snapshotname 1516636570
[root@kvm01 opt]# virsh snapshot-create centos7r
Domain snapshot 1555146019 created
[root@kvm01 opt]# virsh snapshot-list centos7r
Name Creation Time State
1555146019 2019-04-13 17:00:19 +0800 running
[root@kvm01 opt]# virsh snapshot-delete centos7r --snapshotname 1555146019
Domain snapshot 1555146019 deleted
[root@kvm01 opt]# virsh snapshot-list centos7r
Name Creation Time State
[root@kvm01 opt]#
[root@kvm01 opt]# ll
total 8145304
-rw-r--r-- 1 qemu qemu 2161836032 Apr 13 17:08 centos7.qcow2
-rw-r--r-- 1 root root 10737418240 Apr 13 16:55 centos7.raw
-rw-r--r-- 1 qemu qemu 4521459712 Apr 13 13:32 CentOS-7-x86_64-DVD-1708.iso
drwxr-xr-x 10 root root 212 Mar 28 08:53 gitlab
[root@kvm01 opt]# virsh snapshot-create centos7r
创建了快照之后,qcow2文件的大小变大了
Domain snapshot 1555146646 created
[root@kvm01 opt]# ll
total 8402772
-rw-r--r-- 1 qemu qemu ** 2165309440** Apr 13 17:10 centos7.qcow2
-rw-r--r-- 1 root root 10737418240 Apr 13 16:55 centos7.raw
-rw-r--r-- 1 qemu qemu 4521459712 Apr 13 13:32 CentOS-7-x86_64-DVD-1708.iso
drwxr-xr-x 10 root root 212 Mar 28 08:53 gitlab
[root@kvm01 opt]#
[root@kvm01 opt]# virsh snapshot-delete centos7r --snapshotname 1555146646
Domain snapshot 1555146646 deleted
删除快照,qcow2文件的大小不会变小
[root@kvm01 opt]# ll
total 8150104
-rw-r--r-- 1 qemu qemu 2165309440 Apr 13 17:12 centos7.qcow2
-rw-r--r-- 1 root root 10737418240 Apr 13 16:55 centos7.raw
-rw-r--r-- 1 qemu qemu 4521459712 Apr 13 13:32 CentOS-7-x86_64-DVD-1708.iso
drwxr-xr-x 10 root root 212 Mar 28 08:53 gitlab
[root@kvm01 opt]#
1.7克隆
1.完整克隆
实现方法:
1) 命令方式
[root@kvm01 opt]# virt-clone -o centos7r --auto-clone
Allocating ‘centos7-clone.qcow2‘ | 10 GB 00:00:13
Clone ‘centos7r-clone‘ created successfully.
[root@kvm01 opt]# virsh list --all
Id Name State
- centos7r shut off
- centos7r-clone shut off
[root@kvm01 opt]# virsh start centos7r-clone
Domain centos7r-clone started
[root@kvm01 opt]# virsh vncdisplay centos7r-clone
:1
2) 手动方式
1:克隆虚拟磁盘文件
cp /opt/centos7.qcow2 /opt/centos7-chain-clone.qcow2
2:生成新的虚拟机配置文件
centos7r是现有的一个虚拟机名称
virsh dumpxml centos7r >/root/centos7r.xml
vim /root/centos7r.xml
name修改
uuid删掉
disk路径/opt/centos7-chain-clone.qcow2
mac地址删除
3:测试启动
[root@kvm01 opt]#virsh define /root/centos7r.xml
[root@kvm01 opt]# virsh start centos7r-chain-clone
Domain centos7r-chain-clone started
[root@kvm01 opt]# virsh list
Id Name State
4 centos7r running
5 centos7r-clone running
7 centos7r-chain-clone running
2.链接克隆
实现方法:
shell脚本
[root@kvm01 opt]# qemu-img create -f qcow2 -b /opt/centos7.qcow2 /opt/chain.qcow2
Formatting ‘/opt/chain.qcow2‘, fmt=qcow2 size=10737418240 backing_file=‘/opt/centos7.qcow2‘ encryption=off cluster_size=65536 lazy_refcounts=off
[root@kvm01 opt]# virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name chain --memory 1024 --vcpus 1 --disk /opt/chain.qcow2,format=qcow2,size=10 --boot hd --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
注意:
--boot hd 这里可以这样写,并能成功启动,主要在于--disk /opt/chain.qcow2,这里的disk文件一定要是已经安装好的系统的磁盘文件,可以是复制的一个
Starting install...
Domain creation completed.
[root@kvm01 opt]#
[root@kvm01 opt]# virsh list --all
Id Name State
4 centos7r running
5 centos7r-clone running
7 centos7r-chain-clone running
8 chain running
1.8桥接
1.8.1创建桥接网络
1:准备磁盘
复制一个现有的搭建好系统的磁盘
[root@kvm01 opt]# qemu-img create -f qcow2 -b /opt/centos7.qcow2 /opt/bridge.qcow2
创建桥接网络
2:virsh iface-bridge eth0 br0
创建不成功可以手动设置配置文件
3:基于桥接网络创建虚拟机
virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name bridge --memory 1024 --vcpus 1 --disk /opt/bridge.qcow2,format=qcow2,size=10 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole
注意:
--boot hd 这里可以这样写,并能成功启动,主要在于--disk /opt/chain.qcow2,这里的disk文件一定要是已经安装好的系统的磁盘文件,可以是复制的一个
[root@kvm01 opt]# virsh vncdisplay bridge
:1
4.TightVNC登录
windows上面ping 10.0.0.130
[d:~]$ ping 10.0.0.130
正在 Ping 10.0.0.130 具有 32 字节的数据:
来自 10.0.0.130 的回复: 字节=32 时间=1ms TTL=64
来自 10.0.0.130 的回复: 字节=32 时间=1ms TTL=64
5.10.0.0.130 的 Ping 统计信息:
数据包: 已发送 = 2,已接收 = 2,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 1ms,最长 = 1ms,平均 = 1ms
将已有的虚机修改为桥接网络
1.新建一个虚拟机
新建磁盘
[root@kvm01 network-scripts]# qemu-img create -f qcow2 -b /opt/centos7.qcow2 /opt/web01.qcow2
Formatting ‘/opt/web01.qcow2‘, fmt=qcow2 size=10737418240 backing_file=‘/opt/centos7.qcow2‘ encryption=off cluster_size=65536 lazy_refcounts=off
新建虚拟机
[root@kvm01 network-scripts]# virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web01 --memory 1024 --vcpus 1 --disk /opt/web01.qcow2,format=qcow2,size=10 --boot hd --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
Starting install...
Domain creation completed.
[root@kvm01 network-scripts]# virsh list --all
Id Name State
15 centos7r running
16 bridge running
17 web01 running
[root@kvm01 network-scripts]# virsh vncdisplay web01
:2
2.编辑配置文件
停虚拟机
[root@kvm01 network-scripts]# virsh destroy web01
Domain web01 destroyed
编辑配置文件
[root@kvm01 network-scripts]# virsh edit web01
<interface type=‘bridge‘>
<mac address=‘52:54:00:55:aa:fa‘/>
<source bridge=‘br0’/>
启动虚拟机
[root@kvm01 network-scripts]# virsh start web01
Domain web01 started
查看ip
1.8.2 default网络的由来
[root@kvm01 network-scripts]# virsh
Welcome to virsh, the virtualization interactive terminal.
Type: ‘help‘ for help with commands
‘quit‘ to quit
virsh # net-list
Name State Autostart Persistent
default active yes yes
virsh # net-edit default
Network default XML configuration not changed.
<network>
<name>default</name>
<uuid>49812939-ec1e-42db-8c12-501e57024a73</uuid>
<forward mode=‘nat‘/>
<bridge name=‘virbr0‘ stp=‘on‘ delay=‘0‘/>
<mac address=‘52:54:00:18:6b:a6‘/>
<ip address=‘192.168.122.1‘ netmask=‘255.255.255.0‘>
<dhcp>
<range start=‘192.168.122.2‘ end=‘192.168.122.254‘/>
</dhcp>
</ip>
</network>
[root@kvm01 network-scripts]# iptables -t nat -L -n
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
1.8.3kvm NAT模式网络原理图
1.8.4kvm 桥接网络原理图
1.9热添加技术
热添加技术就是不停机的情况下,在线热添加硬盘,内存,cpu,网卡等设备,
热添加技术一般都是在虚拟机资源不够了,又不能停机的情况下使用的,
热添加技术是虚拟机相对于物理机的一个很大的优势,它让资源分配变得更灵活!
使用的虚拟机是上面创建的桥接的web01
ip为10.0.0.131,可通过xshell连接
1.9.1热添加硬盘
创建硬盘
qemu-img create -f qcow2 centos7-add01.qcow2 5G
确保是运行状态
[root@kvm01 ~]# virsh list
Id Name State
15 centos7r running
16 bridge running
18 web01 running
热添加硬盘
virsh attach-disk web01 /opt/centos7-add01.qcow2 vdb --live --cache=none --subdriver=qcow2
xshell连接10.0.0.131
[root@localhost ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c1ccf
Device Boot Start End Blocks Id System
/dev/vda1 2048 20965375 10481664 83 Linux
Disk /dev/vdb: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
新加的磁盘分区-格式化-挂载
[root@localhost ~]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xecaffaf0.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +sizeK,M,G (2048-10485759, default 10485759):
Using default value 10485759
Partition 1 of type Linux and of size 5 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]#mkfs.xfs /dev/vdb1
meta-data=/dev/vdb1 isize=512 agcount=4, agsize=327616 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=1310464, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]# mount /dev/cdb /mnt
mount: special device /dev/cdb does not exist
[root@localhost ~]# mount /dev/vdb1 /mnt
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 10G 971M 8.1G 11% /
devtmpfs 487M 0 487M 0% /dev
tmpfs 497M 0 497M 0% /dev/shm
tmpfs 497M 6.6M 490M 2% /run
tmpfs 497M 0 497M 0% /sys/fs/cgroup
tmpfs 100M 0 100M 0% /run/user/0
/dev/vdb1 5.0G 33M 5.0G 1% /mnt
[root@localhost ~]#
磁盘不够用了
kvm虚拟机131中取消挂载
[root@localhost ~]# umount /mnt
宿主机上剥离刚刚新建的磁盘
[root@kvm01 ~]# virsh detach-disk web01 vdb
Disk detached successfully
kvm虚拟机131中查看是否还有vdb
[root@localhost ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c1ccf
Device Boot Start End Blocks Id System
/dev/vda1 2048 20965375 10481664 83 Linux
宿主机上,给磁盘加10G
[root@kvm01 ~]# qemu-img resize /opt/centos7-add01.qcow2 +10G
Image resized.
[root@kvm01 ~]# qemu-img info /opt/centos7-add01.qcow2
image: /opt/centos7-add01.qcow2
file format: qcow2
virtual size: 15G (16106127360 bytes)
disk size: 13M
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
[root@kvm01 ~]#
加到kvm虚拟机中
[root@kvm01 ~]# virsh attach-disk web01 /opt/centos7-add01.qcow2 vdb --live --cache=none --subdriver=qcow2
Disk attached successfully
kvm虚拟机131中查看
[root@localhost ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c1ccf
Device Boot Start End Blocks Id System
/dev/vda1 2048 20965375 10481664 83 Linux
Disk /dev/vdb: 16.1 GB, 16106127360 bytes, 31457280 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xecaffaf0
Device Boot Start End Blocks Id System
/dev/vdb1 2048 10485759 5241856 83 Linux
[root@localhost ~]#
再重新挂载
[root@localhost ~]# mount /dev/vdb1 /mnt
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 10G 971M 8.1G 11% /
devtmpfs 487M 0 487M 0% /dev
tmpfs 497M 0 497M 0% /dev/shm
tmpfs 497M 6.6M 490M 2% /run
tmpfs 497M 0 497M 0% /sys/fs/cgroup
tmpfs 100M 0 100M 0% /run/user/0
/dev/vdb1 5.0G 33M 5.0G 1% /mnt
磁盘还是5G 删除原分区,新建分区
[root@localhost ~]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): d
Selected partition 1
Partition 1 is deleted
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-31457279, default 2048):
Using default value 2048
Last sector, +sectors or +sizeK,M,G (2048-31457279, default 31457279):
Using default value 31457279
Partition 1 of type Linux and of size 15 GiB is set
Command (m for help): p
Disk /dev/vdb: 16.1 GB, 16106127360 bytes, 31457280 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xecaffaf0
Device Boot Start End Blocks Id System
/dev/vdb1 2048 31457279 15727616 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@localhost ~]#
[root@localhost ~]# partprobe /dev/vdb
Error: Partition(s) 1 on /dev/vdb have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use. As a result, the old partition(s) will remain in use. You should reboot now before making further changes.
[root@localhost ~]# reboot
[root@localhost ~]# mount /dev/vdb1 /mnt/
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 10G 971M 8.1G 11% /
devtmpfs 487M 0 487M 0% /dev
tmpfs 497M 0 497M 0% /dev/shm
tmpfs 497M 6.6M 490M 2% /run
tmpfs 497M 0 497M 0% /sys/fs/cgroup
tmpfs 100M 0 100M 0% /run/user/0
/dev/vdb1 5.0G 33M 5.0G 1% /mnt
[root@localhost ~]# partprobe /dev/vdb
此时磁盘还是5G
[root@localhost ~]# xfs_growfs /mnt/
meta-data=/dev/vdb1 isize=512 agcount=4, agsize=327616 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=1310464, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 1310464 to 3931904
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 10G 971M 8.1G 11% /
devtmpfs 487M 0 487M 0% /dev
tmpfs 497M 0 497M 0% /dev/shm
tmpfs 497M 6.6M 490M 2% /run
tmpfs 497M 0 497M 0% /sys/fs/cgroup
tmpfs 100M 0 100M 0% /run/user/0
/dev/vdb1 15G 33M 15G 1% /mnt
此时磁盘变为了15G
上面是临时生效,如果想永久生效,需要修改配置文件
1.9.2热添加网卡
下面是临时生效,如果想永久生效,需要修改配置文件
准备新磁盘,是在搭建好系统的磁盘基础上新建磁盘
[root@kvm01 opt]# qemu-img create -f qcow2 -b /opt/centos7.qcow2 /opt/web04.qcow2
Formatting ‘/opt/web04.qcow2‘, fmt=qcow2 size=10737418240 backing_file=‘/opt/centos7.qcow2‘ encryption=off cluster_size=65536 lazy_refcounts=off
新建虚拟机
[root@kvm01 opt]# virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web04 --memory 1024 --vcpus 1 --disk /opt/web04.qcow2,format=qcow2,size=10 --boot hd --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
Starting install...
Domain creation completed.
查看端口,为5903
[root@kvm01 opt]# virsh vncdisplay web04
:3
现有网卡
新加网卡
宿主机上执行
virsh attach-interface web04 --type bridge --model virtio --source br0
或
virsh attach-interface web04 --type network --model virtio --source br0
virtio一方面网卡效率高,另一方面,网卡名称是eth0方式,否则就是ens名称
永久生效
[root@kvm01 opt]# virsh destroy web04
Domain web04 destroyed
[root@kvm01 opt]# virsh edit web04
Domain web04 XML configuration edited.
<interface type=‘network‘>
<mac address=‘52:54:00:8b:3a:0c‘/>
<source network=‘default‘/>
<model type=‘virtio‘/>
<address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x03‘ function=‘0x0‘/>
</interface>
<interface type=‘network‘>
<source network=‘default‘/>
<model type=‘virtio‘/>
</interface>
[root@kvm01 opt]# virsh start web04
Domain web04 started
查看kvm虚拟机中的网卡
1.9.3热添加cpu
要在安装虚拟机的时候设置 --vcpus 1,maxvcpus=4
安装参数:
virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web04 --memory 1024 --vcpus 1,maxvcpus=4 --disk /opt/web04.qcow2,format=qcow2,size=10 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole
热添加cpu
virsh setvcpus web04 --count=2
登录kvm虚拟机查看
1.9.4热添加内存
安装参数
virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web04 --memory 512,maxmemory=2048 --vcpus=1,maxvcpus=2 --disk /opt/web04.qcow2,format=qcow2,size=10 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole
热添加内存
virsh setmem web04 1G
1.10热迁移
1.10.1什么是热迁移
相比KVM虚拟机冷迁移中需要拷贝虚拟机虚拟磁盘文件,kvm虚拟机热迁移无需拷贝虚拟磁盘文件,但是需要迁移到的宿主机之间需要有相同的目录结构虚拟机磁盘文件,也就是共享存储,这次通过大家熟悉的nfs来实现,当然也可以采用Glusterfs等分布式文件系统来实现
1.10.2为什么要使用热迁移
上一节我们学习了在线热添加技术,就很容易理解了。假设我初级只有一台16G内存的物理机,为了充分利用资源,我可能运行了8台2G内存的虚拟机,
然后访问量增加,虚拟机的2G内存不够用了,需要扩容,扩容之前我们就需要先迁移一部分虚拟机到其他宿主机上了,有的业务特别核心,暂停的时间不能太长,这时候就要用到我们的热迁移了。
1.10.3热迁移的过程
假设我们有2台宿主机kvm01和kvm02,在kvm01上挂起虚拟机vm01,发送vm的虚拟机配置文件和运行时内存中的数据到kvm02, 接受完毕,kvm02恢复vm01,热迁移完成。
热迁移环境要求
热迁移步骤
一:在kvm01和kvm02上安装kvm和nfs,配置桥接网卡
二:在nfs01上安装配置nfs
三:kvm01和kvm02挂载共享目录/opt
四:安装一台基于桥接模式的虚拟机
五:在kvm01上安装图形界面、vnc服务端和virt-manager
六:启动vnc服务端
七:使用vnc连接宿主机,使用virt-manager进行迁移
说明:详情可以访问的博客https://www.qstack.com.cn/archives/368.html
安装过程
注意:需要互相做好host解析
操作步骤:
一:在kvm01和kvm02上安装kvm和nfs,配置桥接网卡
yum install libvirt* virt-* qemu-kvm* nfs-utils openssh-askpass -y
systemctl start libvirtd.service
virsh iface-bridge eth0 br0
二:在nfs01上安装配置nfs
yum install nfs-utils -y
mkdir /data
vim /etc/exports
/data 10.0.0.0/24(rw,async,no_root_squash,no_all_squash)
systemctl restart rpcbind
systemctl restart nfs
三:kvm01和kvm02挂载共享目录/opt
mount -t nfs 10.0.0.11:/data /opt
四:安装一台基于桥接模式的虚拟机
virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name --memory 1024 --vcpus 1 --disk /opt/oldboy.qcow2,format=qcow2,size=10 --cdrom /opt/CentOS-7-x86_64-DVD-1708.iso --network bridge=br0 --graphics vnc,listen=0.0.0.0 –noautoconsole
将虚拟机ip配置为10.0.0.111
五:在kvm01上安装图形界面、vnc服务端和virt-manager
yum groups install "GNOME Desktop" -y
注意:
安装过程如遇报错:Transaction check error:
file /boot/efi/EFI/centos from install of fwupdate-efi-12-5.el7.centos.x86_64 conflicts with file from package grub2-common-1:2.02-0.65.el7.centos.2.noarch
用yum upgrade -y只升级所有包,不升级软件和系统内核。
之后再yum groups install "GNOME Desktop" -y
yum install tigervnc-server.x86_64 -y
yum install virt-manager -y
六:启动vnc服务端
vncserver :1 启动5901端口的vnc服务端
vncserver -kill :1 关闭5901端口的vnc服务端
注意:我在操作的时候,发现启动后总是被自动kill掉
[root@kvm01 .vnc]# pwd
/root/.vnc
[root@kvm01 .vnc]# cat kvm01:1.log
Xvnc TigerVNC 1.8.0 - built Nov 2 2018 19:05:14
Copyright (C) 1999-2017 TigerVNC Team and many others (see README.txt)
See http://www.tigervnc.org for information on TigerVNC.
Underlying X server release 12001000, The X.Org Foundation
Sat Apr 13 23:59:12 2019
vncext: VNC extension running!
vncext: Listening for VNC connections on all interface(s), port 6000
vncext: created VNC server for screen 0
Killing Xvnc process ID 31465
原因是
[root@kvm01 .vnc]# cat xstartup
#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
/etc/X11/xinit/xinitrc
#vncserver -kill $DISPLAY
不知道为什么,脚本中带有一行自动Kill的命令,注释掉吧
[root@kvm01 .vnc]# netstat -antlp|grep 59
tcp 0 0 0.0.0.0:5900 0.0.0.0:* LISTEN 4984/qemu-kvm
tcp 0 0 0.0.0.0:5901 0.0.0.0:* LISTEN 32523/Xvnc
tcp6 0 0 :::5901 :::* LISTEN 32523/Xvnc
[root@kvm01 .vnc]# cat xstartup
七:使用vnc连接宿主机,使用virt-manager进行迁移
这时候会提醒输入密码,就是之前第6步的时候设置的vnc连接密码
迁移成功
在迁移的过程中,使用ping虚拟机的ip,发现只丢了一个包
以上是关于26.KVM的主要内容,如果未能解决你的问题,请参考以下文章