Display the current kubectl context in the Bash prompt
CommentsIt’s an hot day at Spreaker: we’re finally rolling out to production the very first piece of our next-generation infrastructure, based upon Docker and Kubernetes. Now that we’ve two environments running Kubernetes (staging
and prod
) it’s vital we don’t mess up with kubectl
, manually running on production commands that were supposed to run in our staging environment.
The easy way to reduce such risk that came out to my mind is to display the current kubectl context in the shell prompt. Basically, something like this:
After a quick Google search, I didn’t find anything similar (please correct me if I’m wrong), so here is how I did it.
1. Create ~/kube-prompt.sh
Create a script kube-prompt.sh
in your home with the following content. It just defines a function __kube_ps1()
that prints the current kubectl context (if any):
## 2. Edit your Bash prompt in ~/.bash_profile
Edit the file .bash_profile
in your home, loading ~/kube-prompt.sh
and injecting __kube_ps1()
in your prompt. For example, my prompt is:
You may also be interested in ...
- Kubernetes Security: book review
- My take on the future of applications development and operability
- PHP realpath cache and Kubernetes secrets / configmap updates
- Kubernetes pods /etc/resolv.conf ndots:5 option and why it may negatively affect your application performances
- AWS re:invent 2017 annoucements
- Kubernetes RBAC with kops
- Stepping back from CTO and jumping into operations
- KubeCon 2017 - Kubernetes Takeaways
- Prometheus: understanding the delays on alerting
- Graceful shutdown of pods with Kubernetes
- Distributed Matters Conf: Takeaways
Upcoming conferences
Incontro DevOps 2020 | Virtual | 22 October 2020 |
---|