Demystifying Kubernetes, Context Switching, and k9s: A Comprehensive Guide (Part 1)

Rehmat Sayany
3 min readDec 8, 2023

Introduction

In the ever-evolving landscape of container orchestration, Kubernetes has emerged as the de facto standard for managing containerized applications. Kubernetes provides a robust platform for automating the deployment, scaling, and management of containerized applications. To harness the full power of Kubernetes, users often need to navigate multiple environments and seamlessly switch between them. This is where context switching and tools like kubectl and k9s come into play.

Understanding Kubernetes

Kubernetes, often abbreviated as K8s, is an open-source container orchestration platform designed to automate the deployment, scaling, and management of containerized applications. With Kubernetes, users can abstract away the complexities of managing containers, allowing them to focus on building and deploying scalable applications.

Key Kubernetes Concepts:

  • Pods: The smallest deployable units in Kubernetes, representing one or more containers.
  • Nodes: The physical or virtual machines that run containers.
  • Services: Network abstraction to expose a set of pods as a single, stable endpoint.
  • Contexts: Logical pointers to different Kubernetes clusters, enabling users to manage multiple environments effortlessly.

Context Switching in Kubernetes

Context switching in Kubernetes involves changing the active context, which essentially means changing the Kubernetes cluster or environment that kubectl commands are interacting with. This is crucial when dealing with different stages of your application's lifecycle, such as development, testing, staging, and production.

Understanding kubectl

kubectl is the official command-line tool for interacting with Kubernetes clusters. It acts as a Swiss army knife, providing a wide range of commands for managing various aspects of your Kubernetes resources. One essential aspect of kubectl is its ability to work with different clusters and switch between them using contexts.

Common kubectl Commands:

  • kubectl get pods: List all pods in the current context.
  • kubectl apply -f <yaml-file>: Apply configuration changes to a cluster.
  • kubectl logs <pod-name>: View the logs of a specific pod.

Switching Contexts with kubectl

Switching contexts with kubectl is essential for managing different Kubernetes environments. Here's a quick guide:

Viewing Available Contexts

Before switching contexts, it’s helpful to know what contexts are available on your system. Execute the following command:

kubectl config get-contexts

This command lists all the available contexts, indicating the current active context with an asterisk (*).

get contexts

Switching Contexts

To switch to a different context, use the following command:

kubectl config use-context <context-name>

Replace <context-name> with the name of the context you want to switch to. For example, to switch to the "desktop" context:

kubectl config use-context docker-desktop

Verifying the Active Context

After switching the context, you can confirm the change by running:

kubectl config current-context
current context

This command displays the name of the currently active context.

In the next part of this series, we will delve deeper into practical examples and explore how to leverage both kubectl and k9s for more advanced Kubernetes management tasks. Stay tuned for an in-depth guide on enhancing your Kubernetes experience with these powerful tools!

--

--

Rehmat Sayany

Full Stack developer @westwing passionate about NodeJS, TypeScript, React JS and AWS.