1. Hyper-V概述与环境规划
Hyper-V是Microsoft的虚拟化平台,允许用户在Windows服务器上运行多个虚拟机。Hyper-V是企业级虚拟化的重要工具,适用于各种规模的组织。更多学习教程www.fgedu.net.cn
1.1 Hyper-V版本说明
Hyper-V目前主要版本为Windows Server 2022中的Hyper-V,本教程以Windows Server 2022为例进行详细讲解。Windows Server 2022中的Hyper-V相比之前版本在性能、稳定性和功能方面都有显著提升,支持更多的虚拟化特性。
PS C:\> Get-WindowsFeature | Where-Object Name -eq Hyper-V
# 查看Windows Server版本
PS C:\> Get-WindowsVersion
# 查看系统信息
PS C:\> systeminfo
1.2 环境规划
本次安装环境规划如下:
hyperv01.fgedu.net.cn (192.168.1.141) – Hyper-V主服务器
hyperv02.fgedu.net.cn (192.168.1.142) – Hyper-V备用服务器
Windows Server版本:2022
存储配置:
– 本地存储:512GB SSD
– 共享存储:10TB iSCSI
网络配置:
– 管理网络:192.168.1.0/24
– VM网络:192.168.10.0/24
– 实时迁移网络:192.168.20.0/24
2. 硬件环境要求
Hyper-V作为企业级虚拟化平台,对硬件资源要求较高。学习交流加群风哥微信: itpux-com
2.1 物理主机环境要求
– CPU:至少4核,支持Intel VT-x或AMD-V虚拟化技术
– 内存:至少16GB,建议32GB以上
– 磁盘:至少100GB可用空间,建议使用SSD
– 网络:至少2个千兆以太网接口
# 检查CPU虚拟化支持
PS C:\> Get-WmiObject -Class Win32_Processor | Select-Object Name, VirtualizationFirmwareEnabled
# 检查内存
PS C:\> Get-WmiObject -Class Win32_ComputerSystem | Select-Object TotalPhysicalMemory
# 检查磁盘空间
PS C:\> Get-WmiObject -Class Win32_LogicalDisk | Select-Object DeviceID, Size, FreeSpace
3. Hyper-V安装配置
完成环境准备后,开始安装Hyper-V。
3.1 安装Hyper-V
PS C:\> Install-WindowsFeature -Name Hyper-V -IncludeManagementTools -Restart
# 验证安装
PS C:\> Get-WindowsFeature | Where-Object Name -eq Hyper-V
# 启动Hyper-V管理控制台
PS C:\> virtmgmt.msc
3.2 初始化Hyper-V
PS C:\> New-VMSwitch -Name “External Switch” -NetAdapterName “Ethernet” -AllowManagementOS $true
# 配置Hyper-V存储
PS C:\> New-Item -Path “D:\VMs” -ItemType Directory
# 配置Hyper-V默认存储位置
PS C:\> Set-VMHost -VirtualHardDiskPath “D:\VMs\Virtual Hard Disks” -VirtualMachinePath “D:\VMs\Virtual Machines”
# 验证Hyper-V配置
PS C:\> Get-VMHost
4. Hyper-V配置优化
为了提高Hyper-V的性能和稳定性,需要进行一些配置优化。
4.1 基本配置优化
PS C:\> Set-VMHost -EnableEnhancedSessionMode $true
# 配置Hyper-V内存
PS C:\> Set-VMHost -MemoryStartupBytes 4GB
# 配置Hyper-V处理器
PS C:\> Set-VMHost -VirtualMachineMigrationEnabled $true
# 配置Hyper-V实时迁移
PS C:\> Set-VMHost -UseAnyNetworkForMigration $true
4.2 高可用配置
# 1. 安装故障转移集群功能
PS C:\> Install-WindowsFeature -Name Failover-Clustering -IncludeManagementTools
# 2. 验证集群配置
PS C:\> Test-Cluster -Node hyperv01, hyperv02
# 3. 创建集群
PS C:\> New-Cluster -Name Cluster01 -Node hyperv01, hyperv02 -StaticAddress 192.168.1.143
# 4. 验证集群
PS C:\> Get-Cluster
5. 网络配置
Hyper-V支持多种网络连接模式,包括外部网络、内部网络和专用网络。
5.1 外部网络配置
PS C:\> New-VMSwitch -Name “External Switch” -NetAdapterName “Ethernet” -AllowManagementOS $true
# 验证外部网络
PS C:\> Get-VMSwitch
5.2 内部网络配置
PS C:\> New-VMSwitch -Name “Internal Switch” -SwitchType Internal
# 配置内部网络IP
PS C:\> New-NetIPAddress -InterfaceAlias “vEthernet (Internal Switch)” -IPAddress 192.168.100.1 -PrefixLength 24
# 验证内部网络
PS C:\> Get-VMSwitch
5.3 专用网络配置
PS C:\> New-VMSwitch -Name “Private Switch” -SwitchType Private
# 验证专用网络
PS C:\> Get-VMSwitch
6. 存储配置
Hyper-V支持多种存储选项,包括本地存储和共享存储。
6.1 本地存储配置
PS C:\> New-Item -Path “D:\VMs\Virtual Hard Disks” -ItemType Directory
# 配置Hyper-V默认存储位置
PS C:\> Set-VMHost -VirtualHardDiskPath “D:\VMs\Virtual Hard Disks” -VirtualMachinePath “D:\VMs\Virtual Machines”
# 验证存储配置
PS C:\> Get-VMHost | Select-Object VirtualHardDiskPath, VirtualMachinePath
6.2 共享存储配置
# 1. 安装iSCSI发起程序
PS C:\> Install-WindowsFeature -Name iSCSI-Target-Server
# 2. 连接iSCSI目标
PS C:\> New-IscsiTargetPortal -TargetPortalAddress 192.168.30.100
PS C:\> Connect-IscsiTarget -NodeAddress “iqn.2023-04.com.fgedu:target01”
# 3. 初始化磁盘
PS C:\> Get-Disk | Where-Object PartitionStyle -eq Raw | Initialize-Disk -PartitionStyle GPT
# 4. 创建分区
PS C:\> Get-Disk | Where-Object PartitionStyle -eq GPT | New-Partition -UseMaximumSize -DriveLetter S
# 5. 格式化分区
PS C:\> Format-Volume -DriveLetter S -FileSystem NTFS -NewFileSystemLabel “Shared Storage”
# 6. 创建共享文件夹
PS C:\> New-SmbShare -Name “VMs” -Path “S:\VMs” -FullAccess Everyone
7. 虚拟机配置
Hyper-V允许用户创建和配置虚拟机,以运行不同的操作系统。
7.1 创建虚拟机
PS C:\> New-VM -Name “ubuntu2204” -MemoryStartupBytes 8GB -Generation 2 -NewVHDPath “D:\VMs\Virtual Hard Disks\ubuntu2204.vhdx” -NewVHDSizeBytes 100GB -SwitchName “External Switch”
# 启动虚拟机
PS C:\> Start-VM -Name “ubuntu2204”
# 连接虚拟机
PS C:\> vmconnect.exe localhost ubuntu2204
# 安装操作系统
# 按照提示完成Ubuntu安装
7.2 配置虚拟机
PS C:\> Set-VM -Name “ubuntu2204” -ProcessorCount 4 -DynamicMemory -MemoryMinimumBytes 4GB -MemoryMaximumBytes 8GB
# 配置虚拟机网络
PS C:\> Get-VMNetworkAdapter -VMName “ubuntu2204” | Set-VMNetworkAdapter -SwitchName “External Switch”
# 配置虚拟机存储
PS C:\> Add-VMHardDiskDrive -VMName “ubuntu2204” -Path “D:\VMs\Virtual Hard Disks\ubuntu2204-data.vhdx” -SizeBytes 50GB
# 验证虚拟机配置
PS C:\> Get-VM -Name “ubuntu2204”
8. 性能优化
在生产环境中,需要对Hyper-V进行性能优化以提高虚拟机运行效率。from:www.itpux.com
8.1 内存优化
PS C:\> Set-VMHost -MemoryStartupBytes 4GB
# 配置虚拟机内存
PS C:\> Set-VM -Name “ubuntu2204” -DynamicMemory -MemoryMinimumBytes 4GB -MemoryMaximumBytes 8GB -MemoryStartupBytes 4GB
# 配置内存缓冲区
PS C:\> Set-VM -Name “ubuntu2204” -MemoryBuffer 20
8.2 处理器优化
PS C:\> Set-VMHost -VirtualMachineMigrationEnabled $true
# 配置虚拟机处理器
PS C:\> Set-VM -Name “ubuntu2204” -ProcessorCount 4
# 配置处理器兼容性
PS C:\> Set-VMProcessor -VMName “ubuntu2204” -CompatibilityForMigrationEnabled $true
8.3 存储优化
PS C:\> Set-VMHost -VirtualHardDiskPath “D:\VMs\Virtual Hard Disks” -VirtualMachinePath “D:\VMs\Virtual Machines”
# 配置虚拟机存储
PS C:\> Set-VMHardDiskDrive -VMName “ubuntu2204” -Path “D:\VMs\Virtual Hard Disks\ubuntu2204.vhdx” -ControllerType SCSI -ControllerNumber 0 -ControllerLocation 0
# 配置存储QoS
PS C:\> New-VMStorageQosPolicy -Name “High Performance” -MinimumIops 1000 -MaximumIops 5000
PS C:\> Set-VMHardDiskDrive -VMName “ubuntu2204” -StorageQosPolicy “High Performance”
9. 升级迁移
本节介绍Hyper-V的版本升级和虚拟机迁移方法。
9.1 Hyper-V版本升级
PS C:\> Export-VM -Name “ubuntu2204” -Path “D:\Backups”
# 升级Windows Server
# 1. 下载Windows Server 2022安装介质
# 2. 运行安装程序
# 3. 选择 “升级” 选项
# 4. 按照提示完成升级
# 验证升级
PS C:\> Get-WindowsFeature | Where-Object Name -eq Hyper-V
9.2 虚拟机迁移
PS C:\> Move-VM -Name “ubuntu2204” -DestinationHost “hyperv02”
# 执行存储迁移
PS C:\> Move-VMStorage -VMName “ubuntu2204” -DestinationStoragePath “S:\VMs”
# 验证迁移
PS C:\> Get-VM -Name “ubuntu2204”
10. 备份恢复
本节介绍Hyper-V的备份和恢复方法。
10.1 虚拟机备份
PS C:\> New-Item -Path “C:\Scripts” -ItemType Directory
PS C:\> Set-Content -Path “C:\Scripts\Backup-VM.ps1” -Value @’
$BackupDir = “D:\Backups”
$Date = Get-Date -Format “yyyyMMdd”
# 创建备份目录
New-Item -Path “$BackupDir\$Date” -ItemType Directory -Force
# 导出虚拟机
Get-VM | ForEach-Object {
Export-VM -Name $_.Name -Path “$BackupDir\$Date”
}
# 清理旧备份(保留7天)
Get-ChildItem -Path $BackupDir -Directory | Where-Object {
$_.CreationTime -lt (Get-Date).AddDays(-7)
} | Remove-Item -Recurse -Force
‘@
# 执行备份
PS C:\> C:\Scripts\Backup-VM.ps1
# 添加定时任务
PS C:\> $Trigger = New-ScheduledTaskTrigger -Daily -At 00:00
$Action = New-ScheduledTaskAction -Execute “powershell.exe” -Argument “-File C:\Scripts\Backup-VM.ps1”
Register-ScheduledTask -TaskName “Backup VMs” -Trigger $Trigger -Action $Action -RunLevel Highest
10.2 虚拟机恢复
PS C:\> Stop-VM -Name “ubuntu2204” -Force
# 清理现有虚拟机
PS C:\> Remove-VM -Name “ubuntu2204” -Force
# 恢复虚拟机
PS C:\> Import-VM -Path “D:\Backups\20230405\ubuntu2204”
# 启动虚拟机
PS C:\> Start-VM -Name “ubuntu2204”
# 验证恢复
PS C:\> Get-VM -Name “ubuntu2204”
10.3 虚拟机监控
# 1. 启用Hyper-V性能计数器
PS C:\> Get-Counter -ListSet “Hyper-V*” | ForEach-Object {
Enable-Counter -Counter $_.CounterSetName
}
# 2. 配置事件日志
PS C:\> Get-WinEvent -LogName “Microsoft-Windows-Hyper-V*” | Select-Object TimeCreated, Id, Message
# 3. 配置告警
PS C:\> New-EventLog -LogName “Hyper-V Monitoring” -Source “Hyper-V Monitor”
PS C:\> Write-EventLog -LogName “Hyper-V Monitoring” -Source “Hyper-V Monitor” -EventID 1000 -EntryType Information -Message “Hyper-V Monitoring Started”
通过以上步骤,Hyper-V安装配置、性能优化、升级迁移、备份恢复等内容已全部完成。Hyper-V作为Microsoft的虚拟化平台,能够高效地运行多个虚拟机,是企业级虚拟化的重要工具。
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
