Understanding the concept of context and how to use it with kubectl
Ok, let's start from the beginning.
To understand what a context is, we first need to understand that many software tools use a configuration file to connect to the Kubernetes API. This configuration file contains three main pieces of information:
- Clusters: The clusters section contains all the connection details to the different Kubernetes API servers available.
- Users: Here we can define all the authentication data, like client-certificate and client-key
- Context: The context ("The reason for this tutorial") is where we define what cluster and user we are goin to use to create the connection and optionally we can also define the namespace we want interact with.
The kubeconfig file (usually located at ~/.kube/config) typically includes:
apiVersion: v1
kind: Config
clusters:
- name: dev-cluster
cluster:
server: https://dev.k8s.example.com
certificate-authority: /path/to/ca.crt
users:
- name: dev-user
user:
client-certificate: /path/to/dev.crt
client-key: /path/to/dev.key
contexts:
- name: dev-context
context:
cluster: dev-cluster
user: dev-user
namespace: development
current-context: dev-context
Why Are Contexts Structured This Way?
By separating clusters, users, and contexts, the kubeconfig file lets you:
- Reuse components (e.g., connect the same cluster with different users)
- Easily create a new context without duplicating config
- Manage Kubernetes workflows in a modular way
This separation gives you flexibility when scripting, automating, or managing large multi-cluster environments.
Essential kubectl Context Commands
Here we are going to list everything you need to know to manage contexts with kubectl:
1. List all available contexts
kubectl config get-contexts
Lists all contexts in your kubeconfig. The current context is marked with *.
2. View the current context
kubectl config current-context
Displays the name of the active context.
3. Switch to a different context
kubectl config use-context <context-name>
Changes the active context to the one specified.
4. Set the default namespace for the current context
kubectl config set-context --current --namespace=<namespace>
Sets a default namespace for your active context. This helps avoid needing --namespace in every command.
5. Creating or Modifying a Context
You can create or update a context like this:
kubectl config set-context <context-name> \
--cluster=<cluster-name> \
--user=<user-name> \
--namespace=<namespace>
Use this to script environment setups or define custom workflows.
Things to Remember:
- Use clear and descriptive names: e.g., dev-cluster, prod-eks, test-gke
- Install tools like kubectx for quick switching
- Always double-check your current context before running critical commands
- Back up your kubeconfig file, especially when working with production
Conclusion
Mastering kubectl context management allows you work securely and efficiently across environments. Whether you’re switching between cloud providers or staging and production clusters, contexts give you control and flexibility.