-->

DEVOPSZONES

  • Recent blogs

    kubectl Switch Context

    kubectl Switch Context 

    We frequently need to swap between Kubernetes clusters for different projects. Keeping track of all of their kubectl configurations and files might be challenging. The Kubernetes endpoint and credentials are by default saved in the /.kube/config file by the kubectl command-line client. The configuration is provided as a yml file by the cloud console when working with a Kubernetes instance that is hosted in the cloud. Then, the file must be given as the value of the kubectl environment variable KUBECONFIG. This can quickly grow annoying and challenging to handle.

    A Context in Kubernetes is used to group access parameters in a kubeconfig file under names that are simple to remember. The three parameters that each Context contains are Cluster, Namespace, and User. In this post, we'll demonstrate using the kubectl command to examine and modify Context in Kubernetes.

    How Does Kubernetes Change Context?

    You connect to a specific cluster using a configuration known as a context. The conventional method for switching between, reading, and altering various Kubernetes setups is kubectl config (aka Kubernetes contexts). Here are some examples of frequently used kubectl commands:

    • To see the current context, use current-context.
    • To remove a specific cluster from the kubeconfig, use Delete-cluster.
    • To describe one or more contexts, use the get-contexts command.
    • The kubeconfig-defined clusters are shown by the get-clusters command.
    • Set-context updates the kubeconfig contextual entry.
    • A user entry is created using the kubeconfig command set-credentials.
    • The view is used to display the unified kubeconfig settings.

    To set per-context settings, use the command "kubectl config set-context my-context —cluster=my-app —namespace=production." With the default Kubernetes cluster and namespace options, this method will create a new context with the name my-context. The arguments from the my-context context would be used in any further kubectl calls, connecting you to the my-app cluster inside the production namespace.


    By default, the kubectl tool uses the current Context's arguments to connect with the cluster. The following command will result in the display of the current context.

    The following command is used in a kubeconfig file to list all the contexts.

    kubectl config get-contexts


    Set Up a Fresh Context

    Because there isn't a context that can be utilised for switching, we have created one here. Based on a username, this command will build a context.

    kubectl config set-context my-context –user=cluster-admin

    Use the following code to revert to a previous place.

    kubectl config use-context minikube

    Show Merged Kubeconfig settings with kubectl config view.


    # display combined configuration from many kubeconfig files at once

    KUBECONFIG=~/.kube/config:~/.kube/kubconfig2

    kubectl config view

    # get the password for the manas user

    kubectl config view -o jsonpath='{.users[?(@.name == "manas")].user.password}'

     # display the first user

    kubectl config view -o jsonpath='{.users[].name}'   

    # get a list of users

    kubectl config view -o jsonpath='{.users[*].name}'   

    # display list of contexts

    kubectl config get-contexts    

    # display the current-context                      

    kubectl config current-context       

    # set the default context to my-cluster-name                

    kubectl config use-context my-cluster-name           

    # set a cluster entry in the kubeconfig

    kubectl config set-cluster my-cluster-name           


    # configure the URL to a proxy server to use for requests made by this client in the kubeconfig

    kubectl config set-cluster my-cluster-name --proxy-url=my-proxy-url


    # add a new user to your kubeconf that supports basic auth

    kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword


    # permanently save the namespace for all subsequent kubectl commands in that context.

    kubectl config set-context --current --namespace=ggckad-s2


    # set a context utilizing a specific username and namespace.

    kubectl config set-context gce --user=cluster-admin --namespace=foo \

      && kubectl config use-context gce

    # delete user manas

    kubectl config unset users.manas                       



    No comments