-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathCreate_a_Load_Balancer.ps1
210 lines (166 loc) · 7.35 KB
/
Create_a_Load_Balancer.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
Set-Location c:\
Clear-Host
Install-Module -Name Az -Force -AllowClobber -Verbose
#Log into Azure
Connect-AzAccount
#Select the correct subscription
Get-AzSubscription -SubscriptionName "MSDN Platforms" | Select-AzSubscription
#create a resource group
$rgName='MyResourceGroupSLB'
$location='westeurope'
New-AzResourceGroup -Name $rgName -Location $location
#public IP address for the load balancer => a zonal Public IP address in zone 1
$publicIp = New-AzPublicIpAddress `
-ResourceGroupName $rgName `
-Name 'myPublicIP' `
-Location $location `
-AllocationMethod static `
-SKU Standard `
-zone 1
#Create a front-end IP
$feip = New-AzLoadBalancerFrontendIpConfig -Name 'myFrontEndPool' -PublicIpAddress $publicIp
#Create a back-end address pool
$bepool = New-AzLoadBalancerBackendAddressPoolConfig -Name 'myBackEndPool'
#Create a health probe
$probe = New-AzLoadBalancerProbeConfig `
-Name 'myHealthProbe' `
-Protocol Http -Port 80 `
-RequestPath / -IntervalInSeconds 360 -ProbeCount 5
#Create a load balancer rule
$rule = New-AzLoadBalancerRuleConfig `
-Name 'myLoadBalancerRuleWeb' -Protocol Tcp `
-Probe $probe -FrontendPort 80 -BackendPort 80 `
-FrontendIpConfiguration $feip `
-BackendAddressPool $bePool
#Create the NAT rules
$natrule1 = New-AzLoadBalancerInboundNatRuleConfig `
-Name 'myLoadBalancerRDP1' `
-FrontendIpConfiguration $feip `
-Protocol tcp -FrontendPort 4221 `
-BackendPort 3389
$natrule2 = New-AzLoadBalancerInboundNatRuleConfig `
-Name 'myLoadBalancerRDP2' `
-FrontendIpConfiguration $feip `
-Protocol tcp `
-FrontendPort 4222 `
-BackendPort 3389
$natrule3 = New-AzLoadBalancerInboundNatRuleConfig `
-Name 'myLoadBalancerRDP3' `
-FrontendIpConfiguration $feip `
-Protocol tcp `
-FrontendPort 4223 `
-BackendPort 3389
#Create load balancer
$lb = New-AzLoadBalancer `
-ResourceGroupName $rgName `
-Name 'MyLoadBalancer' `
-SKU Standard `
-Location $location `
-FrontendIpConfiguration $feip `
-BackendAddressPool $bepool `
-Probe $probe `
-LoadBalancingRule $rule `
-InboundNatRule $natrule1,$natrule2,$natrule3
#Create a virtual network
# Create subnet config
$subnetConfig = New-AzVirtualNetworkSubnetConfig `
-Name "mySubnet" `
-AddressPrefix 10.0.2.0/24
# Create the virtual network
$vnet = New-AzVirtualNetwork `
-ResourceGroupName "myResourceGroupSLB" `
-Location $location `
-Name "myVnet" `
-AddressPrefix 10.0.0.0/16 `
-Subnet $subnetConfig
#Create public IP addresses for the VMs
$RdpPublicIP_1 = New-AzPublicIpAddress `
-Name "RdpPublicIP_1" `
-ResourceGroupName $RgName `
-Location $location `
-SKU Standard `
-AllocationMethod static
$RdpPublicIP_2 = New-AzPublicIpAddress `
-Name "RdpPublicIP_2" `
-ResourceGroupName $RgName `
-Location $location `
-SKU Standard `
-AllocationMethod static
$RdpPublicIP_3 = New-AzPublicIpAddress `
-Name "RdpPublicIP_3" `
-ResourceGroupName $RgName `
-Location $location `
-SKU Standard `
-AllocationMethod static
#Create a network security group rule for port 3389
$rule1 = New-AzNetworkSecurityRuleConfig -Name 'myNetworkSecurityGroupRuleRDP' -Description 'Allow RDP' `
-Access Allow -Protocol Tcp -Direction Inbound -Priority 1000 `
-SourceAddressPrefix Internet -SourcePortRange * `
-DestinationAddressPrefix * -DestinationPortRange 3389
#Create a network security group rule for port 80
$rule2 = New-AzNetworkSecurityRuleConfig -Name 'myNetworkSecurityGroupRuleHTTP' -Description 'Allow HTTP' `
-Access Allow -Protocol Tcp -Direction Inbound -Priority 2000 `
-SourceAddressPrefix Internet -SourcePortRange * `
-DestinationAddressPrefix * -DestinationPortRange 80
#Create a network security group
$nsg = New-AzNetworkSecurityGroup -ResourceGroupName $RgName -Location $location `
-Name 'myNetworkSecurityGroup' -SecurityRules $rule1,$rule2
#Create NICs
# Create NIC for VM1
$nicVM1 = New-AzNetworkInterface -ResourceGroupName $rgName -Location $location `
-Name 'MyNic1' -PublicIpAddress $RdpPublicIP_1 -LoadBalancerBackendAddressPool $bepool -NetworkSecurityGroup $nsg `
-LoadBalancerInboundNatRule $natrule1 -Subnet $vnet.Subnets[0]
$nicVM2 = New-AzNetworkInterface -ResourceGroupName $rgName -Location $location `
-Name 'MyNic2' -PublicIpAddress $RdpPublicIP_2 -LoadBalancerBackendAddressPool $bepool -NetworkSecurityGroup $nsg `
-LoadBalancerInboundNatRule $natrule2 -Subnet $vnet.Subnets[0]
$nicVM3 = New-AzNetworkInterface -ResourceGroupName $rgName -Location $location `
-Name 'MyNic3' -PublicIpAddress $RdpPublicIP_3 -LoadBalancerBackendAddressPool $bepool -NetworkSecurityGroup $nsg `
-LoadBalancerInboundNatRule $natrule3 -Subnet $vnet.Subnets[0]
#Create virtual machines
#Set an administrator username and password for the VMs
$cred = Get-Credential
# ############## VM1 ###############
# Create a virtual machine configuration
$vmConfig = New-AzVMConfig -VMName 'myVM1' -VMSize Standard_DS1_v2 `
| Set-AzVMOperatingSystem -Windows -ComputerName 'myVM1' -Credential $cred `
| Set-AzVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2019-Datacenter -Version latest `
| Add-AzVMNetworkInterface -Id $nicVM1.Id
# Create a virtual machine
$vm1 = New-AzVM -ResourceGroupName $rgName -Zone 1 -Location $location -VM $vmConfig
# ############## VM2 ###############
# Create a virtual machine configuration
$vmConfig = New-AzVMConfig -VMName 'myVM2' -VMSize Standard_DS1_v2 `
| Set-AzVMOperatingSystem -Windows -ComputerName 'myVM2' -Credential $cred `
| Set-AzVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2019-Datacenter -Version latest `
| Add-AzVMNetworkInterface -Id $nicVM2.Id
# Create a virtual machine
$vm2 = New-AzVM -ResourceGroupName $rgName -Zone 2 -Location $location -VM $vmConfig
# ############## VM3 ###############
# Create a virtual machine configuration
$vmConfig = New-AzVMConfig -VMName 'myVM3' -VMSize Standard_DS1_v2 `
| Set-AzVMOperatingSystem -Windows -ComputerName 'myVM3' -Credential $cred `
| Set-AzVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2019-Datacenter -Version latest `
| Add-AzVMNetworkInterface -Id $nicVM3.Id
# Create a virtual machine
$vm3 = New-AzVM -ResourceGroupName $rgName -Zone 3 -Location $location -VM $vmConfig
#Install IIS with a custom web page
$vm1_rdp_ip = (Get-AzPublicIPAddress -ResourceGroupName $rgName -Name "RdpPublicIP_1").IpAddress
$vm2_rdp_ip = (Get-AzPublicIPAddress -ResourceGroupName $rgName -Name "RdpPublicIP_2").IpAddress
$vm3_rdp_ip = (Get-AzPublicIPAddress -ResourceGroupName $rgName -Name "RdpPublicIP_3").IpAddress
mstsc /v:$vm1_rdp_ip
mstsc /v:$vm2_rdp_ip
mstsc /v:$vm3_rdp_ip
# Install IIS
Install-WindowsFeature -name Web-Server -IncludeManagementTools
# Remove default htm file
remove-item C:\inetpub\wwwroot\iisstart.htm
#Add custom htm file
Add-Content -Path "C:\inetpub\wwwroot\iisstart.htm" -Value $("Hello World from host " + $env:computername)
#Close the RDP connections with myVM1, myVM2, and myVM3
#Test load balancer
Get-AzPublicIPAddress `
-ResourceGroupName "myResourceGroupSLB" `
-Name "myPublicIP" | Select-Object IpAddress
#enter the public IP address in to a web browser
#Clean Up
Remove-AzResourceGroup -Name myResourceGroupSLB -Force