kvm系统要求
1核心
2G内存
6G硬盘
# 1.查看硬件是否支持虚拟化
lscpu # 查看cpu信息
# Virtualization: VT-x
# Intel CPU:VT-x
# AMD CPU: AMD-v
# 如果没有就在bios中开启
# 2.查看操作系统是否支持
egrep 'svm|vmx' /proc/cpuinfo # 在flags中包含svm或者vmx表示支持
# 3.查看内核模块是否有kvm
# 如果没有该模块,零时使用 `modprobe kvm`加载kvm模块(模块存在于/lib/modules/xxxx/kernel目录下);
# 永久生效,在/etc/modules-load.d/ 创建.conf的文件,并把模块名写在文件中
lsmod | grep kvm
虚拟化主机部署
Centos
yum grouplist # 查看支持的分组以及已安装的分组
#实际安装的组可以通过 yum groupinfo '虚拟化*' 查看安装的分组以及分组包含的软件包
# 安装的主要软件包 yum install virt-manager qemu-img qemu-kvm qemu-kvm-tools libvirt virt-install bridge-utils
yum -y groupinstall '虚拟化*'
# 虚拟化主机部署验证
# 查看libvirtd的状态
systemctl status libvirtd
lsmod | grep kvm
# 开启ipv4转发
# CentOS防火墙开启masquerade后即开启包转发功能
# 也可以通过命令sysctl net.ipv4.ip_forward=1手动开启包转发功能。
firewall-cmd --add-masquerade # 当前开启
firewall-cmd --permanent --add-masquerade # 永久开启
# 查看ipv4转发是否开启
sysctl net.ipv4.ip_forward # 如果=1为开启,=0则未开启
yum -y install iptables-services # 安装iptables
systemctl disable firewalld.service
systemctl stop firewalld.service
systemctl enable iptables
systemctl start iptables
# 清空iptables规则
iptables -F
iptables -t nat -F
iptables -t mangle -F
iptables -t raw -F
iptables -t secure -F
systemctl restart libvirtd # 将kvm虚拟机的转发规则重新写入iptables
Ubuntu
sudo apt update
sudo apt upgrade
sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virtinst virt-manager virt-top
kvm相关安装包及其作用
qemu-kvm 主要的KVM程序包
python-virtinst 创建虚拟机所需要的命令行工具和程序库
virt-manager GUI虚拟机管理工具
virt-top 虚拟机统计命令
virt-viewer GUI连接程序,连接到已配置好的虚拟机
libvirt C语言工具包,提供libvirt服务
libvirt-client 虚拟客户机提供的C语言工具包
virt-install 基于libvirt服务的虚拟机创建命令
bridge-utils 创建和管理桥接设备的工具
虚拟机安装
virt-install 命令行方式安装
安装命令
qemu-img create -f qcow2 /localVM/tp/ubuntuTP.qcow2 40G
virt-install \
--name ubuntuTP \
--vcpus 1,maxvcpus=4 \
--disk path=/localVM/tp/ubuntuTP.qcow2 \
--os-variant=ubuntu22.04 \
--memory 2048,maxmemory=8192 \
--cdrom=/myfile/iso/ubuntu-22.04-live-server-amd64.iso \
--graphics vnc,password=asdqwe@,listen=0.0.0.0,port=5920,keymap=en-us \
--network network=default,model=virtio,driver.iommu=on \
--noautoconsole # 不要自动尝试连接到客户控制台
virt-install \
--name centosTP \
--vcpus 1,maxvcpus=4 \
--disk path=/vm/tml/centosTP.qcow2 \
--os-variant=centos7 \
--memory 1024,maxmemory=8192 \
--cdrom=/public/CentOS-7-x86_64-Minimal-2009.iso \
--graphics vnc,password=asdqwe@,listen=0.0.0.0,port=5920,keymap=en-us \
--network network=default,model=virtio,driver.iommu=on \
--network bridge=brwifi,model=virtio,driver.iommu=on \
--noautoconsole
#--disk 先用qume-img创建磁盘,disk选项只指定path引用就可以,如果不预先创建,这里指定参数创建,则会指定多大就会占用多大空间
#--os-variant virt-install --osinfo list 查看支持的系统标识
# 安装windows如果需要支持半虚拟化驱动disk
--disk path=/vm/win10.qcow2,bus=virtio \
--disk path=半虚拟化驱动的路径,device=floppy \
--network bridge=virbr0,model=virtio
磁盘操作
qemu-img create -f qcow2 /disk1.qcow2 40G # 创建磁盘
virsh attach-disk dom名字 - -source 创建的磁盘路基 - -target vdb - -cache writeback - -subdriver qcow2 - -persistent # 向vm添加磁盘
参数解读(virt-install --help)
使用 virt-install --option=? 查看此选项的详细设置
-n NAME, --name NAME Name of the guest instance
--memory MEMORY Configure guest memory allocation. Ex:
--memory 1024 (in MiB)
--memory memory=1024,currentMemory=512
--vcpus VCPUS Number of vCPUs to configure for your guest. Ex:
--vcpus 5
--vcpus 5,maxvcpus=10,cpuset=1-4,6,8
--vcpus sockets=2,cores=4,threads=2
--cpu CPU CPU model and features. Ex:
--cpu coreduo,+x2apic
--cpu host-passthrough
--cpu host
--metadata METADATA Configure guest metadata. Ex:
--metadata name=foo,title="My pretty title",uuid=...
--metadata description="My nice long description"
--xml XML Perform raw XML XPath options on the final XML. Example:
--xml ./cpu/@mode=host-passthrough
--xml ./devices/disk[2]/serial=new-serial
--xml xpath.delete=./clock
Installation Method Options:
--cdrom CDROM CD-ROM installation media
-l LOCATION, --location LOCATION
Distro install URL, eg. https://host/path. See man page for specific distro examples.
--pxe Boot from the network using the PXE protocol
--import Build guest around an existing disk image
-x EXTRA_ARGS, --extra-args EXTRA_ARGS
Additional arguments to pass to the install kernel booted from --location
--initrd-inject INITRD_INJECT
Add given file to root of initrd from --location
--unattended [UNATTENDED]
Perform an unattended installation
--install INSTALL Specify fine grained install options
--reinstall DOMAIN Reinstall existing VM. Only install options are applied, all other VM configuration options are ignored.
--cloud-init [CLOUD_INIT]
Perform a cloud image installation, configuring cloud-init
--boot BOOT Configure guest boot settings. Ex:
--boot hd,cdrom,menu=on
--boot init=/sbin/init (for containers)
--idmap IDMAP Enable user namespace for LXC container. Ex:
--idmap uid.start=0,uid.target=1000,uid.count=10
OS options:
--os-variant OS_VARIANT, --osinfo OS_VARIANT
The OS being installed in the guest.
This is used for deciding optimal defaults like VirtIO.
Example values: fedora29, rhel7.0, win10, ...
Use '--osinfo list' to see a full list.
Device Options:
--disk DISK Specify storage with various options. Ex.
--disk size=10 (new 10GiB image in default location)
--disk /my/existing/disk,cache=none
--disk device=cdrom,bus=scsi
--disk=?
-w NETWORK, --network NETWORK
Configure a guest network interface. Ex:
--network bridge=mybr0
--network network=my_libvirt_virtual_net
--network network=mynet,model=virtio,mac=00:11...
--network none
--network help
--graphics GRAPHICS Configure guest display settings. Ex:
--graphics spice
--graphics vnc,port=5901,listen=0.0.0.0
--graphics none
--controller CONTROLLER
Configure a guest controller device. Ex:
--controller type=usb,model=qemu-xhci
--controller type=scsi,model=virtio-scsi
--input INPUT Configure a guest input device. Ex:
--input tablet
--input keyboard,bus=usb
--serial SERIAL Configure a guest serial device
--parallel PARALLEL Configure a guest parallel device
--channel CHANNEL Configure a guest communication channel
--console CONSOLE Configure a text console connection between the guest and host
--hostdev HOSTDEV Configure physical USB/PCI/etc host devices to be shared with the guest
--filesystem FILESYSTEM
Pass host directory to the guest. Ex:
--filesystem /my/source/dir,/dir/in/guest
--filesystem template_name,/,type=template
--sound [SOUND] Configure guest sound device emulation
--audio AUDIO Configure host audio backend for sound devices
--watchdog WATCHDOG Configure a guest watchdog device
--video VIDEO Configure guest video hardware.
--smartcard SMARTCARD
Configure a guest smartcard device. Ex:
--smartcard mode=passthrough
--redirdev REDIRDEV Configure a guest redirection device. Ex:
--redirdev usb,type=tcp,server=192.168.1.1:4000
--memballoon MEMBALLOON
Configure a guest memballoon device. Ex:
--memballoon model=virtio
--tpm TPM Configure a guest TPM device. Ex:
--tpm /dev/tpm
--rng RNG Configure a guest RNG device. Ex:
--rng /dev/urandom
--panic PANIC Configure a guest panic device. Ex:
--panic default
--shmem SHMEM Configure a guest shared memory device. Ex:
--shmem name=shmem0
--memdev MEMDEV Configure a guest memory device. Ex:
--memdev dimm,target.size=1024
--vsock VSOCK Configure guest vsock sockets. Ex:
--vsock cid.auto=yes
--vsock cid.address=7
--iommu IOMMU Configure an IOMMU device. Ex:
--iommu model=intel,driver.aw_bits=48
Guest Configuration Options:
--iothreads IOTHREADS
Set domain <iothreads> and <iothreadids> configuration.
--seclabel SECLABEL, --security SECLABEL
Set domain seclabel configuration.
--keywrap KEYWRAP Set guest to perform the S390 cryptographic key management operations.
--cputune CPUTUNE Tune CPU parameters for the domain process.
--numatune NUMATUNE Tune NUMA policy for the domain process.
--memtune MEMTUNE Tune memory policy for the domain process.
--blkiotune BLKIOTUNE
Tune blkio policy for the domain process.
--memorybacking MEMORYBACKING
Set memory backing policy for the domain process. Ex:
--memorybacking hugepages=on
--features FEATURES Set domain <features> XML. Ex:
--features acpi=off
--features apic=on,apic.eoi=on
--clock CLOCK Set domain <clock> XML. Ex:
--clock offset=localtime,rtc_tickpolicy=catchup
--pm PM Configure VM power management features
--events EVENTS Configure VM lifecycle management policy
--resource RESOURCE Configure VM resource partitioning (cgroups)
--sysinfo SYSINFO Configure SMBIOS System Information. Ex:
--sysinfo host
--sysinfo bios.vendor=MyVendor,bios.version=1.2.3,...
--qemu-commandline QEMU_COMMANDLINE
Pass arguments directly to the QEMU emulator. Ex:
--qemu-commandline='-display gtk,gl=on'
--qemu-commandline env=DISPLAY=:0.1
--launchSecurity LAUNCHSECURITY, --launchsecurity LAUNCHSECURITY
Configure VM launch security (e.g. SEV memory encryption). Ex:
--launchSecurity sev
Virtualization Platform Options:
-v, --hvm This guest should be a fully virtualized guest
-p, --paravirt This guest should be a paravirtualized guest
--container This guest should be a container guest
--virt-type VIRT_TYPE
Hypervisor name to use (kvm, qemu, xen, ...)
--arch ARCH The CPU architecture to simulate
--machine MACHINE The machine type to emulate
Miscellaneous Options:
--autostart Have domain autostart on host boot up.
--transient Create a transient domain.
--destroy-on-exit Force power off the domain when the console viewer is closed.
--wait [WAIT] Minutes to wait for install to complete.
--autoconsole AUTOCONSOLE
Configure guest console auto connect. Example:
--autoconsole text
--autoconsole graphical
--autoconsole none
--noautoconsole Don't automatically try to connect to the guest console
--noreboot Don't boot guest after completing install.
--print-xml [XMLONLY]
Print the generated domain XML rather than create the guest.
--dry-run Run through install process, but do not create devices or define the guest.
--check CHECK Enable or disable validation checks. Example:
--check path_in_use=off
--check all=off
-q, --quiet Suppress non-error output
-d, --debug Print debugging information
kvm常用文件位置
默认nat网络配置文件
# 使用virsh net-edit default 命令修改
# /etc/libvirt/qemu/networks/default.xml
<network>
<name>default</name>
<uuid>0b385979-f3a4-4d10-8d24-fdbbed0307a3</uuid>
<forward mode='nat'/>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:dd:bc:c8'/>
<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>
存储池配置文件(存虚拟机的磁盘镜像)
# virsh pool-edit ubuntu22.04 修改
# /etc/libvirt/storage/
# /etc/libvirt/storage/autostart
<pool type='dir'>
<name>ubuntu22.04</name>
<uuid>66414018-799e-404e-b930-503662a1f632</uuid>
<capacity unit='bytes'>0</capacity>
<allocation unit='bytes'>0</allocation>
<available unit='bytes'>0</available>
<source>
</source>
<target>
<path>/vm/tp/ubuntu22.04</path>
</target>
</pool>
虚拟机配置文件
# /etc/libvirt/qemu/
内存气球
kvm常用命令
# 客户机(虚拟机)相关virt*
virt-top :查看客户机负载
virt-install:安装客户机系统
# virsh程序是管理virsh客户机,用来创建、修改、暂停和关闭域。它也可以用来列出当前的域
virsh list [--all] : 查看所有的客户机
virsh autostart
virsh net-edit 网络配置名称:修改网络配置
virsh setvcpus 虚拟机名称 2 --live:修改指定虚拟机的cpu核数
virsh dumpxml 虚拟机名称 | grep memballon -C2:查看内存气球的配置
qemu-agent-command
在虚拟机上安装 QEMU 客户机代理
apt install qemu-guest-agent
systemctl enable qemu-guest-agent
virsh qemu-agent-command --domain ubuntuTP '{"execute":"guest-network-get-interfaces"}' #查ip
新建模版机后,部署脚本
#!/usr/bin/env bash
set -euo pipefail
declare -r highPerformanceSSDPath="/vm/hpssd"
declare -r normalSSDPath="/vm/nomralssd"
declare -A tmplDiskMap=(["b"]="/vm/tml/centos/centosTP.qcow2" ["a"]="/vm/tml/ubuntu22.04/ubuntuTP.qcow2")
declare -A tmplXmlMap=(["b"]="/vm/tml/centos/centosTP.xml" ["a"]="/vm/tml/ubuntu22.04/ubuntutp.xml")
declare -A vmDiskTypePath=(["a"]="/vm/hpssd/" ["b"]="/vm/nomralssd/")
# export LIBGUESTFS_DEBUG=1 LIBGUESTFS_TRACE=1
function identityCheck() {
if [ $(whoami) != root ]; then
echo 请以root身份运行
exit 1
fi
}
function systemMenu() {
cat <<-EOF
--------------------------------
A.部署Ubuntu22.02系统
B.部署CentOS7.9系统
--------------------------------
EOF
}
function systemEnvMenu() {
cat <<-EOF
--------------------------------
A.高性能磁盘主机
B.普通SSD磁盘主机
--------------------------------
EOF
}
function deployVM() {
chooseSystem=""
choseSystemDisk=""
hostName=""
iplast=""
while :; do
systemMenu
read -p 请选择要部署的系统: chooseSystem
chooseSystem=$(echo $chooseSystem | tr 'A-Z' 'a-z')
if [[ "$chooseSystem" =~ [a|A|b|B] ]]; then
break
fi
echo 没有这个选项\"${chooseSystem}\",请重新选择
done
while :; do
systemEnvMenu
read -p 请选择系统磁盘类型: choseSystemDisk
choseSystemDisk=$(echo $choseSystemDisk | tr 'A-Z' 'a-z')
if [[ "$choseSystemDisk" =~ [a|A|b|B] ]]; then
break
fi
echo 没有这个选项\"${choseSystemDisk}\",请重新选择
done
while :; do
read -p 请输入主机名称: hostName
if [ "$hostName" != "" ]; then
break
fi
done
while :; do
read -p 请输入ip地址的最后一位: iplast
if [[ "$iplast" =~ (^[1-9][0-9]$)|(^[5-9]$)|(^1[0-9]{2}$)|(^25[0-4]$)|(^2[0-4][0-9]$) ]]; then
break
fi
echo ip:\"${iplast}\"非法,请重新输入
done
vmdiskDir=${vmDiskTypePath["$choseSystemDisk"]}${hostName}
if [ ! -d "$vmdiskDir" ]; then
mkdir -p $vmdiskDir
fi
vmdisk=${vmdiskDir}/${hostName}.qcow2
vmxml=${vmdiskDir}/${hostName}.xml
if [ -e "$vmdisk" ]; then
echo -e "\033[31m${vmdisk}已存在\033[0m"
exit 1
fi
vmuuid=$(uuidgen)
ip="192.168.158.${iplast}"
vmmac1="52:54:$(dd if=/dev/urandom count=1 2>/dev/null | md5sum | sed -r 's/^(..)(..)(..)(..).*$/\1:\2:\3:\4/')"
vmmac2="52:54:$(dd if=/dev/urandom count=1 2>/dev/null | md5sum | sed -r 's/^(..)(..)(..)(..).*$/\1:\2:\3:\4/')"
echo -e "\033[32m虚拟机名字是${hostName},虚拟机ID是:${vmuuid},虚拟机磁盘是:${vmdisk},虚拟机mac是:${vmmac1},ip是:${ip}】\033[0m"
tmplDiskPath=${tmplDiskMap[$chooseSystem]}
cp ${tmplXmlMap[$chooseSystem]} $vmxml
qemu-img create -f qcow2 -b $tmplDiskPath -F qcow2 $vmdisk
if [ "$chooseSystem" = "b" ]; then
guestmount -a $vmdisk -m /dev/centos_centostp/root /vm/temp
sed -ri "s#IPADDR=192.168.158.2#IPADDR=${ip}#" /vm/temp/etc/sysconfig/network-scripts/ifcfg-eth0
sed -ri 's#UUID=29104de9-6297-4b33-aa51-891aa5073e6b##' /vm/temp/etc/sysconfig/network-scripts/ifcfg-eth0
sed -ri 's#UUID=835c4284-a9fc-4325-bb97-e9fda3bccfea##' /vm/temp/etc/sysconfig/network-scripts/ifcfg-eth1
sed -ri "s#centostp#${hostName}#" /vm/temp/etc/hostname
elif [[ "$chooseSystem" = "a" ]]; then
guestmount -a $vmdisk -m /dev/sda4 /vm/temp
sed -ri "s#192.168.158.3#${ip}#" /vm/temp/etc/netplan/00-installer-config.yaml
sed -ri "s#ubuntutp#${hostName}#" /vm/temp/etc/hostname
fi
guestunmount /vm/temp
sed -ri "s#vmname#${hostName}#" $vmxml
sed -ri "s#vmuuid#${vmuuid}#" $vmxml
sed -ri "s#vmdisk#${vmdisk}#" $vmxml
sed -ri "s#vmmac1#${vmmac1}#" $vmxml
sed -ri "s#vmmac2#${vmmac2}#" $vmxml
virsh define $vmxml
echo -e "\033[32m部署完成\033[0m"
}
identityCheck
deployVM
拓展
系统预备工具
PXE
kickstart
cobbler
部署工具
ansible
puppet
saltstack
chef
持续集成及持续发布工具
git
github
gitlab
gogs
jenkins
docker
kubernetes