Configure Kubernetes prompt for bash and zsh using kube-ps1

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:

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 VariableDefaultMeaning
KUBE_PS1_NS_ENABLEtrueDisplay the namespace. If set to false, this will also disable KUBE_PS1_DIVIDER
KUBE_PS1_DIVIDER:The separator between context and namespace
KUBE_PS1_BINARYkubectlDefault Kubernetes binary
KUBE_PS1_CLUSTER_FUNCTIONNo default, must be user-suppliedFunction to customize how cluster is displayed
KUBE_PS1_SYMBOL_PADDINGtrueAdds a space (padding) after the symbol to prevent clobbering prompt characters
KUBE_PS1_SYMBOL_DEFAULTDefault prompt symbol. Unicode \u2388
KUBE_PS1_SYMBOL_USE_IMGfalse 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_FUNCTIONNo default, it must be user-suppliedFunction to customize how the namespace is displayed
KUBE_PS1_SYMBOL_ENABLEtrueDisplay 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:

VariableDefaultMeaning
KUBE_PS1_PREFIX_COLORnullSet the default color of the prompt prefix
KUBE_PS1_CTX_COLORredSet the default color of the context
KUBE_PS1_SYMBOL_COLORblueSet the default color of the Kubernetes symbol
KUBE_PS1_BG_COLORcyanSet default color of the namespace
KUBE_PS1_SUFFIX_COLORnullSet the default color of the prompt suffix
KUBE_PS1_NS_COLORcyanSet 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.

Your IT Journey Starts Here!

Ready to level up your IT skills? Our new eLearning platform is coming soon to help you master the latest technologies.

Be the first to know when we launch! Join our waitlist now.

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Leave a Comment

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

Related Post

This tutorial will show you how to install and use MongoDB Compass on Ubuntu 24.04 | Debian 12 in a […]

We looked at how to install Jira on Rocky Linux Server in our previous tutorial. In this article, We’ll learn […]

Atlassian created Jira, a bug tracking and agile project management application. Jira has a number of solutions and deployment choices […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.