kubeprompt is a Bash script that dynamically updates your shell prompt to display the current Kubernetes cluster and namespace.
It enhances usability by providing quick commands to switch contexts, namespaces, and clusters efficiently.
β
Dynamic Kubernetes-aware prompt displaying the active cluster & namespace
β
Quick context switching with kc and namespace switching with kn
β
Aliases for frequently used kubectl commands (k, kgp, kgd, etc.)
β
Retains last used namespace per cluster instead of resetting to default
β
**Autocompletion support for kubectl, kubectx, and kubens
sudo yum install -y bash-completion # For redhat or centos
exec bash # Restart shell for changes to take effect
type _get_comp_words_by_ref # Check if Bash completion is working
echo 'source <(kubectl completion bash)' >> ~/.bashrc
source ~/.bashrc # Reload Bash settingsalias k='kubectl' # Set 'k' as a shortcut for kubectl
complete -F __start_kubectl k # Enable autocompletion for alias 'k'
echo 'complete -F __start_kubectl k' >> ~/.bashrc # Persist autocompletionsudo git clone https://github.com/ahmetb/kubectx /opt/kubectx
sudo ln -s /opt/kubectx/kubectx /usr/local/bin/kubectx
sudo ln -s /opt/kubectx/kubens /usr/local/bin/kubens
echo "source /opt/kubectx/completion/kubectx.bash" >> ~/.bashrc
echo "source /opt/kubectx/completion/kubens.bash" >> ~/.bashrc
source ~/.bashrcgit clone https://github.com/yourusername/kubeprompt.git
cd kubepromptA shell prompt extension for Kubernetes users that displays the current Kubernetes cluster and namespace in your terminal prompt.
For Linux (.bashrc):
echo "source ~/kubeprompt/kubeprompt.sh" >> ~/.bashrc
source ~/.bashrcFor macOS (.bash_profile):
echo "source ~/kubeprompt/kubeprompt.sh" >> ~/.bash_profile
source ~/.bash_profileAfter installing, your shell prompt will look like this:
user@machine [~/work] [k-1] | kube-system $[k-1]β Shows the current Kubernetes cluster (config1, config2, etc.).kube-systemβ Displays the active namespace.
Switch between Kubernetes clusters using:
k1 # Switch to cluster 1
k2 # Switch to cluster 2
k3 # Switch to cluster 3
k4 # Switch to cluster 4The prompt updates immediately with the new cluster and retains the last used namespace.
Example Scenario:
user@machine [~/work] [k-1] | default $
$ k2
Switched to Kubernetes config: ~/.kube/config2 with namespace: default
user@machine [~/work] [k-2] | default $kc my-clusterSwitches to the given Kubernetes context while preserving the current namespace.
Example Scenario:
user@machine [~/work] [k-1] | default $
$ kc my-new-cluster
Switched to Kubernetes context: my-new-cluster
user@machine [~/work] [k-3] | default $kn kube-systemChanges the current namespace to kube-system.
Example Scenario:
user@machine [~/work] [k-1] | default $
$ kn kube-system
Switched to namespace: kube-system
user@machine [~/work] [k-1] | kube-system $klistShows available kubeconfigs stored in ~/.kube/.
Example Output:
config1
config2
config3
config4kcurDisplays the currently used Kubernetes configuration.
Example Output:
Current KUBECONFIG: ~/.kube/config2kclearUnsets the current KUBECONFIG variable.
| Command | Description |
|---|---|
k |
Alias for kubectl |
kg |
Alias for kubectl get |
kgp |
Get all pods (kubectl get pods) |
kgd |
Get all deployments (kubectl get deployments) |
kgn |
Get all nodes (kubectl get nodes) |
kgs |
Get all services (kubectl get services) |
kga |
Get all Kubernetes objects (kubectl get all) |
- Displays current Kubernetes cluster and namespace.
- Uses short names
[k-1],[k-2]instead of longkubernetes-admin@cluster-name.
- If you switch from
k1tok2, the namespace remains the same.
- Uses
kubectx (kc)for context switching. - Uses
kubens (kn)for namespace switching.
- Works with
kubectl,kubectx, andkubens.
Make sure the following tools are installed:
sudo apt install -y kubectl # Debian/Ubuntu
brew install kubectl # macOSsudo apt install kubectx # Debian/Ubuntu
brew install kubectx # macOSTry running:
source ~/.bashrc
kn kube-systemIf the issue persists, restart your terminal.
Feel free to fork and submit a Pull Request! π
If you have ideas for improvements, create an Issue in the repository.
This project is licensed under the MIT License.
Now, you can manage Kubernetes clusters and namespaces efficiently with a single glance at your terminal!
π₯ Star the repository on GitHub and spread the word! π₯
π GitHub Repository: https://github.com/pmaddev/kubeprompt