Home > Net >  Create Multiple Virtual envs for Multiple kubernetes contexts
Create Multiple Virtual envs for Multiple kubernetes contexts

Time:11-26

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 

CodePudding user response:

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.

CodePudding user response:

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() 

Read more here

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.

  • Related