1.虚拟化
1.1概念
将计算机的各种实体资源(CPU、内存、磁盘空间、网络适配器等),予以抽象 、转换后呈现出来并可供分区、组合为一个或多个电脑配置环境。
1.2应用数据化的原因
1)同一台物理机运行多个不同版本应用软件
2)硬件依赖性较低
3)便于数据迁移
1.3虚拟化的优势
1)提高应用兼容性
2)降低运营成本
3)加速应用部署
4)提高服务器可用性
5)提高资源利用率
6)动态调度资源
7)降低能源消耗
2.KVM
2.1KVM
1)KVM是开源软件
2)是x86架构且硬件支持虚拟化技术(如intel VT或AMD-V)的Linux全虚拟化解决方案
3)包含为处理器提供底层虚拟化可加载的核心模块kvm.ko(kvm-intel.ko或kvm-AMD.ko)
4)需要经过修改的QEMU软件(qemu-kvm),作为虚拟机上层控制和界面
5)能在不改变linux或windows镜像的情况下同时运行多个虚拟机,(它的意思是多个虚拟机使用同一镜像)并为每一个虚拟机配置个性化硬件环境(网卡、磁盘、图形适配器……)同时KVM还能够使用ksm技术帮助宿主服务器节约内存。
2.2Virtual Machine Manager
1)虚拟机管理员,可以让用户管理多个虚拟机
2)创建、编辑、引导或停止虚拟机
3)查看并控制每个虚拟机的控制台
4)查看每部虚拟机的性能及使用率
5)查看每部正在运行中的虚拟机以及主控端的即时性及使用率信息
6)无论在本机或远程,皆可使用KVM、Xen、QEMU
3.KVM的部署
3.1环境准备
[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[[email protected] ~]# uname -r
3.10.0-693.el7.x86_64
[[email protected] ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.101 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::af24:1fe3:9292:9747 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:2c:94:03 txqueuelen 1000 (Ethernet)
RX packets 714 bytes 132837 (129.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 247 bytes 35367 (34.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:2c:94:0d 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
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 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
[[email protected] ~]# getenforce
Disabled
[[email protected] ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
3.2安装相关软件包
[[email protected] ~]# yum install qemu-kvm libvirt virt-install virt-manager bridge-utils
说明:qemu-kvm ----模拟计算机的工具,为KVM虚拟机提供IO设备
libvirt ----管理虚拟机
virt-install ----命令行的虚拟机创建安装工具
bridge-utils ----网桥工具
3.4启动
[[email protected] ~]# systemctl start libvirtd
方法一:使用命令行安装部署
3.4创建网桥
[[email protected] ~]# nmcli c add type bridge autoconnect yes con-name br0 ifname br0
Connection 'br0' (f8cd2a6d-1683-4174-825a-76d79b8cefcd) successfully added.
[[email protected] ~]# nmcli c modify br0 ipv4.addresses 10.0.0.101/24 ipv4.method manual
[[email protected] ~]# nmcli c modify br0 ipv4.gateway 10.0.0.254
[[email protected] ~]# nmcli c delete eth0
[[email protected] ~]# nmcli c add type bridge-slave autoconnect yes con-name eth0 ifname eth0 master br0
3.5重启
[[email protected] ~]# reboot
3.6创建安装磁盘
[[email protected] ~]# mkdir -p /var/linux/images
[[email protected] ~]# qemu-img create -f qcow2 /var/linux/images/centos74.img 20G
Formatting '/var/linux/images/centos71.img', fmt=qcow2 size=21474836480 encryption=off cluster_size=65536 lazy_refcounts=off
3.7安装虚拟机
[[email protected] ~]# virt-install --name centos71 --ram 512 --vcpus 2 --disk path=/var/linux43/images/centos71.img,size=10 --os-type linux --os-variant rhel7 --graphics none --console pty,target_type=serial --location=/var/CentOS-7-x86_64-DVD-1708.iso --extra-args 'console=ttyS0,115200n8 serial'
方法二:VNC
3.4下载VNC(官网:http://www.tightvnc.com)
3.5创建安装磁盘
[[email protected] ~]# qemu-img create -f qcow2 /var/linux/images/centos742.img 20G
3.6安装虚拟机(NAT模式)
virt-install --virt-type=kvm --name=c72 --vcpus=1 -r 1024 -c /var/CentOS-7-x86_64-DVD-1503-01.iso --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole --os-type=linux --os-variant=rhel7 --disk path=/var/linux/images/centos742.img,size=20,format=qcow2
3.7Windows下VNC连接
4.KVM的相关命令
4.1退出虚拟机
ctrl + ]
4.2进入虚拟机
virsh console centos74
4.3查看虚拟机的及状态
virsh list
virsh list --all
4.4启动虚拟机
virsh start centos74
4.5关闭虚拟机
virsh shutdown centos74
4.6强制关闭虚拟机
virsh destroy centos74
4.7删除虚拟机
virsh shutdown centos74
virsh undefine centos74
5.磁盘管理
5.1查看当前虚拟机硬盘信息
[[email protected] ~]# qemu-img info /var/linux/images/centos74.img
image: /var/linux/images/centos74.img
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 1.2G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false
5.2转换磁盘格式(qcow2转换为raw)
[[email protected] ~]# qemu-img convert -f qcow2 -O raw /var/linux/images/centos74.img /var/linux/images/centos74.img
5.3KVM虚拟机添加硬盘
5.3.1创建一块新的硬盘
[[email protected] ~]# qemu-img create -f qcow2 /var/linux/images/centos74-add.qcow2 2G
5.3.2查看创建的硬盘信息
[[email protected] ~]# qemu-img info /var/linux/images/centos74-add.qcow2
image: /var/linux/images/centos74-add.qcow2
file format: qcow2
virtual size: 2.0G (2147483648 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false
5.3.3为虚拟机添加硬盘
[[email protected] ~]# virsh attach-disk centos74 /var/linux/images/centos74-add.qcow2 vdb --live --cache=none --subdriver=qcow2
Disk attached successfully
说明:vdb 第二块硬盘
--live 热添加
--sudbriver 驱动类型
detach-disk 分离磁盘设备
attach-disk 附加磁盘设备
5.4格式化磁盘(在KVM虚拟机中操作)
[[email protected] ~]# mkfs.xfs /dev/vdb
6.快照管理
6.1创建快照
[[email protected] ~]# virsh snapshot-create cnetos74
6.2查看主机快照列表
[[email protected] ~]# virsh snapshot-list centos74
6.3查看快照信息
[[email protected] ~]# virsh snapshot-info centos74 --spanshotname 1516607756
6.4还原快照
[[email protected] ~]# virsh snapshot-revert centos74 --spanshotname 1516607756
6.5删除快照
[[email protected] ~]# virsh snapshot-delete centos74 --spanshotname 1516607756
7.KVM虚拟机克隆
[[email protected] ~]# virsh-clone --auto-clone -o centos74
8.KVM虚拟机冷迁移
8.1在KVM02中安装kvm组件
[[email protected] ~]# yum install -y libvirt* virt-* qemu-kvm*
8.2配置桥接网络
[[email protected] 02~]# virsh iface-bridge eth0 br0
8.3KVM主机将虚拟机关闭,导出配置文件
[[email protected] ~]# virsh shutdown centos74
[[email protected] ~]# virsh dumpxml centos74 > centos74.xml
8.4将虚拟文件传输到KVM02上
[[email protected] ~]# scp -rp centos74.xml 10.0.0.102:/data
8.5KVM02主机导入文件
[[email protected] ~]# virsh define centos74.xml
8.6启动虚拟机
[[email protected] ~]# virsh start centos74
9.KVM虚拟机热迁移
9.1KVM虚拟机热迁移的核心
共享存储。这里使用NFS共享存储(详情参考:http://blog.51cto.com/13520761/2087720)
9.1安装virt-manager所需桌面及vnc-server
[[email protected] ~]# yum groupinstall "GNOME Desktop" -y
[[email protected] ~]# yum install -y tigervnc-server
[[email protected] ~]# yum install -y opnssh-askpass
9.2复制vnc配置文件
[[email protected] ~]# cp /usr/lib/systemd/system/[email protected] /usr/lib/systemd/system/[email protected]\:1.server
9.3修改配置文件
[[email protected] ~]#
egrep -v "^#|^$" /usr/lib/systemd/system/[email protected]\:1.service
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[Service]
Type=forking
User=root
ExecStartPre=-/usr/bin/vncserver -kill %i
ExecStart=/usr/bin/vncserver %i
PIDFile=/root/.vnc/%H%i.pid
ExecStop=-/usr/bin/vncserver -kill %i
[Install]
WantedBy=multi-user.target
9.4设置vnc连接时的密码
[[email protected] ~]# vncpasswd
9.5启动vnc服务
[[email protected] ~]# systemctl start [email protected]\:1.services
9.6在KVM主机上部署NFS服务
9.7在KVM02主机上挂载目录
[[email protected] ~]# mount -t nfs 172.16.1.101:/var/linux/images /var/linux/images
9.8vnc连接KVM宿主机
9.9添加KVM02宿主机
9.10主机热迁移
9.11在KVM02上查看虚拟主机
[[email protected] ~]# virsh list --all
原文地址:http://blog.51cto.com/13520761/2106973