kubernetes: change the current/default context via kubectl command

12/7/2021

I am doing an exercise from KodeKoud which provide the CKAD certification training.

The exercise has a my-kube-config.yml file located under root/. The file content is below:

(I ommited some unrelated parts)

apiVersion: v1
kind: Config

clusters:
- name: production
  cluster:
    certificate-authority: /etc/kubernetes/pki/ca.crt
    server: https://controlplane:6443

- name: development
  cluster:
    certificate-authority: /etc/kubernetes/pki/ca.crt
    server: https://controlplane:6443

- name: test-cluster-1
  cluster:
    certificate-authority: /etc/kubernetes/pki/ca.crt
    server: https://controlplane:6443

contexts:

- name: test-user@production
  context:
    cluster: production
    user: test-user

- name: research
  context:
    cluster: test-cluster-1
    user: dev-user

users:
- name: test-user
  user:
    client-certificate: /etc/kubernetes/pki/users/test-user/test-user.crt
    client-key: /etc/kubernetes/pki/users/test-user/test-user.key
- name: dev-user
  user:
    client-certificate: /etc/kubernetes/pki/users/dev-user/developer-user.crt
    client-key: /etc/kubernetes/pki/users/dev-user/dev-user.key


current-context: test-user@development

The exercise asking me to:

use the dev-user to access test-cluster-1. Set the current context to the right one so I can do that.

Since I see in the config file, there is a context named research which meets the requirement, so I run the following command to change the current context to the required one:

kubectl config use-context research

but the console gives me error: error: no context exists with the name: "research".

Ok, I guessed maybe the name with value research is not acceptable, maybe I have to follow the convention of <user-name>@<cluster-name>? I am not sure , but I then tried the following:

  1. I modified the name from research to dev-user@test-cluster-1, so that context part becomes:
- name: dev-user@test-cluster-1
  context:
    cluster: test-cluster-1
    user: dev-user
  1. after that I run command: kubectl config use-context dev-user@test-cluster-1, but I get error:
error: no context exists with the name: "dev-user@test-cluster-1"

Why? Based on the course material that is the way to chagne the default/current context. Is the course out-dated that I am using a deprecated one? What is the problem?

-- user842225
kubernetes

2 Answers

12/8/2021

To able to change context, you have to edit $HOME/.kube/config file with your config data and merge with default one. I've tried to replicate your config file and it was possible to change the context, however your config file looks very strange.

See the lines from my console for your reference:

bazhikov@cloudshell:~ (nb-project-326714)$ kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority: /etc/kubernetes/pki/ca.crt
    server: https://controlplane:6443
  name: development
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://35.246.22.167
  name: gke_nb-project-326714_europe-west2_cluster-west2
...
...
...
- name: test-user
  user:
    client-certificate: /etc/kubernetes/pki/users/test-user/test-user.crt
    client-key: /etc/kubernetes/pki/users/test-user/test-user.key
bazhikov@cloudshell:~ (nb-project-326714)$ kubectl config use-context research
Switched to context "research".

Copy your default config file prior editing if you don't want to ruin your cluster config :)

-- Bazhikov
Source: StackOverflow

4/8/2022

Your initial idea was correct. You would need to change the context to research which can be done using

kubectl config use-context research

But the command would not be applied to the correct config in this instance. You can see the difference by checking the current-context with and without a kubeconfig directed to the my-kube-config file.

kubectl config current-context

kubernetes-admin@kubernetes

kubectl config --kubeconfig=/root/my-kube-config current-context

test-user@development

So run the use-context command with the correct kubeconfig

kubectl config --kubeconfig=/root/my-kube-config use-context research

-- Grabot
Source: StackOverflow