LevelUpWith

Explore / Exam Prep for the Certified Kubernetes Application Developer (CKAD) / Lesson 2

Lesson 2: Contexts, Namespaces and a Fast Shell Setup

[Exam craft] Learn to switch kubeconfig contexts and namespaces safely, because every CKAD task names the cluster and namespace it must be solved in, and set up the alias and shell completion that save time on every task.

Free to read, no account needed. Part of Exam Prep for the Certified Kubernetes Application Developer (CKAD).

By the end of this level you'll be able to

  • Switch context with kubectl config use-context and set a default namespace with kubectl config set-context --current --namespace
  • Set up the k alias, bash completion and an export for --dry-run=client -o yaml in under a minute
  • Check the current context and namespace before each task so work never lands on the wrong cluster or namespace

Why this level matters

The CKAD is 15 to 20 hands-on tasks in 2 hours, and you need 66% to pass. Each task tells you where to do the work: a host or cluster, and usually a namespace. If you build a perfect Deployment in the wrong cluster or namespace, the grader cannot find it, so the task scores nothing. This level covers three habits that guard against that:

  1. Switch contexts and namespaces on purpose.
  2. Set up a fast shell in under a minute.
  3. Check where you are before every task.

A reminder from Level 1: the purchase includes one retake and two simulator sessions, and the certification lasts two years. The quizzes in this course help you check your understanding, but they are no substitute for typing the commands yourself. Nothing here guarantees a pass. The curriculum is CNCF's (CC BY 4.0), and this course is independent of the Linux Foundation and CNCF, which do not endorse it.

Concept: what a context is

kubectl reads a kubeconfig file, which is ~/.kube/config by default. A context is a named set of three things:

  • cluster: which API server to talk to
  • user: which credentials to use
  • namespace: the default namespace, optional (when it is empty, kubectl uses default)

The file's current-context decides which context kubectl uses. A useful way to picture it: each context is a bookmark that holds a server, a login and a starting folder. The namespace belongs to the context. It is not a global setting.

Task 1: Create a second practice cluster

To practise switching, you need two clusters. In Level 1 you built ckad. Now add another.

  1. Create it:
    kind create cluster --name ckad2
    
  2. List the contexts:
    kubectl config get-contexts
    
    CURRENT   NAME         CLUSTER      AUTHINFO     NAMESPACE
              kind-ckad    kind-ckad    kind-ckad
    *         kind-ckad2   kind-ckad2   kind-ckad2
    
    kind names each context kind-<cluster-name>. When kind creates a cluster it also makes it the current context, which is why the * is now on kind-ckad2.

Task 2: Switch context and set a namespace

  1. Switch to the first cluster:
    kubectl config use-context kind-ckad
    
    Switched to context "kind-ckad".
    
  2. Create a namespace and make it the default for this context:
    kubectl create namespace web
    kubectl config set-context --current --namespace=web
    
    Context "kind-ckad" modified.
    
  3. Check the result. The NAMESPACE column for kind-ckad should now say web:
    kubectl config get-contexts
    
  4. Switch to kind-ckad2 and run kubectl get pods. kubectl looks in default, because that context still has no namespace set. The namespace stays with the context it was set on.

Two points to remember:

  • set-context --current --namespace does not check that the namespace exists. If you mistype it, kubectl just says No resources found and gives no error.
  • For a single command you can override the namespace with -n <namespace> instead.

Task 3: Set up a fast shell in under a minute

The official exam environment already has kubectl with the k alias and Bash completion. Your kind machine does not, so add them yourself and practise with the same setup you will have on exam day. Run these lines once:

echo 'source <(kubectl completion bash)' >> ~/.bashrc
echo 'alias k=kubectl' >> ~/.bashrc
echo 'complete -o default -F __start_kubectl k' >> ~/.bashrc
echo 'export do="--dry-run=client -o yaml"' >> ~/.bashrc
source ~/.bashrc

What each line does:

  • Line 1 loads Tab completion for kubectl.
  • Line 2 creates the k alias.
  • Line 3 gives the alias the same completion. Without it, k get po<Tab> does not complete, because Bash attaches completion to command names and k is a separate name.
  • Line 4 stores the dry-run flags in a variable. k run web --image=nginx $do prints a manifest and creates nothing. Level 3 covers dry runs properly.

Test it: type k get no and press Tab. It should complete to k get nodes.

In the exam, $do and any aliases you add only last for the shell session or host you set them up on. If a task moves you to another host, re-run the export.

Task 4: The pre-task check

Do this before you touch any task:

  1. Run the switch command the task gives you. Copy and paste it so you avoid typos.
  2. Confirm the context:
    k config current-context
    
  3. Confirm the namespace:
    k config view --minify -o jsonpath='{..namespace}'; echo
    
    If this prints nothing, you are in default.
  4. Choose how to target the namespace. Either set it on the context with set-context --current --namespace, or add -n <namespace> to every command. Do not mix the two methods within one task.
  5. Check your work afterwards with k get <resource> -n <namespace>.

Timed practice (3 minutes)

Start with kind-ckad2 as the current context. Make kind-ckad the current context with web as its default namespace, then prove both without opening the kubeconfig file.

Solution:

k config use-context kind-ckad
k config set-context --current --namespace=web
k config current-context                          # kind-ckad
k config view --minify -o jsonpath='{..namespace}'; echo   # web

What the exam asks about this

The exam does not test this skill as a separate task, but every task depends on it. Each task tells you which host or context and which namespace to use. Work in the wrong place and the task scores zero, however good the work is. Use the switch command the task gives you, check where you are, and use k and Tab completion to save seconds on every command.

Key takeaways

  • A context is a cluster, a user and an optional namespace. use-context changes which one kubectl uses.
  • kubectl config set-context --current --namespace=<ns> sets the default namespace for the current context only.
  • The exam already has k and completion. At home, set them up with complete -o default -F __start_kubectl k and export do="--dry-run=client -o yaml".
  • Before every task: switch, check the context, check the namespace. After every task: check your work in the correct namespace.

A 6-question knowledge check follows this lesson in the app, free and without an account.

Keep going: levels 3 and 4 are free

Create a free account to open the next two levels, try the AI tutor, save your progress and pick up where you left off. No card needed.

Next: Level 3, Imperative Commands, Dry Runs and kubectl explain.

Create a free account →See the whole course

More like this