安装KVM
[root@localhost ~]#yum -y install qemu-kvm libvirt virt-manager virt-install virt-viewer
[root@localhost ~]#systemctl start --now libvirtd
[root@localhost ~]#yum -y install cockpit cockpit-machines
[root@localhost ~]#systemctl enable --now cockpit.socket
#Ubuntu 安装 KVM
apt install qemu-kvm virt-manager libvirt-daemon-system
[root@ubuntu1804 ~]#apt -y install qemu-kvm virt-manager libvirt-daemon-system
#如果没有开启CPU虚拟化功能会提示以下信息
[root@ubuntu1804 ~]#kvm-ok
INFO: Your CPU does not support KVM extensions
KVM acceleration can NOT be used
#添加CPU的虚拟化支持再执行
[root@ubuntu1804 ~]#kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used
[root@localhost ~]#export DISPLAY=10.0.0.1:0.0
[root@localhost ~]#virt-manager
安装虚拟机系统
#创建磁盘文件
qemu-img create -f qcow2 /var/lib/libvirt/images/rocky8.qcow2 20G
#配合磁盘文件和镜像创建虚拟机
virt-install --virt-type kvm --name rocky8 --ram 1024 --vcpus 2 --cdrom=/data/isos/Rocky-8.5-x86_64-minimal.iso --disk path=/var/lib/libvirt/images/rocky8.qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --os-variant=rhel8.2
#创建完成后进入管理界面进行安装
virt-manager
KVM虚拟化管理脚本
#!/bin/bash
#
#********************************************************************
#Author: shuhong
#QQ: 985347841
#Date: 2022-09-13
#FileName: install_nginx.sh
#URL: hhhh
#Description: The test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
. /etc/init.d/functions
Red="\e[1;31m"
Purple="\e[1;35m"
Green="\e[1;32m"
Blue="\e[1;36m"
Yellow="\e[1;33m"
End="\e[0m"
#克隆集群
clone() {
lists=virsh list --all |awk '/.*template.*/{print $2}'
echo "模板清单:"
echo "<-------------------->"
for x in $lists;do
echo -e "$Purple$x$End"
done
echo "<-------------------->"
read -p "请输入克隆模板: " template
#read -p "请输入克隆模板ip: " ip
read -p "请输入集群名称: " name
read -p "请输入节点数: " node
read -p "请输入ip起点x(y.y.y.x): " num
for ((i=0;i<$node;i++)) ;do
let n=$i+1
arg[$i]="$name-0$n"
virt-clone -o $template -n ${arg[$i]} -f /var/lib/libvirt/images/${arg[$i]}.qcow2 || continue
virsh start ${arg[$i]} || continue
sleep 60
echo $num ${arg[$i]}
if [[ $template =~ "ubuntu" ]];then
ssh root@10.0.0.100 "sed -i -r 's@(^[[:space:]]+- 10.0.0.)[0-9]+@\1$num@' /etc/netplan/00-installer-config.yaml;hostnamectl set-hostname ${arg[$i]} ;reboot"
else
ssh root@192.168.100.10 "sed -i -r 's@(^IPADDR=).*@\1192.168.10.$num@' /etc/sysconfig/network-scripts/ifcfg-eth0;hostnamectl set-hostname ${arg[$i]} ;reboot"
fi
let num=$num+1
done
}
#删除集群
delete(){
lists1=virsh list --all | awk 'NR!=1{print $2}'|sed -nr "s/^(.*)-[0-9]+$/\1/p" |uniq
echo "<-------------------->"
for n in $lists1 ;do
echo -e "$Purple$n$End"
done
echo "<-------------------->"
dname=1
echo $dname
read -p "请输入集群名称:" dname
echo $danme
for i in virsh list --all| awk -v dname=$dname '$0 ~ dname{print $2}'
;do
echo -e "$Red$i$End"
if [[ $i =~ "template" ]]; then
continue;
elif [[ $dname == "" ]];then
action "删除失败" false
exit
else
virsh destroy $i &> /dev/null
virsh undefine --remove-all-storage $i &> /dev/null && action "删除成功" true || action "删除失败" false
fi
done
}
#单节点启动
nodestart(){
echo "<-------------------->"
for m in virsh list --all | egrep ".*off$"|awk '{print $2}'
;do
echo -e "$Purple$m$End"
done
echo "<-------------------->"
read -p "请输入节点名称:" startnode
virsh start $startnode &> /dev/null && action "$startnode 节点启动成功" true || action "$startnode 节点启动失败" false
}
#单节点重启
noderestart(){
echo "<-------------------->"
for o in virsh list --all | egrep ".*running$"|awk '{print $2}'
;do
echo -e "$Purple$o$End"
done
echo "<-------------------->"
read -p "请输入节点名称:" restartnode
virsh reboot $restartnode &> /dev/null && action "$restartnode 节点重启成功" true || action "$restartnode 节点重启失败" false
}
#单节点关机
nodestop(){
echo "<-------------------->"
for p in virsh list --all | egrep ".*running$"|awk '{print $2}'
;do
echo -e "$Purple$p$End"
done
echo "<-------------------->"
read -p "请输入节点名称:" stopnode
virsh shutdown $stopnode &> /dev/null && action "$stopnode 节点关机成功" true || action "$stopnode 节点关机失败" false
}
#启动集群
clusterstart(){
lists2=virsh list --all | awk 'NR!=1{print $2}'|sed -nr "s/^(.*)-[0-9]+$/\1/p"|sort -nr|uniq
echo "<-------------------->"
for q in $lists2 ;do
echo -e "$Purple$q$End"
done
echo "<-------------------->"
read -p "请输入集群名称:" dname
for i1 in virsh list --all| awk -v dname=$dname '$0 ~ dname{print $2}'
;do
echo -e "$Red$i1$End"
virsh start $i1 &> /dev/null && action "$i1 节点启动成功" true || action "$i1 节点启动失败" false
done
}
#关闭集群
clusterstop(){
lists3=virsh list --all | awk 'NR!=1{print $2}'|sed -nr "s/^(.*)-[0-9]+$/\1/p" |sort -nr|uniq
echo "<-------------------->"
for r in $lists3 ;do
echo -e "$Purple$r$End"
done
echo "<-------------------->"
read -p "请输入集群名称:" dname
for i2 in virsh list --all| awk -v dname=$dname '$0 ~ dname{print $2}'
;do
echo -e "$Red$i2$End"
virsh shutdown $i2 &> /dev/null && action "$i2 节点关闭成功" true || action "$i2 节点关闭失败" false
done
}
#集群重启
clusterrestart(){
lists4=virsh list --all | awk 'NR!=1{print $2}'|sed -nr "s/^(.*)-[0-9]+$/\1/p" | sort -nr|uniq
echo "<-------------------->"
for s in $lists4 ;do
echo -e "$Purple$s$End"
done
echo "<-------------------->"
read -p "请输入集群名称:" dname
for i3 in virsh list --all| awk -v dname=$dname '$0 ~ dname{print $2}'
;do
echo -e "$Red$i3$End"
virsh reboot $i3 &> /dev/null && action "$i3 节点重启成功" true || action "$i3 节点重启失败" false
done
}
#单机克隆
clone_Single(){
echo "<-------------------->"
for p in virsh list --all |awk '/.*template.*/{print $2}'
;do
echo -e "$Purple$p$End"
done
echo "<-------------------->"
read -p "请输入克隆模板: " templatecs
#read -p "请输入克隆模板ip: " ip
read -p "请输入名称: " csname
read -p "请输入ipx(y.y.y.x): " num1
virt-clone -o $templatecs -n $csname -f /var/lib/libvirt/images/$csname.qcow2 || continue
virsh start $csname || continue
sleep 60
echo $csname
if [[ $templatecs =~ "ubuntu" ]];then
ssh root@10.0.0.100 "sed -i -r 's@(^[[:space:]]+- 10.0.0.)[0-9]+@\1$num1@' /etc/netplan/00-installer-config.yaml;hostnamectl set-hostname $csname ;reboot"
else
ssh root@192.168.100.10 "sed -i -r 's@(^IPADDR=).*@\1192.168.10.$num1@' /etc/sysconfig/network-scripts/ifcfg-eth0;hostnamectl set-hostname $csname ;reboot"
fi
}
#单机删除
delete_Single(){
echo "<-------------------->"
for p in virsh list --all |awk '{print $2}'
;do
echo -e "$Purple$p$End"
done
echo "<-------------------->"
read -p "请输入机器名称:" dsname
echo $dsname
if [[ $i =~ "template" ]]; then
exit;
elif [[ $dsname == "" ]];then
action "删除失败" false
exit
else
virsh destroy $dsname &> /dev/null
virsh undefine --remove-all-storage $dsname &> /dev/null && action "删除成功" true || action "删除失败" false
fi
}
#创建快照
create_snapshot(){
echo "<-------------------->"
for p in virsh list --all |awk '{print $2}'
;do
echo -e "$Purple$p$End"
done
echo "<-------------------->"
read -p "请选择你要创建快照的机器:" shname
virsh snapshot-list $shname
read -p "请输入创建的快照名称:" createsnap
virsh snapshot-create-as $shname $createsnap && echo -e "$Blue 创建成功 $End" || echo -e "$Red 创建失败 $End"
virsh snapshot-list $shname
}
#删除快照
delete_snapshot(){
echo "<-------------------->"
for p in virsh list --all |awk '{print $2}'
;do
echo -e "$Purple$p$End"
done
echo "<-------------------->"
read -p "请输入你要删除快照的机器:" dshname
virsh snapshot-list $dshname
read -p "请输入删除的快照名称:" deletesnap
virsh snapshot-delete $dshname $deletesnap && echo -e "$Blue 删除成功 $End" || echo -e "$Red 删除失败 $End"
virsh snapshot-list $dshname
}
#恢复快照
recover_snapshot(){
echo "<-------------------->"
for p in virsh list --all |awk '{print $2}'
;do
echo -e "$Purple$p$End"
done
echo "<-------------------->"
read -p "请输入你要恢复快照的机器:" rshname
virsh snapshot-list $rshname
read -p "请输入恢复的快照名称:" recoversnap
virsh snapshot-revert $rshname $recoversnap && echo -e "$Blue 恢复成功 $End" || echo -e "$Red 恢复失败 $End"
virsh snapshot-list $rshname
}
virsh list --all
PS3="请按要求输入操作选项:"
select lists in 克隆集群 删除集群 启动集群 关闭集群 集群重启 单节点启动 单节点关机 单节点重启 克隆单机 单机删除 创建快照 删除快照 恢复快照 退出;do
case $REPLY in # 1 2 3 4 5 6 7 8 9 10 11 12 13
1)
echo -e "$Blue$lists$End"
clone
;;
2)
echo -e "$Red$lists$End"
delete
;;
3)
echo -e "$Green$lists$End"
clusterstart
;;
4)
echo -e "$Red$lists$End"
clusterstop
;;
5)
echo -e "$Yellow$lists$End"
clusterrestart
;;
6)
echo -e "$Green$lists$End"
nodestart
;;
7)
echo -e "$Red$lists$End"
nodestop
;;
8)
echo -e "$Yellow$lists$End"
noderestart
;;
9)
echo -e "$Blue$lists$End"
clone_Single
;;
10)
echo -e "$Blue$lists$End"
delete_Single
;;
11)
echo -e "$Blue$lists$End"
create_snapshot
;;
12)
echo -e "$Blue$lists$End"
delete_snapshot
;;
13)
echo -e "$Blue$lists$End"
recover_snapshot
;;
14)
echo -e "$Blue$lists$End"
exit
;;
*)
echo -e "$Red输入错误$End"
;;
esac
done
KVM迁移
#主要迁移一下两个文件
[root@wdy ~]#ll /etc/libvirt/qemu/
total 232
-rw------- 1 root root 4596 Sep 28 20:50 dayu-ubuntu-template.xml
-rw------- 1 root root 4587 Sep 27 14:30 easzlab-deploy-01.xml
-rw------- 1 root root 4620 Sep 27 11:52 easzlab-haproxy-keepalive-01.xml
-rw------- 1 root root 4620 Sep 27 11:53 easzlab-haproxy-keepalive-02.xml
-rw------- 1 root root 4620 Sep 27 11:54 easzlab-haproxy-keepalive-03.xml
-rw------- 1 root root 5562 Sep 29 16:15 easzlab-k8s-ceph-01.xml
-rw------- 1 root root 5562 Sep 29 16:51 easzlab-k8s-ceph-02.xml
-rw------- 1 root root 5562 Sep 29 17:08 easzlab-k8s-ceph-03.xml
-rw------- 1 root root 4593 Sep 27 14:14 easzlab-k8s-etcd-01.xml
-rw------- 1 root root 4593 Sep 27 14:16 easzlab-k8s-etcd-02.xml
-rw------- 1 root root 4593 Sep 27 14:17 easzlab-k8s-etcd-03.xml
-rw------- 1 root root 4599 Sep 27 14:20 easzlab-k8s-harbor-01.xml
-rw------- 1 root root 4599 Sep 27 14:21 easzlab-k8s-harbor-02.xml
-rw------- 1 root root 4599 Sep 27 14:23 easzlab-k8s-harbor-03.xml
-rw------- 1 root root 4599 Sep 27 20:32 easzlab-k8s-master-01.xml
-rw------- 1 root root 4599 Sep 27 21:02 easzlab-k8s-master-02.xml
-rw------- 1 root root 4599 Sep 27 21:03 easzlab-k8s-master-03.xml
-rw------- 1 root root 4596 Sep 27 21:03 easzlab-k8s-node-01.xml
-rw------- 1 root root 4596 Sep 27 21:03 easzlab-k8s-node-02.xml
-rw------- 1 root root 4596 Sep 27 21:03 easzlab-k8s-node-03.xml
-rw------- 1 root root 4581 Oct 11 10:47 MYSQL-Zabbix-01.xml
-rw------- 1 root root 4581 Oct 11 10:49 MYSQL-Zabbix-02.xml
-rw------- 1 root root 4581 Oct 11 10:51 MYSQL-Zabbix-03.xml
drwx------. 3 root root 86 Sep 12 10:09 networks
-rw------- 1 root root 5952 Sep 27 11:52 rocky8.6-template.xml
-rw------- 1 root root 5946 Oct 11 10:01 Rocky-dayu-2-05.xml
-rw------- 1 root root 4596 Sep 26 08:42 ubuntu20.04-template.xml
-rw------- 1 root root 4587 Oct 11 10:39 Zabbix5-server-01.xml
-rw------- 1 root root 4587 Oct 11 10:41 Zabbix5-server-02.xml
-rw------- 1 root root 4587 Oct 11 10:42 Zabbix5-server-03.xml
[root@wdy ~]#ll /var/lib/libvirt/images/
total 385229968
-rw------- 1 root root 4122279936 Oct 4 20:05 dayu-ubuntu-01.qcow2
-rw------- 1 qemu qemu 16493248512 Oct 12 19:23 easzlab-deploy-01.qcow2
-rw------- 1 qemu qemu 4586209280 Oct 12 19:47 easzlab-haproxy-keepalive-01.qcow2
-rw------- 1 qemu qemu 4611768320 Oct 12 19:38 easzlab-haproxy-keepalive-02.qcow2
-rw------- 1 qemu qemu 4534763520 Oct 12 19:40 easzlab-haproxy-keepalive-03.qcow2
-rw------- 1 qemu qemu 214781394944 Oct 12 19:47 easzlab-k8s-ceph-01-1.qcow2
-rw------- 1 qemu qemu 107390828544 Oct 12 18:47 easzlab-k8s-ceph-01-2.qcow2
-rw------- 1 root root 107390828544 Sep 29 16:16 easzlab-k8s-ceph-01-3.qcow2
-rw------- 1 qemu qemu 9596829696 Oct 12 19:47 easzlab-k8s-ceph-01.qcow2
-rw------- 1 qemu qemu 214781394944 Oct 12 18:47 easzlab-k8s-ceph-02-1.qcow2
-rw------- 1 qemu qemu 107390828544 Oct 12 18:47 easzlab-k8s-ceph-02-2.qcow2
-rw------- 1 qemu qemu 9595060224 Oct 12 19:47 easzlab-k8s-ceph-02.qcow2
-rw------- 1 qemu qemu 214781394944 Oct 12 19:47 easzlab-k8s-ceph-03-1.qcow2
-rw------- 1 qemu qemu 107390828544 Oct 12 18:47 easzlab-k8s-ceph-03-2.qcow2
-rw------- 1 qemu qemu 9586016256 Oct 12 19:47 easzlab-k8s-ceph-03.qcow2
-rw------- 1 qemu qemu 5156241408 Oct 12 19:47 easzlab-k8s-etcd-01.qcow2
-rw------- 1 qemu qemu 5214109696 Oct 12 19:47 easzlab-k8s-etcd-02.qcow2
-rw------- 1 qemu qemu 5231673344 Oct 12 19:47 easzlab-k8s-etcd-03.qcow2
-rw------- 1 qemu qemu 9658236928 Oct 12 19:47 easzlab-k8s-harbor-01.qcow2
-rw------- 1 qemu qemu 4468375552 Oct 12 19:47 easzlab-k8s-harbor-02.qcow2
-rw------- 1 qemu qemu 4567990272 Oct 12 19:24 easzlab-k8s-harbor-03.qcow2
-rw------- 1 qemu qemu 9827450880 Oct 12 19:47 easzlab-k8s-master-01.qcow2
-rw------- 1 qemu qemu 9708240896 Oct 12 19:47 easzlab-k8s-master-02.qcow2
-rw------- 1 qemu qemu 9696509952 Oct 12 19:47 easzlab-k8s-master-03.qcow2
-rw------- 1 qemu qemu 10365960192 Oct 12 19:47 easzlab-k8s-node-01.qcow2
-rw------- 1 qemu qemu 10340859904 Oct 12 19:47 easzlab-k8s-node-02.qcow2
-rw------- 1 qemu qemu 10627907584 Oct 12 19:47 easzlab-k8s-node-03.qcow2
-rw------- 1 qemu qemu 10190258176 Oct 12 19:35 MYSQL-Zabbix-01.qcow2
-rw------- 1 qemu qemu 8570732544 Oct 12 19:28 MYSQL-Zabbix-02.qcow2
-rw------- 1 qemu qemu 9089974272 Oct 12 19:19 MYSQL-Zabbix-03.qcow2
-rw------- 1 root root 32217432064 Oct 4 20:05 rocky8.6-template.qcow2
-rw------- 1 root root 3900571871 Oct 11 10:04 Rocky-dayu-2-05.qcow2
-rw------- 1 root root 4115464192 Sep 27 10:42 ubuntu20.04-template.qcow2
-rw------- 1 qemu qemu 4812570624 Oct 12 19:45 Zabbix5-server-01.qcow2
-rw------- 1 qemu qemu 4749459456 Oct 12 19:45 Zabbix5-server-02.qcow2
-rw------- 1 qemu qemu 4763287552 Oct 12 19:45 Zabbix5-server-03.qcow2