'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
Solution 1:[1]
i would try initializing multiple objects for the cluster as suggested in the official client repo
from pick import pick # install pick using `pip install pick`
from kubernetes import client, config
from kubernetes.client import configuration
def main():
contexts, active_context = config.list_kube_config_contexts()
if not contexts:
print("Cannot find any context in kube-config file.")
return
contexts = [context['name'] for context in contexts]
active_index = contexts.index(active_context['name'])
cluster1, first_index = pick(contexts, title="Pick the first context",
default_index=active_index)
cluster2, _ = pick(contexts, title="Pick the second context",
default_index=first_index)
client1 = client.CoreV1Api(
api_client=config.new_client_from_config(context=cluster1))
client2 = client.CoreV1Api(
api_client=config.new_client_from_config(context=cluster2))
print("\nList of pods on %s:" % cluster1)
for i in client1.list_pod_for_all_namespaces().items:
print("%s\t%s\t%s" %
(i.status.pod_ip, i.metadata.namespace, i.metadata.name))
print("\n\nList of pods on %s:" % cluster2)
for i in client2.list_pod_for_all_namespaces().items:
print("%s\t%s\t%s" %
(i.status.pod_ip, i.metadata.namespace, i.metadata.name))
if __name__ == '__main__':
main()
You can also use python with pick for picking up the contexts
from pick import pick # `pip install pick`
from kubernetes import client, config
from kubernetes.client import configuration
def main():
contexts, active_context = config.list_kube_config_contexts()
if not contexts:
print("Cannot find any context in kube-config file.")
return
contexts = [context['name'] for context in contexts]
active_index = contexts.index(active_context['name'])
option, _ = pick(contexts, title="Pick the context to load",
default_index=active_index)
# Configs can be set in Configuration class directly or using helper
# utility
config.load_kube_config(context=option)
print("Active host is %s" % configuration.Configuration().host)
You can also try using the Environment variables in different terminals storing the different K8s contexts details.
Solution 2:[2]
- First create separate config files for cluster context you would like to switch
Terminal 1:
user@machine $ kubectl config view --minify --flatten --context=context-1 > $HOME/.kube/config-context-1
Terminal 2:
user@machine $ kubectl config view --minify --flatten --context=context-2 > $HOME/.kube/config-context-2
- Create Different virtual environments for different for different clusters and activate them
Terminal 1:
user@machine $ python3 -m venv context-1
user@machine $ . ./context-1/bin/activate
Terminal 2:
user@machine $ python3 -m venv context-2
user@machine $ . ./context-2/bin/activate
- Export the new config files to the respective environments
Terminal 1:
(context-1) user@machine $ export KUBECONFIG="$HOME/.kube/config-context-1"
Terminal 2:
(context-2) user@machine $ export KUBECONFIG="$HOME/.kube/config-context-2"
You check your pods now its would be of different contexts
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
