forked from jetvova/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVmScaleSets.cs
279 lines (260 loc) · 12.4 KB
/
VmScaleSets.cs
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2016-2020, Pulumi Corporation. All rights reserved.
using System;
using System.IO;
using Pulumi;
using Pulumi.Azure.Compute;
using Pulumi.Azure.Compute.Inputs;
using Pulumi.Azure.Core;
using Pulumi.Azure.Lb;
using Pulumi.Azure.Lb.Inputs;
using Pulumi.Azure.Monitoring;
using Pulumi.Azure.Monitoring.Inputs;
using Pulumi.Azure.Network;
public static class VmScaleSets
{
public static Output<string> Run()
{
// Read a list of target locations from the config file:
// Expecting a comma-separated list, e.g., "westus,eastus,westeurope"
var locations = new Config().Require("locations").Split(",");
var resourceGroup = new ResourceGroup("cosmosvms-rg", new ResourceGroupArgs {Location = locations[0]});
var vmss = new CosmosApp("vmss", new CosmosAppArgs
{
ResourceGroup = resourceGroup,
Locations = locations,
DatabaseName = "pricedb",
ContainerName = "prices",
Factory = new Builder(resourceGroup).BuildVMScaleSetApp,
});
return Output.Format($"{vmss.Endpoint}/cosmos");
}
private class Builder
{
private ResourceGroup resourceGroup;
public Builder(ResourceGroup resourceGroup)
{
this.resourceGroup = resourceGroup;
}
public Func<RegionalContext, IRegionalEndpoint> BuildVMScaleSetApp(GlobalContext context)
{
var options =
CustomResourceOptions.Merge(context.Options, new CustomResourceOptions {DeleteBeforeReplace = true});
var file = File.ReadAllText("./vm/vmCustomData.yaml");
return (RegionalContext region) =>
{
var location = region.Location;
var domainName = $"rnddnplm{location}"; //TODO: random
var publicIp = new PublicIp($"pip-{location}", new PublicIpArgs
{
ResourceGroupName = resourceGroup.Name,
Location = location,
AllocationMethod = "Static",
DomainNameLabel = domainName,
},
options);
var loadBalancer = new LoadBalancer($"lb-{location}", new LoadBalancerArgs
{
ResourceGroupName = resourceGroup.Name,
Location = location,
FrontendIpConfigurations =
{
new LoadBalancerFrontendIpConfigurationArgs
{
Name = "PublicIPAddress",
PublicIpAddressId = publicIp.Id,
}
}
},
options);
var bpepool = new BackendAddressPool($"bap-{location}", new BackendAddressPoolArgs
{
ResourceGroupName = resourceGroup.Name,
LoadbalancerId = loadBalancer.Id,
},
options);
var probe = new Probe($"ssh-probe-{location}".Truncate(16), new ProbeArgs
{
ResourceGroupName = resourceGroup.Name,
LoadbalancerId = loadBalancer.Id,
Port = 80,
},
options);
var rule = new Rule($"rule-{location}", new RuleArgs
{
ResourceGroupName = resourceGroup.Name,
BackendAddressPoolId = bpepool.Id,
BackendPort = 80,
FrontendIpConfigurationName = "PublicIPAddress",
FrontendPort = 80,
LoadbalancerId = loadBalancer.Id,
ProbeId = probe.Id,
Protocol = "Tcp",
},
options);
var vnet = new VirtualNetwork($"vnet-{location}", new VirtualNetworkArgs
{
ResourceGroupName = resourceGroup.Name,
Location = location,
AddressSpaces = {"10.0.0.0/16"},
},
options);
var subnet = new Subnet($"subnet-{location}", new SubnetArgs
{
ResourceGroupName = resourceGroup.Name,
AddressPrefix = "10.0.2.0/24",
VirtualNetworkName = vnet.Name,
},
options);
var customData = Output.All<string>(context.CosmosAccount.Endpoint,
context.CosmosAccount.PrimaryMasterKey, context.Database.Name, context.Container.Name)
.Apply(values =>
{
return file.Replace("${ENDPOINT}", values[0])
.Replace("${MASTER_KEY}", values[1])
.Replace("${DATABASE}", values[2])
.Replace("${COLLECTION}", values[3])
.Replace("${LOCATION}", location);
});
var scaleSet = new ScaleSet($"vmss-{location}", new ScaleSetArgs
{
ResourceGroupName = resourceGroup.Name,
Location = location,
NetworkProfiles =
{
new ScaleSetNetworkProfileArgs
{
IpConfigurations =
{
new ScaleSetNetworkProfileIpConfigurationArgs
{
LoadBalancerBackendAddressPoolIds = {bpepool.Id},
Name = "IPConfiguration",
Primary = true,
SubnetId = subnet.Id,
}
},
Name = "networkprofile",
Primary = true,
}
},
OsProfile = new ScaleSetOsProfileArgs
{
AdminUsername = "neo",
AdminPassword = "SEcurePwd$3",
ComputerNamePrefix = "lab",
CustomData = customData,
},
OsProfileLinuxConfig = new ScaleSetOsProfileLinuxConfigArgs
{DisablePasswordAuthentication = false},
Sku = new ScaleSetSkuArgs
{
Capacity = 1,
Name = "Standard_DS1_v2",
Tier = "Standard",
},
StorageProfileDataDisks =
{
new ScaleSetStorageProfileDataDiskArgs
{
Caching = "ReadWrite",
CreateOption = "Empty",
DiskSizeGb = 10,
Lun = 0,
}
},
StorageProfileImageReference = new ScaleSetStorageProfileImageReferenceArgs
{
Offer = "UbuntuServer",
Publisher = "Canonical",
Sku = "18.04-LTS",
Version = "latest",
},
StorageProfileOsDisk = new ScaleSetStorageProfileOsDiskArgs
{
Caching = "ReadWrite",
CreateOption = "FromImage",
ManagedDiskType = "Standard_LRS",
Name = "",
},
UpgradePolicyMode = "Automatic",
},
CustomResourceOptions.Merge(options, new CustomResourceOptions {DependsOn = {bpepool, rule}}));
var autoscale = new AutoscaleSetting($"as-{location}", new AutoscaleSettingArgs
{
ResourceGroupName = resourceGroup.Name,
Location = location,
Notification = new AutoscaleSettingNotificationArgs
{
Email = new AutoscaleSettingNotificationEmailArgs
{
CustomEmails = {"[email protected]"},
SendToSubscriptionAdministrator = true,
SendToSubscriptionCoAdministrator = true,
},
},
Profiles =
{
new AutoscaleSettingProfileArgs
{
Capacity = new AutoscaleSettingProfileCapacityArgs
{
Default = 1,
Maximum = 10,
Minimum = 1,
},
Name = "defaultProfile",
Rules =
{
new AutoscaleSettingProfileRuleArgs
{
MetricTrigger = new AutoscaleSettingProfileRuleMetricTriggerArgs
{
MetricName = "Percentage CPU",
MetricResourceId = scaleSet.Id,
Operator = "GreaterThan",
Statistic = "Average",
Threshold = 75,
TimeAggregation = "Average",
TimeGrain = "PT1M",
TimeWindow = "PT5M",
},
ScaleAction = new AutoscaleSettingProfileRuleScaleActionArgs
{
Cooldown = "PT1M",
Direction = "Increase",
Type = "ChangeCount",
Value = 1,
},
},
new AutoscaleSettingProfileRuleArgs
{
MetricTrigger = new AutoscaleSettingProfileRuleMetricTriggerArgs
{
MetricName = "Percentage CPU",
MetricResourceId = scaleSet.Id,
Operator = "LessThan",
Statistic = "Average",
Threshold = 25,
TimeAggregation = "Average",
TimeGrain = "PT1M",
TimeWindow = "PT5M",
},
ScaleAction = new AutoscaleSettingProfileRuleScaleActionArgs
{
Cooldown = "PT1M",
Direction = "Decrease",
Type = "ChangeCount",
Value = 1,
},
},
}
}
},
TargetResourceId = scaleSet.Id,
},
options);
return new AzureEndpoint(publicIp.Id);
};
}
}
}