Created
September 23, 2023 10:03
-
-
Save kitos9112/44b9d550428abb89a571fd611d019cba to your computer and use it in GitHub Desktop.
Bootstraps a Kubernetes cluster using multipass and HyperV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# | |
# | |
# Generated by: Marcos Soutullo | |
# | |
# Last edited on: 22/09/2023 | |
# | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$false)] | |
[ValidateSet('20.04', '22.04')] | |
[string] | |
$UbuntuImage = "22.04", | |
[Parameter(Mandatory=$false)] | |
[ValidateRange(1,4)] | |
[Int64] | |
$WorkersCount = 1, | |
[Parameter(Mandatory=$false)] | |
[ValidateRange(1,3)] | |
[Int64] | |
$MastersCount = 1, | |
[Parameter(Mandatory=$false)] | |
[ValidatePattern('(?-i:^[0-9]+G$)')] | |
[string] | |
$MastersMem = "4G", | |
[Parameter(Mandatory=$false)] | |
[ValidateRange(1,4)] | |
[Int64] | |
$MastersCpu = 2, | |
[Parameter(Mandatory=$false)] | |
[ValidatePattern('(?-i:^[0-9]+G$)')] | |
[string] | |
$WorkersMem = "4G", | |
[Parameter(Mandatory=$false)] | |
[ValidateRange(1,4)] | |
[int] | |
$WorkersCpu = 2, | |
[Parameter(Mandatory=$false)] | |
[ValidatePattern('(?-i:^[0-9]+G$)')] | |
[string] | |
$WorkersDisk = "15G", | |
[Parameter(Mandatory=$false)] | |
[ValidateSet('start', 'stop', 'delete', 'launch')] | |
[string] | |
$Action = "launch", | |
[Parameter(Mandatory=$false)] | |
[string] | |
$NetworkName = "Multipass", | |
[Parameter(Mandatory=$false)] | |
[switch] | |
$ForceReCreation = $false, | |
# K8S Cloud-init userdata to load once launching Ubuntu VMs | |
[Parameter(Mandatory=$false)] | |
[System.IO.FileInfo] | |
$CloudInitUserDataPath = "C:\Users\msoutullo\data\kubernetes\multipass\k8s-user-data.yaml" | |
) | |
function Write-Log { | |
Param( | |
$Message, | |
$Path = "$env:TEMP\bootstrap-k8s.log" | |
) | |
"[$([DateTime]::Now)] $Message" | Tee-Object -FilePath $Path | |
} | |
$MasterNodePrefix="k8s-master-" | |
$WorkerNodePrefix="k8s-worker-" | |
# $MultipassCache = "C:\Windows\System32\config\systemprofile\AppData\Local\multipassd\cache\network-cache\data8" | |
# Restart-Service -Name "multipass" -Force -Verb runAs | |
# Get-ChildItem $MultipassCache -recurse -Verb runAs | Where-Object { !($_.PSIsContainer) } | ForEach-Object { remove-item $_.FullName -Force -Verb runAs } | |
try { | |
$MultipassOutput = (multipass list --format json |ConvertFrom-Json).list |Where-Object name -Match "^$MasterNodePrefix|$WorkerNodePrefix" | |
if ($Action -in "stop","start","delete" ) { | |
$MastersCount = ($MultipassOutput |Where-Object name -Match "^$MasterNodePrefix" | Measure-Object).Count | |
$WorkersCount = ($MultipassOutput |Where-Object name -Match "^$WorkerNodePrefix" | Measure-Object).Count | |
} | |
} catch { | |
Write-Log "Not able to parse 'multipass list --format json' - Is it Multipass actually running?" | |
Write-Log "Exception Message `n`n$_.Exception.Message" | |
exit 1 | |
} | |
## 1. MasterNodePrefix NODES | |
#### Perform action [launch | start | stop | delete] | |
foreach($i in 1..$MastersCount) { | |
Write-Log "Triggering build of master node... $i out of $MastersCount master/s count" | |
if ($Action -eq "launch" ) { | |
$MultipassLaunch = "multipass launch $UbuntuImage --cpus 2 --memory 2G --name $MasterNodePrefix$i --disk 10G --cloud-init $CloudInitUserDataPath --network $NetworkName" | |
if ("$MasterNodePrefix$i" -notin $MultipassOutput.name) { | |
Write-Log "START JOB --> $MultipassLaunch" | |
Invoke-Expression -Command $MultipassLaunch | |
} elseif ($ForceReCreation-eq $true ) { | |
Write-Log "Forcing $MasterNodePrefix$i deletion..." | |
Invoke-Expression -Command "multipass delete $MasterNodePrefix$i && multipass purge" | |
Write-Log "START JOB --> $MultipassLaunch" | |
Invoke-Expression -Command $MultipassLaunch | |
} else { | |
Write-Log "Starting $MasterNodePrefix$i node..." | |
Invoke-Expression -Command "multipass start $MasterNodePrefix$i" | |
} | |
} else { | |
Write-Log "Deleting multipass $i out of $MastersCount master/s count " | |
Invoke-Expression -Command 'multipass $Action $MasterNodePrefix$i' | |
if ($Action -eq "delete" ) { | |
Invoke-Expression -Command 'multipass purge' | |
} | |
} | |
} | |
## 1. WORKER NODES | |
# Perform action on [launch | start | stop | delete] worker nodes | |
foreach($i in 1..$WorkersCount) { | |
if ($Action -eq "launch" ) { | |
Write-Log "Triggering build of worker node... $i out of $WorkersCount workers count" | |
$MultipassLaunch = "multipass launch $UbuntuImage --cpus $WorkersCpu --memory $WorkersMem --name $WorkerNodePrefix$i --disk $WorkersDisk --cloud-init $CloudInitUserDataPath --network $NetworkName" | |
if ("$WorkerNodePrefix$i" -notin $MultipassOutput.name) { | |
Write-Log "START JOB --> $MultipassLaunch" | |
Invoke-Expression -Command $MultipassLaunch | |
} elseif ($ForceReCreation-eq $true ) { | |
Write-Log "Forcing $WorkerNodePrefix$i deletion..." | |
Invoke-Expression -Command "multipass delete $WorkerNodePrefix$i && multipass purge" | |
Invoke-Expression -Command $MultipassLaunch | |
} else { | |
Write-Log "Starting $WorkerNodePrefix$i node..." | |
Invoke-Expression -Command "multipass start $WorkerNodePrefix$i" | |
} | |
} else { | |
Write-Log "Deleting multipass $i out of $WorkersCount worker/s count " | |
Invoke-Expression -Command "multipass $Action $WorkerNodePrefix$i" | |
if ($Action -eq "delete" ) { | |
Invoke-Expression -Command "multipass purge" | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment