PowerShell — Define shortcut/alias for common Kubernetes commands
While taking Kubernetes certification exams, I had set some aliases for common kubernetes commands at the start of exam to save some typing and avoid typing mistakes. I decided to do the same at home in powershell on my windows machine. In PowerShell, it is called Set-Alias. We can use it to set some alias for a command or just define short cut for any exe.
Set-Alias -Name k -Value kubectl
Set-Alias -Name np -Value C:\Windows\notepad.exe
Now we can just type k get all (shortening kubectl to k) or np to open notepad. Then I defined few more commands that can take parameters too. In powershell, we first have to define a function and then set alias for that function.
function GetPods([string]$namespace=”default”)
{
kubectl get pods -n $namespace
}
Set-Alias -Name kgp -Value GetPods
Now just typing kgp would get me list of pods in default namespace or
kgp kube-system would get list of pods from kube-system namespace. As a developer, some of most common kubernetes commands for me are: get pods, describe pod, get logs, apply yaml, exec into container etc. I created following file to set alias for these commands. Type following at powershell command prompt:
notepad $profile
Then paste following sample file (customize it for default namespace or naming convention or additional commands):
Set-Alias -Name k -Value kubectl
Set-Alias -Name np -Value C:\Windows\notepad.exefunction GetPods([string]$namespace=”default”)
{
kubectl get pods -n $namespace
}
Set-Alias -Name kgp -Value GetPodsfunction GetPodsWide([string]$namespace=”default”)
{
kubectl get pods -n $namespace -o wide
}
Set-Alias -Name kgpw -Value GetPodsfunction GetAll([string]$namespace=”default”)
{
kubectl get all -n $namespace
}
Set-Alias -Name kgall -Value GetAllfunction GetNodes()
{
kubectl get nodes -o wide
}
Set-Alias -Name kgn -Value GetNodesfunction DescribePod([string]$container, [string]$namespace=”default”)
{
kubectl describe po $container -n $namespace
}
Set-Alias -Name kdp -Value DescribePodfunction GetLogs([string]$container, [string]$namespace=”default”)
{
kubectl logs pod/$container -n $namespace
}
Set-Alias -Name klp -Value GetLogsfunction ApplyYaml([string]$filenamer, [string]$namespace=”default”)
{
kubectl apply -f $filename -n $namespace
}
Set-Alias -Name kaf -Value ApplyYamlfunction ExecContainerShell([string]$container, [string]$namespace=”default”)
{
kubectl exec -it $container -n $namespace — sh
}
Set-Alias -Name kexec -Value ExecContainerShell
Example Usage:
To get all pods with node information:
kgpw
To get all pods with node information for test namespace:
kgpw test
Exec into container:
kexec <container>
Exec into container in test namespace (as a convention, I make namespace always as last parameter):
kexec <container> test
In addition, I have used it to switch config context or login commands. I work in test namespace most of the time, so I have set default namespace in my file to test. These aliases are not big deal but just save some typing/typos during the day.
Thanks!