Create Git Bash profile in Windows Terminal and enable kubectl auto complete
Thirumurthi S
4 min read
Create Git Bash profile in Windows Terminal and enable kubectl auto completion
- In this blog we will see how to setup the Git Bash in Windows Terminal. Along with that we can setup the kubectl alias and enable auto completion feature.
- When working with the kubernetes resources, this auto completion feature helps a lot. Earlier I used to copy paste the pod names created by the deployment manifest which has the hash. With auto complete feature it makes it easy for us.
Pre-requisites
Docker Desktop Kubernetes cluster
- The new version of Docker Desktop for Windows provides feature to enable Kubernetes cluster
- For this blog I have enabled the Docker K8S cluster, this is very easy to enable
- From Docker Desktop settings under Kubernetes, enable it. Clicking Apply which will install the cluster
- Use the
wsl -l
to check if the ubutnu distro is installed. If not install it from the Microsoft Store. - Also, I had configured WSL2 Ubuntu 22.04 Distribution instead of using the Docker Default WSL
- The Ubuntu distribution can be set in the Docker Desktop from Settings -> Resources -> WSL
- The advantage is that we can mount volumes from WSL to K8S cluster
kubectl
kubectl
installed in the machine- In my case I installed the kubectl using Chocolatey windows package manager
- Open command prompt as administrator user and issue the command
> choco install kubernetes-cli
Git Bash CLI
- Git Bash CLI can be installed form the GitHub website
Windows Terminal
Window Terminal
installed in the machine- Windows Terminal is a application like PowerShell, Bash, cmd prompt, etc. It support multiple browser and we can create profiles and perform customizations.
To learn more about check the Microsoft Learn documentation
Installation steps:
- We can install
Windows Terminal
directly from the Microsoft Store, just search and install it. - To install it manually, download the bundle
WindowsTerminal*.msixbundle
from the GitHub release page. Refer the GitHub terminal project link- Issue the command
Add-AppxPackage <downloaded-msixbundle>
- Manual installation requires
VC++ v14 Desktop Framework Package
to be installed separately
- Issue the command
- We can install
Create a profile for Git Bash in Windows Terminal
Open Windows Terminal, click the v near the tab + symbol.
- Select the
Settings
- Select the
- Click the
Add a new profile
- Click the
- Below snapshot displays the values I used to create the profile.
- We need to provide the path of the Git Bash.exe
- We can add the path to the windows icon if needed
- I unchecked the source path, which was selected by default. In my case I wanted the git bash to point to userprofile path when opened in Windows Terminal
- I left the title blank, other options also blank
- Click Save
- Click the
+
near the tabs at the top of Windows Terminal, now it will list the newly created GitBash profile.
- Once the Git Bash is opened in Windows terminal, lets try
kubectl
command to list the context fromkubeconfig
.- Command to list the context is
kubectl config get-contexts
, I had multiple context, refer snapshot below
- Command to list the context is
Instruction to set Kubectl alias and auto completion feature
- I am setting up the kubectl alias as k
- Navigate to
C:\Program Files\Git\etc\profile.d
in windows - Open the
alias.sh
file and update the below contents to the bottom of the file
- Navigate to
alias k=kubectl
source <(kubectl completion bash)
complete -o default -F __start_kubectl k
- Alternatively we can create a
.bashrc
file under theC:/Users/<user-name>/
(open cmd prompt and use the commandecho %USERPROFILE%
to display the current user path this where we need to create the .bashrc file and copy paste the content if the file doesn't exists)
Validate kubectl alias and auto completion works
- Open up Windows Terminal, click
+
near the tabs on top, select the GitBash Profile - Type
k -n kube-s
and hit tab, you should see the namespace getting auto-filled in this casekube-system
.
- After hitting tab with ,get pods, below result is displayed
- The auto complete feature will also work on the pod names, for example
# Create a simple nginx pod
$ k run nginx --image=nginx
# Type, below command and hit tab if nginx is the only pod it will auto complete it.
$ k -n default logs pod/n
1
Subscribe to my newsletter
Read articles from Thirumurthi S directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by