Tips and Tricks: K8S Context Commands
Thursday, Sep 23, 2021 16:00 · 437 words · 3 minutes read
Tips and Tricks
What is a K8S context?
Kubernetes context allows engineers to configure access to multiple clusters by using configuration files. It is a set of parameters, which describes K8S cluster, namespace and user, basically the information needed to access the K8S cluster and its resources. This information is stored on the filesystem within ~/.kube/config or ~/.kube/KUBECONFIG (by default), or defined across multiple configuration files.
...
contexts:
- context:
cluster: dev-k8s
namespace: apps
user: clusterUser_dev-k8s
name: clusterUser_dev-k8s
- context:
cluster: minikube
extensions:
- extension:
provider: minikube.sigs.k8s.io
version: v1.21.0
name: context_info
namespace: default
user: minikube
name: minikube
current-context: dev-k8s
...
K8S context CLI commands
Display the merged K8S settings
kubectl config view
or
cat ~/.kube/config
Output:
...
contexts:
- context:
cluster: dev-k8s
namespace: apps
user: clusterUser_dev-k8s
name: clusterUser_dev-k8s
- context:
cluster: minikube
extensions:
- extension:
provider: minikube.sigs.k8s.io
version: v1.21.0
name: context_info
namespace: default
user: minikube
name: minikube
current-context: dev-k8s
...
Display the current K8S context
kubectl config current-context
Output:
dev-k8s
List all K8S contexts
kubectl config get-contexts
Output:
* dev-k8s dev-k8s clusterUser_dev-k8s apps
minikube minikube minikube default
Switch to a different K8S context
kubectl config use-context minikube
Output:
Switched to context "minikube".
Double check the current context
kubectl config get-contexts
Output:
dev-k8s dev-k8s clusterUser_dev-k8s apps
* minikube minikube minikube default
Additional information
You could get more helpful information about the commands listed above or have a look at what else is available to do with the context by using:
kubectl config use-context --help
Output:
Sets the current-context in a kubeconfig file
Aliases:
use-context, use
Examples:
# Use the context for the minikube cluster
kubectl config use-context minikube
Usage:
kubectl config use-context CONTEXT_NAME [options]
More kubectl config commands
kubectl config --help
Output:
Modify kubeconfig files using subcommands like "kubectl config set current-context my-context"
...
Available Commands:
current-context Displays the current-context
delete-cluster Delete the specified cluster from the kubeconfig
delete-context Delete the specified context from the kubeconfig
get-clusters Display clusters defined in the kubeconfig
get-contexts Describe one or many contexts
rename-context Renames a context from the kubeconfig file.
set Sets an individual value in a kubeconfig file
set-cluster Sets a cluster entry in kubeconfig
set-context Sets a context entry in kubeconfig
set-credentials Sets a user entry in kubeconfig
unset Unsets an individual value in a kubeconfig file
use-context Sets the current-context in a kubeconfig file
view Display merged kubeconfig settings or a specified kubeconfig file
Usage:
kubectl config SUBCOMMAND [options]
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).