Skip to content
Advertisement

Create Multiple Virtual envs for Multiple kubernetes contexts

As of now i do

 kubectl  --context <cluster context> get pod  -A 

to get pod in specific cluster

is there a python way to set kubernetes context for a virtual env , so we can use multiple context at the same time example :

Terminal 1:
(cluster context1) user@machine # 

Terminal 2:
(cluster context2) user@machine #

This should be equivalent of

Terminal 1:
user@machine #  kubectl  --context <cluster context1> get pod  -A 

Terminal 2:
user@machine # kubectl  --context <cluster context1> get pod  -A 

Advertisement

Answer

This isn’t probably a rational solution, but anyway… At some time I used different kubectl versions for different clusters and I came up with a venv-like solution to switch between them. I wrote text files like this:

export KUBECONFIG="/path/to/kubeconfig"
export PATH="/path/including/the/right/kubectl"

And activated them in the same fashion as venv: source the_file. If you can split your contexts to separate files, you can add export KUBECONFIG="/path/to/kubeconfig" to your venv/bin/activate and it will use the desired config when you activate the venv.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement