This repository was archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 557
/
Copy pathkuberneteswindowssetup.ps1
535 lines (451 loc) · 18.2 KB
/
kuberneteswindowssetup.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
<#
.SYNOPSIS
Provisions VM as a Kubernetes agent.
.DESCRIPTION
Provisions VM as a Kubernetes agent.
#>
[CmdletBinding(DefaultParameterSetName="Standard")]
param(
[string]
[ValidateNotNullOrEmpty()]
$MasterIP,
[parameter()]
[ValidateNotNullOrEmpty()]
$KubeDnsServiceIp,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$MasterFQDNPrefix,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$Location,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$AgentKey,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$AzureHostname,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$AADClientId,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$AADClientSecret
)
$global:CACertificate = "{{WrapAsVariable "caCertificate"}}"
$global:AgentCertificate = "{{WrapAsVariable "clientCertificate"}}"
$global:DockerServiceName = "Docker"
$global:KubeDir = "c:\k"
$global:KubeBinariesSASURL = "{{WrapAsVariable "kubeBinariesSASURL"}}"
$global:KubeBinariesVersion = "{{WrapAsVariable "kubeBinariesVersion"}}"
$global:WindowsTelemetryGUID = "{{WrapAsVariable "windowsTelemetryGUID"}}"
$global:KubeletStartFile = $global:KubeDir + "\kubeletstart.ps1"
$global:KubeProxyStartFile = $global:KubeDir + "\kubeproxystart.ps1"
$global:TenantId = "{{WrapAsVariable "tenantID"}}"
$global:SubscriptionId = "{{WrapAsVariable "subscriptionId"}}"
$global:ResourceGroup = "{{WrapAsVariable "resourceGroup"}}"
$global:SubnetName = "{{WrapAsVariable "subnetName"}}"
$global:MasterSubnet = "{{WrapAsVariable "subnet"}}"
$global:SecurityGroupName = "{{WrapAsVariable "nsgName"}}"
$global:VNetName = "{{WrapAsVariable "virtualNetworkName"}}"
$global:RouteTableName = "{{WrapAsVariable "routeTableName"}}"
$global:PrimaryAvailabilitySetName = "{{WrapAsVariable "primaryAvailabilitySetName"}}"
$global:KubeClusterCIDR = "{{WrapAsVariable "kubeClusterCidr"}}"
$global:KubeServiceCIDR = "{{WrapAsVariable "kubeServiceCidr"}}"
$global:UseManagedIdentityExtension = "{{WrapAsVariable "useManagedIdentityExtension"}}"
$global:UseInstanceMetadata = "{{WrapAsVariable "useInstanceMetadata"}}"
$global:CNIPath = [Io.path]::Combine("$global:KubeDir", "cni")
$global:NetworkMode = "L2Bridge"
$global:CNIConfig = [Io.path]::Combine($global:CNIPath, "config", "`$global:NetworkMode.conf")
$global:CNIConfigPath = [Io.path]::Combine("$global:CNIPath", "config")
$global:WindowsCNIKubeletOptions = " --network-plugin=cni --cni-bin-dir=$global:CNIPath --cni-conf-dir=$global:CNIConfigPath"
$global:HNSModule = [Io.path]::Combine("$global:KubeDir", "hns.psm1")
$global:VolumePluginDir = [Io.path]::Combine("$global:KubeDir", "volumeplugins")
#azure cni
$global:NetworkPolicy = "{{WrapAsVariable "networkPolicy"}}"
$global:VNetCNIPluginsURL = "{{WrapAsVariable "vnetCniWindowsPluginsURL"}}"
$global:AzureCNIDir = [Io.path]::Combine("$global:KubeDir", "azurecni")
$global:AzureCNIBinDir = [Io.path]::Combine("$global:AzureCNIDir", "bin")
$global:AzureCNIConfDir = [Io.path]::Combine("$global:AzureCNIDir", "netconf")
$global:AzureCNIKubeletOptions = " --network-plugin=cni --cni-bin-dir=$global:AzureCNIBinDir --cni-conf-dir=$global:AzureCNIConfDir"
$global:AzureCNIEnabled = $false
filter Timestamp {"$(Get-Date -Format o): $_"}
function
Write-Log($message)
{
$msg = $message | Timestamp
Write-Output $msg
}
function Set-TelemetrySetting()
{
Set-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\DataCollection" -Name "CommercialId" -Value $global:WindowsTelemetryGUID -Force
}
function Resize-OSDrive()
{
$osDrive = ((Get-WmiObject Win32_OperatingSystem).SystemDrive).TrimEnd(":")
$size = (Get-Partition -DriveLetter $osDrive).Size
$maxSize = (Get-PartitionSupportedSize -DriveLetter $osDrive).SizeMax
if ($size -lt $maxSize)
{
Resize-Partition -DriveLetter $osDrive -Size $maxSize
}
}
function
Get-KubeBinaries()
{
$zipfile = "c:\k.zip"
Invoke-WebRequest -Uri $global:KubeBinariesSASURL -OutFile $zipfile
Expand-Archive -path $zipfile -DestinationPath C:\
}
function
Write-AzureConfig()
{
$azureConfigFile = $global:KubeDir + "\azure.json"
$azureConfig = @"
{
"tenantId": "$global:TenantId",
"subscriptionId": "$global:SubscriptionId",
"aadClientId": "$AADClientId",
"aadClientSecret": "$AADClientSecret",
"resourceGroup": "$global:ResourceGroup",
"location": "$Location",
"subnetName": "$global:SubnetName",
"securityGroupName": "$global:SecurityGroupName",
"vnetName": "$global:VNetName",
"routeTableName": "$global:RouteTableName",
"primaryAvailabilitySetName": "$global:PrimaryAvailabilitySetName",
"useManagedIdentityExtension": $global:UseManagedIdentityExtension,
"useInstanceMetadata": $global:UseInstanceMetadata
}
"@
$azureConfig | Out-File -encoding ASCII -filepath "$azureConfigFile"
}
function
Write-KubeConfig()
{
$kubeConfigFile = $global:KubeDir + "\config"
$kubeConfig = @"
---
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: "$global:CACertificate"
server: https://${MasterIP}:443
name: "$MasterFQDNPrefix"
contexts:
- context:
cluster: "$MasterFQDNPrefix"
user: "$MasterFQDNPrefix-admin"
name: "$MasterFQDNPrefix"
current-context: "$MasterFQDNPrefix"
kind: Config
users:
- name: "$MasterFQDNPrefix-admin"
user:
client-certificate-data: "$global:AgentCertificate"
client-key-data: "$AgentKey"
"@
$kubeConfig | Out-File -encoding ASCII -filepath "$kubeConfigFile"
}
function
New-InfraContainer()
{
cd $global:KubeDir
docker build -t kubletwin/pause .
}
function
Set-VnetPluginMode($mode)
{
# Sets Azure VNET CNI plugin operational mode.
$fileName = [Io.path]::Combine("$global:AzureCNIConfDir", "10-azure.conflist")
(Get-Content $fileName) | %{$_ -replace "`"mode`":.*", "`"mode`": `"$mode`","} | Out-File -encoding ASCII -filepath $fileName
}
function
Install-VnetPlugins()
{
# Create CNI directories.
mkdir $global:AzureCNIBinDir
mkdir $global:AzureCNIConfDir
# Download Azure VNET CNI plugins.
# Mirror from https://github.com/Azure/azure-container-networking/releases
$zipfile = [Io.path]::Combine("$global:AzureCNIDir", "azure-vnet.zip")
Invoke-WebRequest -Uri $global:VNetCNIPluginsURL -OutFile $zipfile
Expand-Archive -path $zipfile -DestinationPath $global:AzureCNIBinDir
del $zipfile
# Windows does not need a separate CNI loopback plugin because the Windows
# kernel automatically creates a loopback interface for each network namespace.
# Copy CNI network config file and set bridge mode.
move $global:AzureCNIBinDir/*.conflist $global:AzureCNIConfDir
# Enable CNI in kubelet.
$global:AzureCNIEnabled = $true
}
function
Set-AzureNetworkPolicy()
{
# Azure VNET network policy requires tunnel (hairpin) mode because policy is enforced in the host.
Set-VnetPluginMode "tunnel"
}
function
Set-NetworkConfig
{
Write-Log "Configuring networking with NetworkPolicy:$global:NetworkPolicy"
# Configure network policy.
if ($global:NetworkPolicy -eq "azure") {
Install-VnetPlugins
Set-AzureNetworkPolicy
}
}
function
Write-KubernetesStartFiles($podCIDR)
{
mkdir $global:VolumePluginDir
$KubeletArgList = @("--hostname-override=`$global:AzureHostname","--pod-infra-container-image=kubletwin/pause","--resolv-conf=""""""""","--kubeconfig=c:\k\config","--cloud-provider=azure","--cloud-config=c:\k\azure.json")
$KubeletCommandLine = @"
c:\k\kubelet.exe --hostname-override=`$global:AzureHostname --pod-infra-container-image=kubletwin/pause --resolv-conf="" --allow-privileged=true --enable-debugging-handlers --cluster-dns=`$global:KubeDnsServiceIp --cluster-domain=cluster.local --kubeconfig=c:\k\config --hairpin-mode=promiscuous-bridge --v=2 --azure-container-registry-config=c:\k\azure.json --runtime-request-timeout=10m --cloud-provider=azure --cloud-config=c:\k\azure.json
"@
if ($global:KubeBinariesVersion -lt "1.8.0")
{
# --api-server deprecates from 1.8.0
$KubeletArgList += "--api-servers=https://`${global:MasterIP}:443"
$KubeletCommandLine += " --api-servers=https://`${global:MasterIP}:443"
}
# more time is needed to pull windows server images
$KubeletCommandLine += " --image-pull-progress-deadline=20m --cgroups-per-qos=false --enforce-node-allocatable=`"`""
$KubeletCommandLine += " --volume-plugin-dir=`$global:VolumePluginDir"
# Configure kubelet to use CNI plugins if enabled.
if ($global:AzureCNIEnabled) {
$KubeletCommandLine += $global:AzureCNIKubeletOptions
} else {
$KubeletCommandLine += $global:WindowsCNIKubeletOptions
}
$KubeletArgListStr = "`"" + ($KubeletArgList -join "`",`"") + "`""
$KubeletArgListStr = "@`($KubeletArgListStr`)"
$kubeStartStr = @"
`$global:AzureHostname = "$AzureHostname"
`$global:MasterIP = "$MasterIP"
`$global:KubeDnsServiceIp = "$KubeDnsServiceIp"
`$global:MasterSubnet = "$global:MasterSubnet"
`$global:KubeClusterCIDR = "$global:KubeClusterCIDR"
`$global:KubeServiceCIDR = "$global:KubeServiceCIDR"
`$global:KubeBinariesVersion = "$global:KubeBinariesVersion"
`$global:CNIPath = "$global:CNIPath"
`$global:NetworkMode = "$global:NetworkMode"
`$global:CNIConfig = "$global:CNIConfig"
`$global:HNSModule = "$global:HNSModule"
`$global:VolumePluginDir = "$global:VolumePluginDir"
`$global:NetworkPolicy="$global:NetworkPolicy"
"@
if ($global:NetworkPolicy -eq "azure") {
$kubeStartStr += @"
Write-Host "NetworkPolicy azure, starting kubelet."
$KubeletCommandLine
"@
} else {
$kubeStartStr += @"
function
Get-DefaultGateway(`$CIDR)
{
return `$CIDR.substring(0,`$CIDR.lastIndexOf(".")) + ".1"
}
function
Get-PodCIDR()
{
`$podCIDR = c:\k\kubectl.exe --kubeconfig=c:\k\config get nodes/`$(`$global:AzureHostname.ToLower()) -o custom-columns=podCidr:.spec.podCIDR --no-headers
return `$podCIDR
}
function
Test-PodCIDR(`$podCIDR)
{
return `$podCIDR.length -gt 0
}
function
Update-CNIConfig(`$podCIDR, `$masterSubnetGW)
{
`$jsonSampleConfig =
"{
""cniVersion"": ""0.2.0"",
""name"": ""<NetworkMode>"",
""type"": ""wincni.exe"",
""master"": ""Ethernet"",
""capabilities"": { ""portMappings"": true },
""ipam"": {
""environment"": ""azure"",
""subnet"":""<PODCIDR>"",
""routes"": [{
""GW"":""<PODGW>""
}]
},
""dns"" : {
""Nameservers"" : [ ""<NameServers>"" ]
},
""AdditionalArgs"" : [
{
""Name"" : ""EndpointPolicy"", ""Value"" : { ""Type"" : ""OutBoundNAT"", ""ExceptionList"": [ ""<ClusterCIDR>"", ""<MgmtSubnet>"" ] }
},
{
""Name"" : ""EndpointPolicy"", ""Value"" : { ""Type"" : ""ROUTE"", ""DestinationPrefix"": ""<ServiceCIDR>"", ""NeedEncap"" : true }
}
]
}"
`$configJson = ConvertFrom-Json `$jsonSampleConfig
`$configJson.name = `$global:NetworkMode.ToLower()
`$configJson.ipam.subnet=`$podCIDR
`$configJson.ipam.routes[0].GW = `$masterSubnetGW
`$configJson.dns.Nameservers[0] = `$global:KubeDnsServiceIp
`$configJson.AdditionalArgs[0].Value.ExceptionList[0] = `$global:KubeClusterCIDR
`$configJson.AdditionalArgs[0].Value.ExceptionList[1] = `$global:MasterSubnet
`$configJson.AdditionalArgs[1].Value.DestinationPrefix = `$global:KubeServiceCIDR
if (Test-Path `$global:CNIConfig)
{
Clear-Content -Path `$global:CNIConfig
}
Write-Host "Generated CNI Config [`$configJson]"
Add-Content -Path `$global:CNIConfig -Value (ConvertTo-Json `$configJson -Depth 20)
}
try
{
`$masterSubnetGW = Get-DefaultGateway `$global:MasterSubnet
`$podCIDR=Get-PodCIDR
`$podCidrDiscovered=Test-PodCIDR(`$podCIDR)
# if the podCIDR has not yet been assigned to this node, start the kubelet process to get the podCIDR, and then promptly kill it.
if (-not `$podCidrDiscovered)
{
`$argList = $KubeletArgListStr
`$process = Start-Process -FilePath c:\k\kubelet.exe -PassThru -ArgumentList `$argList
# run kubelet until podCidr is discovered
Write-Host "waiting to discover pod CIDR"
while (-not `$podCidrDiscovered)
{
Write-Host "Sleeping for 10s, and then waiting to discover pod CIDR"
Start-Sleep 10
`$podCIDR=Get-PodCIDR
`$podCidrDiscovered=Test-PodCIDR(`$podCIDR)
}
# stop the kubelet process now that we have our CIDR, discard the process output
`$process | Stop-Process | Out-Null
}
# Turn off Firewall to enable pods to talk to service endpoints. (Kubelet should eventually do this)
netsh advfirewall set allprofiles state off
# startup the service
`$hnsNetwork = Get-HnsNetwork | ? Name -EQ `$global:NetworkMode.ToLower()
if (!`$hnsNetwork)
{
Write-Host "No HNS network found, creating a new one..."
ipmo `$global:HNSModule
`$hnsNetwork = New-HNSNetwork -Type `$global:NetworkMode -AddressPrefix `$podCIDR -Gateway `$masterSubnetGW -Name `$global:NetworkMode.ToLower() -Verbose
}
Start-Sleep 10
# Add route to all other POD networks
Update-CNIConfig `$podCIDR `$masterSubnetGW
$KubeletCommandLine
}
catch
{
Write-Error `$_
}
"@
}
$kubeStartStr | Out-File -encoding ASCII -filepath $global:KubeletStartFile
$kubeProxyStartStr = @"
`$env:KUBE_NETWORK = "l2bridge"
`$global:NetworkMode = "L2Bridge"
`$hnsNetwork = Get-HnsNetwork | ? Name -EQ `$global:NetworkMode.ToLower()
while (!`$hnsNetwork)
{
Start-Sleep 10
`$hnsNetwork = Get-HnsNetwork | ? Name -EQ `$global:NetworkMode.ToLower()
}
c:\k\kube-proxy.exe --v=3 --proxy-mode=kernelspace --hostname-override=$AzureHostname --kubeconfig=c:\k\config
"@
$kubeProxyStartStr | Out-File -encoding ASCII -filepath $global:KubeProxyStartFile
}
function
New-NSSMService
{
# setup kubelet
c:\k\nssm install Kubelet C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
c:\k\nssm set Kubelet AppDirectory $global:KubeDir
c:\k\nssm set Kubelet AppParameters $global:KubeletStartFile
c:\k\nssm set Kubelet DisplayName Kubelet
c:\k\nssm set Kubelet Description Kubelet
c:\k\nssm set Kubelet Start SERVICE_AUTO_START
c:\k\nssm set Kubelet ObjectName LocalSystem
c:\k\nssm set Kubelet Type SERVICE_WIN32_OWN_PROCESS
c:\k\nssm set Kubelet AppThrottle 1500
c:\k\nssm set Kubelet AppStdout C:\k\kubelet.log
c:\k\nssm set Kubelet AppStderr C:\k\kubelet.err.log
c:\k\nssm set Kubelet AppStdoutCreationDisposition 4
c:\k\nssm set Kubelet AppStderrCreationDisposition 4
c:\k\nssm set Kubelet AppRotateFiles 1
c:\k\nssm set Kubelet AppRotateOnline 1
c:\k\nssm set Kubelet AppRotateSeconds 86400
c:\k\nssm set Kubelet AppRotateBytes 1048576
net start Kubelet
# setup kubeproxy
c:\k\nssm install Kubeproxy C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
c:\k\nssm set Kubeproxy AppDirectory $global:KubeDir
c:\k\nssm set Kubeproxy AppParameters $global:KubeProxyStartFile
c:\k\nssm set Kubeproxy DisplayName Kubeproxy
c:\k\nssm set Kubeproxy DependOnService Kubelet
c:\k\nssm set Kubeproxy Description Kubeproxy
c:\k\nssm set Kubeproxy Start SERVICE_AUTO_START
c:\k\nssm set Kubeproxy ObjectName LocalSystem
c:\k\nssm set Kubeproxy Type SERVICE_WIN32_OWN_PROCESS
c:\k\nssm set Kubeproxy AppThrottle 1500
c:\k\nssm set Kubeproxy AppStdout C:\k\kubeproxy.log
c:\k\nssm set Kubeproxy AppStderr C:\k\kubeproxy.err.log
c:\k\nssm set Kubeproxy AppRotateFiles 1
c:\k\nssm set Kubeproxy AppRotateOnline 1
c:\k\nssm set Kubeproxy AppRotateSeconds 86400
c:\k\nssm set Kubeproxy AppRotateBytes 1048576
net start Kubeproxy
}
function
Set-Explorer
{
# setup explorer so that it is usable
New-Item -Path HKLM:"\\SOFTWARE\\Policies\\Microsoft\\Internet Explorer"
New-Item -Path HKLM:"\\SOFTWARE\\Policies\\Microsoft\\Internet Explorer\\BrowserEmulation"
New-ItemProperty -Path HKLM:"\\SOFTWARE\\Policies\\Microsoft\\Internet Explorer\\BrowserEmulation" -Name IntranetCompatibilityMode -Value 0 -Type DWord
New-Item -Path HKLM:"\\SOFTWARE\\Policies\\Microsoft\\Internet Explorer\\Main"
New-ItemProperty -Path HKLM:"\\SOFTWARE\\Policies\\Microsoft\\Internet Explorer\\Main" -Name "Start Page" -Type String -Value http://bing.com
}
try
{
# Set to false for debugging. This will output the start script to
# c:\AzureData\CustomDataSetupScript.log, and then you can RDP
# to the windows machine, and run the script manually to watch
# the output.
if ($true) {
Write-Log "Provisioning $global:DockerServiceName... with IP $MasterIP"
Write-Log "apply telemetry data setting"
Set-TelemetrySetting
Write-Log "resize os drive if possible"
Resize-OSDrive
Write-Log "download kubelet binaries and unzip"
Get-KubeBinaries
Write-Log "Write azure config"
Write-AzureConfig
Write-Log "Write kube config"
Write-KubeConfig
Write-Log "Create the Pause Container kubletwin/pause"
New-InfraContainer
Write-Log "Configure networking"
Set-NetworkConfig
Write-Log "write kubelet startfile with pod CIDR of $podCIDR"
Write-KubernetesStartFiles $podCIDR
Write-Log "install the NSSM service"
New-NSSMService
Write-Log "Set Internet Explorer"
Set-Explorer
Write-Log "Setup Complete"
}
else
{
# keep for debugging purposes
Write-Log ".\CustomDataSetupScript.ps1 -MasterIP $MasterIP -KubeDnsServiceIp $KubeDnsServiceIp -MasterFQDNPrefix $MasterFQDNPrefix -Location $Location -AgentKey $AgentKey -AzureHostname $AzureHostname -AADClientId $AADClientId -AADClientSecret $AADClientSecret"
}
}
catch
{
Write-Error $_
}