1 kube-ps1: Kubernetes prompt for bash and zsh
2 ============================================
4 A script that lets you add the current Kubernetes context and namespace
5 configured on `kubectl` to your Bash/Zsh prompt strings (i.e. the `$PS1`).
7 Inspired by several tools used to simplify usage of `kubectl`.
13 Homebrew package manager:
17 $ brew install kube-ps1
21 1. Clone this repository
22 2. Source the kube-ps1.sh in your `~/.zshrc` or your `~/.bashrc`
25 AUR Package available at [https://aur.archlinux.org/packages/kube-ps1/](https://aur.archlinux.org/packages/kube-ps1/).
29 source /path/to/kube-ps1.sh
30 PROMPT='$(kube_ps1)'$PROMPT
34 source /path/to/kube-ps1.sh
35 PS1='[\u@\h \W $(kube_ps1)]\$ '
38 ### Zsh Plugin Managers
40 #### Using [zplugin](https://github.com/zdharma/zplugin)
44 zplugin light jonmosco/kube-ps1
45 PROMPT='$(kube_ps1)'$PROMPT
50 The default prompt assumes you have the `kubectl` command line utility installed.
51 Official installation instructions and binaries are available:
53 [Install and Set up kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/)
55 If using this with OpenShift, the `oc` tool needs installed. It can be obtained
59 brew install openshift-cli
61 or the source can be downloaded:
63 [OC Client Tools](https://www.openshift.org/download.html)
65 Set the binary to `oc` with the following environment variable:
71 If neither binary is available, the prompt will print the following:
74 (<symbol>|BINARY-N/A:N/A)
79 There are several great tools that make using kubectl very enjoyable:
81 - [`kubectx` and `kubens`](https://github.com/ahmetb/kubectx) are great for
82 fast switching between clusters and namespaces.
86 I have begun porting kube-ps1 to tmux as a status line plugin. If you prefer
87 tmux, and like the functionality provided by kube-ps1, checkout the
88 [kube-tmux](https://github.com/jonmosco/kube-tmux) project
92 The default prompt layout is:
95 (<symbol>|<context>:<namespace>)
98 If the current-context is not set, kube-ps1 will return the following:
104 ## Enabling/Disabling
106 If you want to stop showing Kubernetes status on your prompt string temporarily
107 run `kubeoff`. To disable the prompt for all shell sessions, run `kubeoff -g`.
108 You can enable it again in the current shell by running `kubeon`, and globally
112 kubeon : turn on kube-ps1 status for this shell. Takes precedence over
113 global setting for current session
114 kubeon -g : turn on kube-ps1 status globally
115 kubeoff : turn off kube-ps1 status for this shell. Takes precedence over
116 global setting for current session
117 kubeoff -g : turn off kube-ps1 status globally
122 The default settings can be overridden in `~/.bashrc` or `~/.zshrc` by setting
123 the following environment variables:
125 | Variable | Default | Meaning |
126 | :------- | :-----: | ------- |
127 | `KUBE_PS1_BINARY` | `kubectl` | Default Kubernetes binary |
128 | `KUBE_PS1_NS_ENABLE` | `true` | Display the namespace. If set to `false`, this will also disable `KUBE_PS1_DIVIDER` |
129 | `KUBE_PS1_PREFIX` | `(` | Prompt opening character |
130 | `KUBE_PS1_SYMBOL_ENABLE` | `true ` | Display the prompt Symbol. If set to `false`, this will also disable `KUBE_PS1_SEPARATOR` |
131 | `KUBE_PS1_SYMBOL_PADDING` | `false` | Adds a space (padding) after the symbol to prevent clobbering prompt characters |
132 | `KUBE_PS1_SYMBOL_DEFAULT` | `⎈ ` | Default prompt symbol. Unicode `\u2388` |
133 | `KUBE_PS1_SYMBOL_USE_IMG` | `false` | ☸️ , Unicode `\u2638` as the prompt symbol |
134 | `KUBE_PS1_SEPARATOR` | | | Separator between symbol and context name |
135 | `KUBE_PS1_DIVIDER` | `:` | Separator between context and namespace |
136 | `KUBE_PS1_SUFFIX` | `)` | Prompt closing character |
137 | `KUBE_PS1_CLUSTER_FUNCTION` | No default, must be user supplied | Function to customize how cluster is displayed |
138 | `KUBE_PS1_NAMESPACE_FUNCTION` | No default, must be user supplied | Function to customize how namespace is displayed |
140 For terminals that do not support UTF-8, the symbol will be replaced with the
143 To disable a feature, set it to an empty string:
146 KUBE_PS1_SEPARATOR=''
151 The default colors are set with the following environment variables:
153 | Variable | Default | Meaning |
154 | :------- | :-----: | ------- |
155 | `KUBE_PS1_PREFIX_COLOR` | `null` | Set default color of the prompt prefix |
156 | `KUBE_PS1_SYMBOL_COLOR` | `blue` | Set default color of the Kubernetes symbol |
157 | `KUBE_PS1_CTX_COLOR` | `red` | Set default color of the context |
158 | `KUBE_PS1_SUFFIX_COLOR` | `null` | Set default color of the prompt suffix |
159 | `KUBE_PS1_NS_COLOR` | `cyan` | Set default color of the namespace |
160 | `KUBE_PS1_BG_COLOR` | `null` | Set default color of the prompt background |
162 Blue was used for the default symbol to match the Kubernetes color as closely
163 as possible. Red was chosen as the context name to stand out, and cyan for the
166 Set the variable to an empty string if you do not want color for each
170 KUBE_PS1_CTX_COLOR=''
173 Names are usable for the following colors:
176 black, red, green, yellow, blue, magenta, cyan
179 256 colors are available by specifying the numerical value as the variable
182 ## Customize display of cluster name and namespace
184 You can change how the cluster name and namespace are displayed using the
185 `KUBE_PS1_CLUSTER_FUNCTION` and `KUBE_PS1_NAMESPACE_FUNCTION` variables
188 For the following examples let's assume the following:
190 cluster name: `sandbox.k8s.example.com`
193 If you're using domain style cluster names, your prompt will get quite long
194 very quickly. Let's say you only want to display the first portion of the
195 cluster name (`sandbox`), you could do that by adding the following:
198 function get_cluster_short() {
199 echo "$1" | cut -d . -f1
202 KUBE_PS1_CLUSTER_FUNCTION=get_cluster_short
205 The same pattern can be followed to customize the display of the namespace.
206 Let's say you would prefer the namespace to be displayed in all uppercase
207 (`ALPHA`), here's one way you could do that:
210 function get_namespace_upper() {
211 echo "$1" | tr '[:lower:]' '[:upper:]'
214 export KUBE_PS1_NAMESPACE_FUNCTION=get_namespace_upper
217 In both cases, the variable is set to the name of the function, and you must have defined the function in your shell configuration before kube_ps1 is called. The function must accept a single parameter and echo out the final value.
219 ### Bug Reports and shell configuration
221 Due to the vast ways of customizing the shell, please try the prompt with a
222 minimal configuration before submitting a bug report.
224 This can be done as follows for each shell before loading kube-ps1:
240 * [Ahmet Alp Balkan](https://github.com/ahmetb)