Kubectx and Kubens
These two command line tools are very helpful when changing the current context and namespace for work, whether in a study environment or a production environment. Relying solely on kubectl for this is a tedious task.
This is where command line tools like kubectx and kubens come into play.
This project https://github.com/ahmetb/kubectx provides these very useful CLIs.
Installation
sudo git clone https://github.com/ahmetb/kubectx /opt/kubectx
sudo ln -s /opt/kubectx/kubectx /usr/local/bin/kubectx
With kubectx you don't need to use long "kubectl config" commands to switch between contexts. This tool is particularly useful for switching context between clusters in a multi-cluster environment.
Kubectx
kubectx
k3d-k3s-default
kind-kind-cluster
[email protected]
kubectx kind-kind-cluster
Switched to context "kind-kind-cluster".
# current context
kubectx -c
kind-kind-cluster
kubens
In the cluster we are already in, we can see the namespaces
kubens
default
demo
kube-node-lease
kube-public
kube-system
kubernetes-dashboard
local-path-storage
kubens demo
Context "kind-kind-cluster" modified.
Active namespace is "demo".
# From this moment on, any command I run is as if I were passing -n demo, meaning everything will go to the demo namespace
kubectl get pods
No resources found in demo namespace.
kubens default
Context "kind-kind-cluster" modified.
Active namespace is "default".
kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-david-c8644f94d-nlgch 1/1 Running 0 143m