PowerShell Desired State Configuration (DSC) kết hợp với Hyper-V tạo thành một nền tảng mạnh mẽ để tự động hóa, kiểm soát và duy trì trạng thái cấu hình hạ tầng ảo hóa Windows. Dưới đây là hướng dẫn thực hành với các kỹ thuật hiện đại, tập trung vào tính khả thi trong môi trường sản xuất.
1. Kiểm tra và điều chỉnh Local Configuration Manager (LCM)
LCM là thành phần cốt lõi chịu trách nhiệm thực thi và giám sát cấu hình DSC. Để xem trạng thái hiện tại:
Get-DscLocalConfigurationManager | Select-Object ConfigurationMode, RefreshMode, RefreshFrequencyMins, ConfigurationModeFrequencyMins
Kết quả trả về cho biết chế độ áp dụng (ApplyOnly/ApplyAndMonitor/ApplyAndAutoCorrect), tần suất kiểm tra lại và cơ chế cập nhật — những yếu tố then chốt ảnh hưởng đến độ tin cậy của hệ thống.
2. Xây dựng kịch bản DSC cho dịch vụ web và ảo hóa
Thay vì chỉ cài đặt IIS, kịch bản sau tích hợp cả cấu hình bảo mật và kiểm soát phiên bản:
Configuration WebServerWithSecurity {
Import-DscResource -ModuleName PSDesiredStateConfiguration, xWebAdministration
Node 'localhost' {
WindowsFeature IISInstall {
Ensure = 'Present'
Name = 'Web-Server'
}
xWebsite SecureSite {
Name = 'DefaultWebSite'
PhysicalPath = 'C:\inetpub\wwwroot'
State = 'Started'
BindingInfo = @(xWebBindingInformation @{
Protocol = 'HTTPS'
Port = 443
CertificateThumbprint = 'A1B2C3...FEDC'
CertificateStore = 'My'
})
DependsOn = '[WindowsFeature]IISInstall'
}
Registry DisableTLS10 {
Key = 'HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0\Server'
ValueName = 'Enabled'
ValueData = '0'
ValueType = 'DWord'
Ensure = 'Present'
}
}
}
WebServerWithSecurity
Kịch bản này không chỉ triển khai IIS mà còn cấu hình HTTPS, vô hiệu hóa giao thức TLS 1.0 và đảm bảo thứ tự phụ thuộc rõ ràng — mô hình phù hợp với tiêu chuẩn bảo mật hiện đại.
3. Áp dụng cấu hình DSC theo hai mô hình
- Mô hình đẩy (Push): Sử dụng lệnh sau để triển khai cục bộ hoặc từ xa:
# Biên dịch và áp dụng ngay lập tức
. .\WebServerWithSecurity.ps1
Start-DscConfiguration -Path .\WebServerWithSecurity -Wait -Verbose -Force
- Mô hình kéo (Pull): Yêu cầu thiết lập máy chủ Pull Server. Thay vì dùng script thủ công, nên triển khai qua DSC Resource
xDscWebService:
Configuration PullServerSetup {
Import-DscResource -ModuleName xPSDesiredStateConfiguration
Node 'PullServer' {
WindowsFeature DSCServiceFeature {
Ensure = 'Present'
Name = 'DSC-Service'
}
xDscWebService PSDSCPullServer {
Ensure = 'Present'
EndpointName = 'PSDSCPullServer'
Port = 8080
PhysicalPath = "$env:SystemDrive\inetpub\wwwroot\PSDSCPullServer"
CertificateThumbPrint = 'AllowUnencryptedTraffic'
ModulePath = "$env:PROGRAMFILES\WindowsPowerShell\DscService\Modules"
ConfigurationPath = "$env:PROGRAMFILES\WindowsPowerShell\DscService\Configuration"
State = 'Started'
DependsOn = '[WindowsFeature]DSCServiceFeature'
}
}
}
4. Quản trị Hyper-V bằng PowerShell – Không cần GUI
Thay vì sử dụng Hyper-V Manager, toàn bộ cấu hình có thể được thực hiện qua PowerShell với độ chính xác cao hơn:
# Kích hoạt Live Migration và cấu hình tối ưu
Set-VMHost -VirtualMachineMigrationEnabled $true `
-VirtualMachineMigrationAuthenticationType Kerberos `
-MaximumVirtualMachineMigrations 4 `
-UseAnyNetworkForMigration $false
# Thiết lập Enhanced Session Mode toàn cục
Set-VMHost -EnableEnhancedSessionMode $true
# Cấu hình NUMA Spanning (chỉ bật khi cần thiết)
Set-VMHost -NumaSpanningEnabled $false
# Tạo switch ảo với QoS và SR-IOV hỗ trợ
New-VMSwitch -Name 'vSwitch-Production' `
-NetAdapterName 'Ethernet' `
-EnableEmbeddedTeaming $true `
-AllowManagementOS $false `
-MinimumBandwidthMode Weight `
-EnableIov $true
5. Tự động hóa cấu hình Hyper-V bằng DSC
Dưới đây là tài nguyên tùy chỉnh (custom DSC resource) để đảm bảo cấu hình Hyper-V luôn ở trạng thái mong muốn — không phụ thuộc vào thao tác thủ công:
Configuration HyperVConsistency {
Import-DscResource -ModuleName PSDesiredStateConfiguration
Node 'localhost' {
Script ConfigureVMHostSettings {
GetScript = { @{ Result = (Get-VMHost | ConvertTo-Json) } }
TestScript = {
$hostConfig = Get-VMHost
return ($hostConfig.EnableEnhancedSessionMode -eq $true) -and
($hostConfig.MaximumVirtualMachineMigrations -eq 4) -and
($hostConfig.NumaSpanningEnabled -eq $false)
}
SetScript = {
Set-VMHost -EnableEnhancedSessionMode $true `
-MaximumVirtualMachineMigrations 4 `
-NumaSpanningEnabled $false
}
}
Registry EnableVMIntegrationServices {
Key = 'HKLM:\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services'
ValueName = 'fEnableRemoteFX'
ValueData = '1'
ValueType = 'DWord'
Ensure = 'Present'
}
}
}
HyperVConsistency
Tài nguyên Script ở trên tuân thủ đầy đủ mẫu DSC (Get/Test/Set), cho phép giám sát liên tục và sửa chữa tự động nếu cấu hình bị thay đổi ngoài ý muốn.
6. Triển khai đa máy chủ với cấu hình động
Để mở rộng sang nhiều nút, sử dụng mảng tên máy chủ và biến môi trường:
$TargetNodes = @('HV-PROD-01', 'HV-PROD-02', 'HV-PROD-03')
Configuration MultiNodeHyperV {
Param([string[]]$NodeList)
Import-DscResource -ModuleName PSDesiredStateConfiguration
Node $NodeList {
Script ApplyStandardHostPolicy {
SetScript = {
Set-VMHost -EnableEnhancedSessionMode $true -MaximumVirtualMachineMigrations 3
New-VMSwitch -Name 'InternalSwitch' -SwitchType Internal -ErrorAction SilentlyContinue
}
TestScript = { (Get-VMHost).EnableEnhancedSessionMode -eq $true }
GetScript = { @{ Result = 'Applied' } }
}
}
}
MultiNodeHyperV -NodeList $TargetNodes
Kịch bản này có thể được biên dịch thành nhiều MOF riêng biệt cho từng nút, hoặc sử dụng cấu hình meta MOF để định tuyến tự động tới đúng đích.