-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare.ps1
127 lines (101 loc) · 5.25 KB
/
prepare.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
<#
.SYNOPSIS
Assists with preparing a Windows VM prior to calling kubeadm join
.DESCRIPTION
This script assists with joining a Windows node to a cluster.
- Downloads Kubernetes binaries (kubelet, kubeadm) at the version specified
- Registers wins as a service in order to run kube-proxy and cni as DaemonSets.
- Registers kubelet as an nssm service. More info on nssm: https://nssm.cc/
.PARAMETER KubernetesVersion
Kubernetes version to download and use
.EXAMPLE
PS> .\PrepareNode.ps1 -KubernetesVersion v1.17.0
#>
Param(
[parameter(Mandatory = $true, HelpMessage="Kubernetes version to use")]
[string] $KubernetesVersion
)
$ErrorActionPreference = 'Stop'
function DownloadFile($destination, $source) {
Write-Host("Downloading $source to $destination")
curl.exe --silent --fail -Lo $destination $source
if (!$?) {
Write-Error "Download $source failed"
exit 1
}
}
if (!$KubernetesVersion.StartsWith("v")) {
$KubernetesVersion = "v" + $KubernetesVersion
}
Write-Host "Using Kubernetes version: $KubernetesVersion"
$global:Powershell = (Get-Command powershell).Source
$global:PowershellArgs = "-ExecutionPolicy Bypass -NoProfile"
$global:KubernetesPath = "$env:SystemDrive\k"
$global:NssmInstallDirectory = "$env:ProgramFiles\nssm"
$kubeletBinPath = "$global:KubernetesPath\kubelet.exe"
mkdir -force "$global:KubernetesPath"
$env:Path += ";$global:KubernetesPath"
[Environment]::SetEnvironmentVariable("Path", $env:Path, [System.EnvironmentVariableTarget]::Machine)
DownloadFile $kubeletBinPath https://dl.k8s.io/$KubernetesVersion/bin/windows/amd64/kubelet.exe
DownloadFile "$global:KubernetesPath\kubelet.exe" https://dl.k8s.io/$KubernetesVersion/bin/windows/amd64/kubelet.exe
DownloadFile "$global:KubernetesPath\kubeadm.exe" https://dl.k8s.io/$KubernetesVersion/bin/windows/amd64/kubeadm.exe
DownloadFile "$global:KubernetesPath\kubectl.exe" https://dl.k8s.io/$KubernetesVersion/bin/windows/amd64/kubectl.exe
DownloadFile "$global:KubernetesPath\kube-proxy.exe" https://dl.k8s.io/$KubernetesVersion/bin/windows/amd64/kube-proxy.exe
DownloadFile "$global:KubernetesPath\wins.exe" https://github.com/rancher/wins/releases/download/v0.0.4/wins.exe
# Create host network to allow kubelet to schedule hostNetwork pods
Write-Host "Creating Docker host network"
docker network create -d nat host
Write-Host "Registering wins service"
wins.exe srv app run --register
start-service rancher-wins
mkdir -force C:\var\log\kubelet
mkdir -force C:\var\lib\kubelet\etc\kubernetes
mkdir -force C:\etc\kubernetes\pki
New-Item -path C:\var\lib\kubelet\etc\kubernetes\pki -type SymbolicLink -value C:\etc\kubernetes\pki\
Write-Host "Installing nssm"
$arch = "win32"
if ([Environment]::Is64BitOperatingSystem) {
$arch = "win64"
}
mkdir -Force $global:NssmInstallDirectory
DownloadFile nssm.zip https://k8stestinfrabinaries.blob.core.windows.net/nssm-mirror/nssm-2.24.zip
tar C $global:NssmInstallDirectory -xvf .\nssm.zip --strip-components 2 */$arch/*.exe
Remove-Item -Force .\nssm.zip
$env:path += ";$global:NssmInstallDirectory"
$newPath = "$global:NssmInstallDirectory;" +
[Environment]::GetEnvironmentVariable("PATH", [EnvironmentVariableTarget]::Machine)
[Environment]::SetEnvironmentVariable("PATH", $newPath, [EnvironmentVariableTarget]::Machine)
function Install-AwsKubernetesFlannel {
param (
[parameter(Mandatory=$true)] $InstallationDirectory,
[parameter(Mandatory=$false)] $FlanneldVersion = "0.11.0",
[parameter(Mandatory=$false)] $DownloadBranch = "master",
[parameter(Mandatory=$false)] $DownloadDirectory = (Join-Path -Path (Get-Item Env:TEMP).Value -ChildPath "flannel")
)
New-Item -Path $DownloadDirectory -ItemType "directory"
$GitHubMicrosoftSDNRepo = "github.com/Microsoft/SDN"
$GitHubFlannelRepo = "github.com/coreos/flannel"
# Download HNS Powershell module.
wget "https://$GitHubMicrosoftSDNRepo/raw/$DownloadBranch/Kubernetes/windows/hns.psm1" `
-OutFile "$InstallationDirectory/hns.psm1"
# Install flanneld executable.
wget "https://$GitHubFlannelRepo/releases/download/v$FlanneldVersion/flanneld.exe" `
-OutFile "$InstallationDirectory/flanneld.exe"
# Install CNI executables.
New-Item -Path "$InstallationDirectory/cni" -ItemType "directory"
wget "https://$GitHubMicrosoftSDNRepo/raw/$DownloadBranch/Kubernetes/flannel/l2bridge/cni/host-local.exe" `
-OutFile "$InstallationDirectory/cni/host-local.exe"
wget "https://$GitHubMicrosoftSDNRepo/raw/$DownloadBranch/Kubernetes/flannel/l2bridge/cni/flannel.exe" `
-OutFile "$InstallationDirectory/cni/flannel.exe"
wget "https://$GitHubMicrosoftSDNRepo/raw/$DownloadBranch/Kubernetes/flannel/overlay/cni/win-overlay.exe" `
-OutFile "$InstallationDirectory/cni/win-overlay.exe"
# Create directories needed for runtime.
New-Item -Path "c:/etc/kube-flannel" -ItemType directory -ErrorAction Ignore
New-Item -Path "c:/run/flannel" -ItemType directory -ErrorAction Ignore
Remove-Item -Path $DownloadDirectory -Recurse
}
Install-AwsKubernetesFlannel -InstallationDirectory $global:KubernetesPath
$WindowsVersion = (Get-ComputerInfo).WindowsVersion
# Pull ready-made Windows containers of the given Windows version.
docker pull "mcr.microsoft.com/powershell:nanoserver-$WindowsVersion"
docker pull "mcr.microsoft.com/dotnet/framework/aspnet:4.8"