Hyper-V và Cloud Integration

75 phút

Hyper-V và Cloud Integration

Hyper-V và Cloud Integration

Hyper-V

Cài đặt

# Cài Hyper-V role
Install-WindowsFeature -Name Hyper-V `
    -IncludeManagementTools -Restart

# Verify
Get-WindowsFeature Hyper-V
Get-VMHost

Cấu hình Host

# Tạo virtual switches
New-VMSwitch -Name "External-Switch" `
    -NetAdapterName "Ethernet" `
    -AllowManagementOS $true

New-VMSwitch -Name "Internal-Switch" `
    -SwitchType Internal

New-VMSwitch -Name "Private-Switch" `
    -SwitchType Private

# Set default paths
Set-VMHost -VirtualMachinePath "D:\Hyper-V\VMs" `
    -VirtualHardDiskPath "D:\Hyper-V\VHDs"

# Enable nested virtualization (nếu cần)
Set-VMProcessor -VMName "VM01" -ExposeVirtualizationExtensions $true

Tạo VM

# Tạo VM
New-VM -Name "APP01" `
    -MemoryStartupBytes 4GB `
    -Generation 2 `
    -NewVHDPath "D:\Hyper-V\VHDs\APP01.vhdx" `
    -NewVHDSizeBytes 80GB `
    -SwitchName "External-Switch"

# Cấu hình
Set-VMProcessor -VMName "APP01" `
    -Count 2 `
    -Reserve 10 `
    -Maximum 80

Set-VMMemory -VMName "APP01" `
    -DynamicMemoryEnabled $true `
    -MinimumBytes 2GB `
    -MaximumBytes 8GB `
    -StartupBytes 4GB

# Thêm network adapter
Add-VMNetworkAdapter -VMName "APP01" `
    -Name "LAN" -SwitchName "External-Switch"

# Mount ISO và start
Set-VMDvdDrive -VMName "APP01" `
    -Path "C:\ISOs\WindowsServer2022.iso"

Start-VM -Name "APP01"

Snapshots / Checkpoints

# Tạo checkpoint
Checkpoint-VM -Name "APP01" -SnapshotName "BeforeUpdate"

# Liệt kê
Get-VMSnapshot -VMName "APP01"

# Restore
Restore-VMSnapshot -VMName "APP01" `
    -Name "BeforeUpdate" -Confirm:$false

# Xóa
Remove-VMSnapshot -VMName "APP01" -Name "BeforeUpdate"

Export/Import VM

# Export
Export-VM -Name "APP01" -Path "D:\Exports"

# Import
Import-VM -Path "D:\Exports\APP01" `
    -Copy -GenerateNewId

# Move VM storage
Move-VMStorage -VMName "APP01" `
    -DestinationStoragePath "D:\NewStorage"

Live Migration

# Enable Live Migration
Enable-VMMigration
Set-VMHost -VirtualMachineMigrationAuthenticationType Kerberos

# Configure networks
Set-VMHost -VirtualMachineMigrationPerformanceOption SMB

# Live migrate
Move-VM -Name "APP01" `
    -DestinationHost "HV02.abc.local" `
    -IncludeStorage `
    -DestinationStoragePath "D:\Hyper-V\VMs"

Hyper-V Replica

# Enable Replica on primary
Set-VMReplicationServer -ReplicationEnabled $true `
    -AllowedAuthenticationType Kerberos `
    -ReplicationKerberosAuthenticationPort 8080 `
    -DefaultStorageLocation "D:\Replica"

# Enable Replica on secondary
Set-VMReplicationServer -ReplicationEnabled $true `
    -AllowedAuthenticationType Kerberos `
    -ReplicationKerberosAuthenticationPort 8080 `
    -DefaultStorageLocation "D:\Replica"

# Configure VM replication
Set-VMReplication -VMName "APP01" `
    -ReplicaServerName "HV02.abc.local" `
    -ReplicaServerPort 8080 `
    -AuthenticationType Kerberos `
    -CompressionEnabled $true `
    -ReplicationFrequencySec 300

Start-VMInitialReplication -VMName "APP01"

# Test failover
Start-VMFailover -VMName "APP01" -AsTest
Complete-VMFailover -VMName "APP01"

Storage trong Hyper-V

Storage Spaces Direct (S2D)

# Chỉ trên Datacenter edition
Enable-ClusterS2D
Get-ClusterS2D
Get-StoragePool -IsPrimordial $false
Get-Volume

# Tạo volume
New-Volume -StoragePoolFriendlyName "S2D*" `
    -FriendlyName "CSV01" `
    -FileSystem CSVFS_ReFS `
    -Size 1TB

Shared VHDX

# Tạo shared VHDX cho guest clustering
New-VHD -Path "D:\Hyper-V\VHDs\Shared.vhdx" `
    -SizeBytes 100GB -Dynamic

Add-VMHardDiskDrive -VMName "SQL01" `
    -Path "D:\Hyper-V\VHDs\Shared.vhdx" `
    -ShareVirtualDisk

Set-VM -Name "SQL01" -AutomaticStopAction TurnOff

Cloud Integration

Azure AD Connect

Sync on-prem AD với Azure AD

Download: https://www.microsoft.com/en-us/download/details.aspx?id=47594

Features:
- Password Hash Sync
- Pass-through Authentication
- Federation with AD FS
- Seamless SSO
# Cài Azure AD Connect
# Chạy installer với GUI

# Verify sync
Get-ADSyncScheduler
Start-ADSyncSyncCycle -PolicyType Delta
Start-ADSyncSyncCycle -PolicyType Initial

Hybrid Azure AD Join

Requirements:
- Azure AD Connect installed
- AD FS or PHS configured
- Service Connection Point (SCP) in AD
- Devices joined to on-prem AD

Config:
- Azure AD Connect → Device Options
- Configure Hybrid Azure AD Join

Azure Backup

# Download MARS agent
# Register with Recovery Services Vault

# Backup:
- Files and folders
- System State
- Volume

# Restore:
- Individual files
- Full server
- Alternate location

Azure Site Recovery

# Replicate on-prem VMs to Azure

# Enable replication for Hyper-V VMs:
# 1. Create Recovery Services Vault
# 2. Configure Hyper-V site
# 3. Install Provider and Agent
# 4. Enable replication per VM

# Failover to Azure:
Start-AzRecoveryServicesAsrUnplannedFailoverJob `
    -ReplicationProtectedItem $rpi `
    -Direction PrimaryToRecovery

# Failback:
Start-AzRecoveryServicesAsrUnplannedFailoverJob `
    -ReplicationProtectedItem $rpi `
    -Direction RecoveryToPrimary

Windows Admin Center

Tính năng cho Hyper-V

- Quản lý VMs từ browser
- Tạo VM, snapshot
- Live migration
- Storage management
- Cluster management
- Azure integration
- Backup và restore

Azure Integration với WAC

- Azure Backup
- Azure Site Recovery
- Azure Update Management
- Azure Monitor
- Azure Security Center

Monitoring

Performance Counters cho Hyper-V

Hyper-V Hypervisor Logical Processor(_Total)\% Total Run Time
Hyper-V Hypervisor Virtual Processor
Hyper-V Dynamic Memory VM
Hyper-V Virtual Storage Device\Read/Write Bytes/sec
Hyper-V Virtual Network Adapter\Bytes/sec

Best Practices

CPU:
- Reserve 10% cho host
- Không oversubscribe quá 8:1

Memory:
- Dynamic memory
- Không overcommit quá 1.5x

Storage:
- Fixed VHDX cho production
- Dynamic VHDX cho test
- ReFS cho S2D
- Tiered storage

Network:
- SR-IOV nếu hỗ trợ
- VMQ (Virtual Machine Queue)
- NIC Teaming
- 10GbE cho production

Backup và DR

Hyper-V Backup

# Windows Server Backup cho Hyper-V
$policy = New-WBPolicy

# Add VMs
$vms = Get-VM | Where-Object State -eq "Running"
Add-WBVirtualMachine -Policy $policy -VirtualMachine $vms

# Add backup target
$target = New-WBFileSpec -FileSpec "E:\"
Add-WBBackupTarget -Policy $policy -Target $target

# Schedule
$schedule = New-WBBackupSchedule `
    -TimesOfDay 22:00 `
    -DaysOfWeek Monday,Tuesday,Wednesday,Thursday,Friday
Set-WBSchedule -Policy $policy -Schedule $schedule

Set-WBPolicy -Policy $policy

# Run
Start-WBBackup -Policy $policy

3-2-1 Backup Rule

3 copies of data
2 different media
1 offsite copy

Bài tập thực hành

Hãy triển khai Hyper-V với HA!

Bài tập 1