Installing MiniKube with Powershell

I noted on Twitter the other day that I had mixed feelings on getting a breakthrough on a problem that I had faced a couple of months ago by revisiting said problem with a fresh pair of eyes and a different point of view.

The problem was a pet project of mine born from the sheer audacity of could I? In this case, could I automate the manual process of installing Minikube; the development environment for Kubernetes platform.

Currently, installing Minikube has a handful of steps that felt rather tedious in the modern age. This is exacerbated by the fact that other Operating Systems such as Linux distros or OSX had package managers handle the installation in a more smooth manner. While there is the option of installing a third party package manager for Windows, I wanted to see how I could approach this problem with the power (!) of Powershell while keeping things as native as possible.

Once I sketched out the problem in Powershell ISE (I’m only just starting to get on the VS Code bandwagon but I already miss the magic of the CTRL-J snippets) , it quickly became apparent that the first major step of installing Hyper-V would require a restart in most, if not all cases and somehow my code needed to live and continue on after said restart.

This is where I got stuck in a bit of despair.

The magic of Workflows are a lie

It had seemed a straight forward problem, of which there were a couple of ways it could be skinned but the most appealing seemed to be the use of Workflows in Powershell. Workflows is actually a implementation of the Windows Workflow Foundation into the Powershell codebase. It is able to split up code into phases, steps etc… which enables it to run over long periods of time, sustain disruptions such as network outages, power failure etc… and is typically used in .NET applications.

Unfortunately, I banged my head against a brick wall attempting to get workflows to work for this particular endeavour to no avail. Despondent and beaten, I shelved my curiosity and enjoyed the Christmas/New Year festivities.

New year, new code

Here we are, in 2019 and I came across a fabulous deep dive into the use of Kubernetes in Secure Environments when my curiosity lit up once more and my drive told me I needed to have another go.

After a rethink about what the problem statement is, I focused on what I wanted to achieve rather than the technology on how to achieve it and the solution presented itself in an almost comically simple manner.

My code had already had enough tests in it that it would only perform the steps it needed to perform so all I really needed to do after configuring the Hyper-V feature and rebooting the workstation was to re-run the script. And that folks, is rather easy to do with the right entry into the Registry.

With that history lesson out of the way, I’ll cease my infernal yapping and bring forth thy code:

Install MiniKube + Kubectl
This script downloads the executables for MiniKube, Kubectl, configures Hyper-V as the hypervisor (if not configured already)
together with configuring a specific network adapter for use with the Minikube virtual machine
Version: 1.0
Author: James Pettigrove
## Check if running as a Administrator (needed for Hyper-V commands)
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
## Check HyperV status
$HypervState = (Get-WindowsOptionalFeature Online FeatureName:MicrosoftHyperV).State
## If missing, enable HyperV
if ($HypervState -eq "Disabled")
$EnableHyperV = Enable-WindowsOptionalFeature Online FeatureName:MicrosoftHyperVManagementPowershell,MicrosoftHyperVAll NoRestart
## If a restart is needed, add registry entry to continue after reboot
if ($EnableHyperV.RestartNeeded -eq $true)
## Set script to re-run after reboot
Set-ItemProperty Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce" Name "Install-MiniKube" Value "C:\Windows\system32\WindowsPowerShell\v1.0\Powershell.exe $PSCommandPath"
## And reboot
## Get version number of latest stable release of kubectl
$KubectlVersion = (Invoke-WebRequest uri UseBasicParsing).content.Trim()
## Turn off progress bars to speed up incoming download *sigh*
$ProgressPreference = "silentlyContinue"
## Download minikube + kubectl to temp location
$MinikubeUrl = ""
$MinikubeDl = "$Env:Temp\minikube.exe"
$KubctlUrl = "$KubectlVersion/bin/windows/amd64/kubectl.exe"
$KubctlDl = "$Env:Temp\kubectl.exe"
Invoke-WebRequest uri $MinikubeUrl OutFile $MinikubeDl
Invoke-WebRequest uri $KubctlUrl OutFile $KubctlDl
## Restore progress bars to default
$ProgressPreference = "Continue"
## Create and copy downloads to Minikube directory in Program Files
$MinikubeDst = "$Env:Programfiles\Minikube"
New-Item $MinikubeDst ItemType Container
Move-Item $MinikubeDl Destination $MinikubeDst
Move-Item $KubctlDl Destination $MinikubeDst
## Update PATH environment variable for this session
$env:Path +=";$MinikubeDst"
## Update PATH environment variable permentantly
[Environment]::SetEnvironmentVariable("Path", $env:Path + ";$MinikubeDst", [EnvironmentVariableTarget]::Machine)
## Check for and clear out any previous MiniKube configurations
if (Test-Path Path "$HOME\.minikube")
Remove-Item Path "$HOME\.minikube" Force Recurse
## Get Network Adapter of choice for use with MiniKube
$NetworkAdapter = Get-NetAdapter | Out-GridView OutputMode Single Title 'Pick your network adapter to use with MiniKube'
## Configure Hyper-V Virtual Switch with Network Adapter chosen previously
New-VMSwitch Name "Minikube" AllowManagementOS $true NetAdapterName $NetworkAdapter.Name
## Configure Minikube to use Hyper-V driver and Virtual Network Adapter
minikube config set vmdriver hyperv
minikube config set hypervvirtualswitch Minikube
minikube config set memory 2048
## Start MiniKube
minikube start

It’s quite dirty and probably needs another pass but I’d love to hear feedback. Hopefully it gets more people into the world of K8s via the local instance sandbox that is Minikube.

James Written by:

One Comment

Helpful? Have a question on the above?