Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I toggle between kubernetes context of various deployed apps?

When I deploy each app using kubernetes/cluster/kube-up.sh over aws I set context using :

CONTEXT=$(kubectl config view | grep current-context | awk '{print $2}')

kubectl config set-context $CONTEXT --namespace=${PROJECT_ID}

I do this for multiple apps and each deploys fine. However I then need to be able to toggle between kubernetes context to interact with an arbitrary deployed app (view logs/ do a kubectl exec )

Here is how to show all my contexts

kubectl config view --output=json

{
    "kind": "Config",
    "apiVersion": "v1",
    "preferences": {},
    "clusters": [
        {
            "name": "aws_kubernetes",
            "cluster": {
                "server": "https://52.87.88.888",
                "certificate-authority-data": "REDACTED"
            }
        },
        {
            "name": "gke_primacyofdirectexperience_us-east1-b_loudhttpscluster",
            "cluster": {
                "server": "https://104.196.888.888",
                "certificate-authority-data": "REDACTED"
            }
        }
    ],
    "users": [
        {
            "name": "aws_kubernetes",
            "user": {
                "client-certificate-data": "REDACTED",
                "client-key-data": "REDACTED",
                "token": "taklamakan"
            }
        },
        {
            "name": "aws_kubernetes-basic-auth",
            "user": {
                "username": "admin",
                "password": "retrogradewaif"
            }
        },
        {
            "name": "gke_primacyofdirectexperience_us-east1-b_loudhttpscluster",
            "user": {
                "client-certificate-data": "REDACTED",
                "client-key-data": "REDACTED",
                "username": "admin",
                "password": "emptyadjacentpossible"
            }
        }
    ],
    "contexts": [
        {
            "name": "aws_kubernetes",
            "context": {
                "cluster": "aws_kubernetes",
                "user": "aws_kubernetes",
                "namespace": "ruptureofthemundaneplane"
            }
        },
        {
            "name": "gke_primacyofdirectexperience_us-east1-b_loudhttpscluster",
            "context": {
                "cluster": "gke_primacyofdirectexperience_us-east1-b_loudhttpscluster",
                "user": "gke_primacyofdirectexperience_us-east1-b_loudhttpscluster",
                "namespace": "primacyofdirectexperience"
            }
        }
    ],
    "current-context": "aws_kubernetes"
}

you can see in above I have deployed two apps ... when I try the obvious to choose my kubernetes context

kubectl config set-context  gke_primacyofdirectexperience_us-east1-b_loudhttpscluster  --namespace=${PROJECT_ID}

... outputs
context "gke_primacyofdirectexperience_us-east1-b_loudhttpscluster" set.


kubectl config set-cluster "gke_primacyofdirectexperience_us-east1-b_loudhttpscluster"

... outputs
cluster "gke_primacyofdirectexperience_us-east1-b_loudhttpscluster" set.

it then just hangs when I issue commands like

kubectl describe pods --namespace=primacyofdirectexperience 

perhaps I am missing the command to also set user since in above json each deployed app gets its own user name ???

UPDATE

kubectl config use-context gke_primacyofdirectexperience_us-east1-b_loudhttpscluster

... outputs
switched to context "gke_primacyofdirectexperience_us-east1-b_loudhttpscluster".

however now when I issue any kubectl command ... for example

kubectl get pods 

.... outputs
Unable to connect to the server: x509: certificate signed by unknown authority

which is an error I have never seen before ... no doubt due to the toggle issue

Even with above error message /kubernetes/cluster/kube-down.sh was able to teardown the cluster so there is hope toggling will work !

like image 711
Scott Stensland Avatar asked Aug 26 '16 15:08

Scott Stensland


People also ask

How do I switch between contexts in kubectl?

You can quickly switch between clusters by using the kubectl config use-context command. To use that command, though, you need to configure contexts in your kubeconfig. This can be done with the kubectl config set-context command.

How do I get contexts in kubectl?

Kubectl current-context command shows the current context of kubectl. When you enter the 'kubectl config current-context' in the virtual machine environment, the following output will be displayed. The 'kubectl config use-context cluster-name' command is used to set the default context to the given cluster name.

How do I switch between namespaces in Kubernetes?

Working with Kubernetes Namespaces Because this can be time-consuming, the default namespace can be modified by using the kubectl config command to set the namespace in the cluster context. To switch from the default namespace to 'K21,' for example, type: kubectl config set-context –current –namespace=K21.


2 Answers

To switch between contexts use use-context:

kubectl config use-context gke_primacyofdirectexperience_us-east1-b_loudhttpscluster

Any kubectl commands applied now will be applied to that cluster (using the primacyofdirectexperience namespace, since you set that as the default for the cluster).

kubectl get pods

Will now get all pods gke_primacyofdirectexperience_us-east1-b_loudhttpscluster on the primacyofdirectexperience namespace. To use a different namespace, you can apply the namspace flag:

kubectl get pods --namespace=someothernamespace

To switch contexts again, just run use-context again:

kubectl config use-context aws_kubernetes

Now,

kubectl get pods

will run on the aws_kubernetes cluster, using the default namespace.

You can always see which context kubectl is currently using by running:

kubectl config current-context
like image 139
Pixel Elephant Avatar answered Nov 15 '22 08:11

Pixel Elephant


To extend the answer (provided by @Pixel Elephant), which explains how to "use-context" to achieve switching between multiple configurations, it is worth to mention that sometimes it is handy to switch between two contexts that differ only by the namespace. You can achieve this by editing your ~/.kube/config to copy the context and change only the namespace.

Alternatively, you can write a short script that will edit the namespace with kubectl set-context $(kubectl config current-context) --namespace <nsname>.

These answers should answer your question completely. However if you are looking for handy tools to simplify the daily use of those commands even more, then users usually try to automate such context switch with some cli script.

I got inspired by kubectx and kswitch scripts, which I can recommend for most cases. They are helping with solving the switching task, but were breaking for me on some bigger or less standard configurations of ~/.kube/config. So I created a sys-exec invocation wrapper and a short-hand around kubectl.

If you call k without params you would see an intercepted prompt to switch context.

Switch kubectl to a different context/cluster/namespace.
Found following options to select from:
 >>> context: [1] franz
 >>> context: [2] gke_foo_us-central1-a_live-v1
 >>> context: [3] minikube
 --> new num [?/q]:

Further, k continues to act as a short-hand. The following is equivalent:

kubectl get pods --all-namespaces
k get pods -A
k p -A
like image 39
Yauhen Yakimovich Avatar answered Nov 15 '22 07:11

Yauhen Yakimovich