Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Install kubectl.exe on windows using PowerShell

I was testing kubernetes in my environment and wanted to deploy kubectl.exe tool on couple of my colleagues system, which I can install or update easily remotly. For this I have written small script which will download latest version of kubectl.exe and set a Environment variable for the same. You can find latest version of kubectl here - https://storage.googleapis.com/kubernetes-release/release/stable.txt and the same can be downloaded from https://storage.googleapis.com/kubernetes-release/release/v1.20.2/bin/windows/amd64/kubectl.exe

I can test kubectl version that client is working fine. 

Microsoft Powershell kubernetes kubectl.exe containerization container virtulization docker cluster orchestration installation vcenter client.png

Download this script here or from github.com/janviudapi.

 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
<#
    .NOTES
    --------------------------------------------------------------------------------
     Code generated by:  Visual Studio Code
     Created on:         01/17/2021 4:57 AM
     Generated by:       http://vcloud-lab.com
     Written by:         J U
     Tested on:          Windows 10, PowerShell 5.1
    --------------------------------------------------------------------------------
    .DESCRIPTION
        GUI script generated using Visual Studio Code
#>

#Install Kubernetes on Windows
[CmdletBinding()]
param (
    [Parameter(
        Position=0,
        ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true,
        HelpMessage='Type folder path to download kubectl.exe'
    )]
    [Alias('Folder')]
    [string]$Path = 'C:\Kubernetes' #change here
)
$kubectlPath = $Path

if (-not(Test-Path $kubectlPath))
{
    Write-Host "`t-- New directory $kubectlPath created."
    $parent = Split-Path $kubectlPath -parent
    $leaf = Split-Path $kubectlPath -leaf
    New-Item -Name $leaf -Path $parent -ItemType Directory | Out-Null
}
else {
    Write-Host "`t-- directory $kubectlPath already exists."
}

if (-not(Test-Path "$kubectlPath\kubectl.exe"))
{
    Write-Host "`t-- Downloading latest version kubectl.exe to directory $kubectlPath."
    $latestVersion = Invoke-RestMethod -Uri 'https://storage.googleapis.com/kubernetes-release/release/stable.txt' 
    Invoke-WebRequest -Uri "https://storage.googleapis.com/kubernetes-release/release/$latestVersion/bin/windows/amd64/kubectl.exe" -OutFile "$kubectlPath\kubectl.exe"
}
else {
    Write-Host "`t-- kubectl.exe already exists under directory $kubectlPath."
}

$pathList = [System.Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::User) -split ';'
if (-not($pathList -contains $kubectlPath))
{
    Write-Host "`t-- Setting up Environment variable 'path' - $kubectlPath."
    [System.Environment]::SetEnvironmentVariable('Path', "$([System.Environment]::GetEnvironmentVariable('Path'));$kubectlPath", [System.EnvironmentVariableTarget]::User)
}
else {
    Write-Host "`t-- Environment variable 'path' - $kubectlPath already exists."
}

Useful Articles
Getting started Ansible AWX tower for IT automation run first playbook
Ansible for VMwary Using vmware_vm_inventory dynamic inventory plugin
Ansible selectattr The error was TemplateRuntimeError no test named 'equalto'
ansible create an array with set_fact
Ansible get information from esxi advanced settings nested dictionary with unique keynames

Go Back

Comment

Blog Search

Page Views

6108166

Follow me on Blogarama