SLB负载均衡测试
最后更新于
最后更新于
# 在Web-VM01和Web-VM02上分别执行
Install-WindowsFeature Web-Server -IncludeManagementTools
# 创建测试页面(在每台VM上显示不同内容以区分)
$hostname = $env:COMPUTERNAME
Set-Content -Path "C:\inetpub\wwwroot\index.html" -Value "<h1>Hello from $hostname</h1>"$NcUri = "https://nc.contoso.com"
# 定义负载均衡器配置
$LBProperties = @{
properties = @{
frontendIPConfigurations = @(
@{
resourceId = "FrontEnd1"
properties = @{
subnet = @{
resourceRef = "/networking/v1/logicalnetworks/PublicVIP/subnets/PublicVIPSubnet"
}
privateIPAddress = "192.148.15.10"
privateIPAllocationMethod = "Static"
}
}
)
backendAddressPools = @(
@{
resourceId = "BackEndPool1"
properties = @{
backendIPConfigurations = @()
}
}
)
loadBalancingRules = @(
@{
resourceId = "HTTPRule"
properties = @{
frontendIPConfigurations = @(
@{ resourceRef = "/networking/v1/loadBalancers/WebLB/frontendIPConfigurations/FrontEnd1" }
)
backendAddressPool = @{
resourceRef = "/networking/v1/loadBalancers/WebLB/backendAddressPools/BackEndPool1"
}
protocol = "TCP"
frontendPort = 80
backendPort = 80
enableFloatingIP = $false
idleTimeoutInMinutes = 4
}
}
)
probes = @(
@{
resourceId = "HealthProbe"
properties = @{
protocol = "HTTP"
port = 80
requestPath = "/"
intervalInSeconds = 15
numberOfProbes = 2
}
}
)
}
}
$body = $LBProperties | ConvertTo-Json -Depth 10
Invoke-RestMethod -Uri "$NcUri/networking/v1/loadBalancers/WebLB" `
-Method Put `
-Body $body `
-UseDefaultCredentials `
-ContentType "application/json"# 测试VIP的HTTP访问
Invoke-WebRequest -Uri "http://192.148.15.10" -UseBasicParsing
# 多次访问,观察负载分发效果
1..10 | ForEach-Object {
$response = Invoke-WebRequest -Uri "http://192.148.15.10" -UseBasicParsing
Write-Host "请求 $_ : $($response.Content)"
}# 模拟故障:停止Web-VM01的IIS
Invoke-Command -ComputerName "Web-VM01" { Stop-Service W3SVC }
# 测试VIP是否仍然可用
Invoke-WebRequest -Uri "http://192.148.15.10" -UseBasicParsing
# 恢复Web-VM01
Invoke-Command -ComputerName "Web-VM01" { Start-Service W3SVC }Invoke-Command -VMName "poc-dcgw" -Credential $cred {
Get-BgpRouteInformation | Where-Object { $_.Network -match "192.148.15" }
}