The kubectl is a vital tool when managing Kubernetes. This tool occupies the engineer’s life and is hence worth spending time configuring it for an efficient and productive environment. There are several commands one can use when managing the Kubernetes resources via kubectl. These include; kubectl get pod, kubectl logs pod_name e.t.c
kube-ps1 is a Kubernetes Bash/Zsh prompt that can be used to display the current cluster and namespace. In other words, it allows you to add the current Kubernetes context and namespace to your Bash/Zsh prompt strings. It aims to simplify the use of kubectl, inspired by several tools.
This guide aims to demonstrate how to configure the Kubernetes prompt for bash and zsh using kube-ps1. I assume that you already have a Kubernetes cluster set up. There are several ways to deploy the cluster as illustrated in the below guides:
- Deploy K0s Kubernetes Cluster on Linux using K0sctl
- Deploy Kubernetes Cluster on Oracle Linux 8 with Kubeadm
- Install Kubernetes on Alpine Linux with k3s
- Deploy Kubernetes Cluster on Rocky Linux 9 using k0s
- Deploy k0s Kubernetes Cluster on Ubuntu
Once the cluster is set up, you need to install kubectl.
curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin
For k0s, use the generated certificate to access the cluster.
export KUBECONFIG=/var/lib/k0s/pki/admin.conf
Install kube-ps1 on your System
kube-ps1 can be installed on several platforms as shown below:
- On macOS:
Install kube-ps1 using Homebrew:
brew update
brew install kube-ps1
- On Linux/macOS:
You can also install kube-ps1 using a source file. Install git tool
### Ubuntu / Debian ###
sudo apt update
sudo apt install git
###CentOS / Fedora ###
sudo yum -y install git
First, clone the repository below:
cd ~/
git clone https://github.com/jonmosco/kube-ps1.git
Source kube-ps1.sh in the directory(kube-ps1) to Bash/Zsh:
$ vim ~/.bashrc #for Bash
source "/home/$USER/kube-ps1/kube-ps1.sh"
PS1='[\u@\h \W $(kube_ps1)]\$ '
$ vim ~/.zshrc #for ZSH
source "/home/$USER/kube-ps1/kube-ps1.sh"
PS1='[\u@\h \W $(kube_ps1)]\$ '
The /home
on macOS is equivalent to /Users
.
kube-ps1 works best with kubens
and kubectx
. They can be installed with the command:
sudo 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
Source your bashrc file:
source ~/.bashrc
Using kube-ps1 to Configure Kubernetes prompt for bash and zsh
Now you can use kube-ps1 to configure Kubernetes prompt for bash and zsh. Switch the namespace:
kubens kube-public
Toggle kube-ps1 on and off:
kubeoff
kubeon
Sample Output:

You can also use the below commands to turn on/off kube-ps1 globally:
kubeon -g
kubeoff -g
Customize kube-ps1
There are several environment variables that can be used to customize kube-ps1.
The table below shows the available environment variable and their use:
Environment Variable | Default | Meaning |
KUBE_PS1_NS_ENABLE | true | Display the namespace. If set to false, this will also disable KUBE_PS1_DIVIDER |
KUBE_PS1_DIVIDER | : | The separator between context and namespace |
KUBE_PS1_BINARY | kubectl | Default Kubernetes binary |
KUBE_PS1_CLUSTER_FUNCTION | No default, must be user-supplied | Function to customize how cluster is displayed |
KUBE_PS1_SYMBOL_PADDING | true | Adds a space (padding) after the symbol to prevent clobbering prompt characters |
KUBE_PS1_SYMBOL_DEFAULT | ⎈ | Default prompt symbol. Unicode \u2388 |
KUBE_PS1_SYMBOL_USE_IMG | false | Unicode \u2638 as the prompt symbol |
KUBE_PS1_SEPARATOR | | | The separator between symbol and context name |
KUBE_PS1_PREFIX | ( | Prompt opening character |
KUBE_PS1_SUFFIX | ) | Prompt closing character |
KUBE_PS1_NAMESPACE_FUNCTION | No default, it must be user-supplied | Function to customize how the namespace is displayed |
KUBE_PS1_SYMBOL_ENABLE | true | Display the prompt Symbol. If set to false, this will also disable KUBE_PS1_SEPARATOR |
Proceed and make customizations to kube-ps1
For example, use the below command and see what happens:
KUBE_PS1_SYMBOL_USE_IMG=true
Sample output:

Change the prefix and suffix
KUBE_PS1_PREFIX={
KUBE_PS1_SUFFIX=}
Sample Output:

To disable a feature, provide an empty string. For example:
KUBE_PS1_SEPARATOR=''
Set kube-ps1 Colors
The table below shows the variable to set colors and the default values:
Variable | Default | Meaning |
KUBE_PS1_PREFIX_COLOR | null | Set the default color of the prompt prefix |
KUBE_PS1_CTX_COLOR | red | Set the default color of the context |
KUBE_PS1_SYMBOL_COLOR | blue | Set the default color of the Kubernetes symbol |
KUBE_PS1_BG_COLOR | cyan | Set default color of the namespace |
KUBE_PS1_SUFFIX_COLOR | null | Set the default color of the prompt suffix |
KUBE_PS1_NS_COLOR | cyan | Set the default color of the namespace |
To set a color, execute the correct variable. For example, setting the prefix and suffix colors to yellow, the commands will be:
KUBE_PS1_PREFIX_COLOR=yellow
KUBE_PS1_SUFFIX_COLOR=yellow
Execution output:

Set the context color.
KUBE_PS1_CTX_COLOR=magenta
Sample Output:

If you want to disable the color, provide an empty string. For example;
KUBE_PS1_CTX_COLOR=''
Output:

The colors names available are black, green, blue, red, cyan, magenta, and yellow. Other 256 colors are also available by specifying the numerical value as the variable.
That marks the end of this brief guide on how to configure the Kubernetes prompt for bash and zsh using kube-ps1. I hope this was valuable.