4
votes

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 !

2

2 Answers

6
votes

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
1
votes

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