LinuxCommandLibrary

kubectx

Switch between Kubernetes contexts

TLDR

List the contexts

$ kubectx
copy

Switch to a named context
$ kubectx [name]
copy

Switch to the previous context
$ kubectx -
copy

Rename a named context
$ kubectx [alias]=[name]
copy

Show the current named context
$ kubectx [[-c|--current]]
copy

Delete a named context
$ kubectx -d [name]
copy

SYNOPSIS

kubectx [CONTEXT_NAME | -]

PARAMETERS

CONTEXT_NAME
    The name of the Kubernetes context to switch to. If the name starts with '-', it will switch to the previously used context.

-
    Switch to the previously used context.

--help
    Display help information.

DESCRIPTION

kubectx is a command-line tool designed to simplify switching between different Kubernetes contexts. In Kubernetes, a context encapsulates access parameters for a cluster. Managing multiple clusters can become cumbersome when using the standard `kubectl` command alone, as you constantly need to modify the `kubeconfig` file or specify context flags.

kubectx streamlines this process by allowing you to quickly switch between contexts using short, easily memorable names. It reads and modifies your `kubeconfig` file to change the current context. This eliminates the need to type long command lines or manually edit the configuration file.

Beyond switching contexts, kubectx offers features such as renaming contexts, deleting contexts, and integrating with command-line completion for improved usability. It significantly enhances the workflow for developers and administrators who manage multiple Kubernetes clusters or namespaces regularly.

CAVEATS

kubectx directly modifies your kubeconfig file. Incorrect usage can potentially lead to configuration errors or unintended access. Always back up your kubeconfig file before making significant changes. Ensure you understand the implications of switching contexts, as it affects which cluster your kubectl commands will target.

CONFIGURATION FILE

kubectx reads and modifies the kubeconfig file, which is typically located at ~/.kube/config. This file stores cluster, user, and context information necessary to access Kubernetes clusters.

COMMAND-LINE COMPLETION

kubectx often comes with command-line completion scripts for shells like Bash, Zsh, and Fish. These scripts enable tab-completion for context names, making it even easier to switch between contexts quickly.

INTEGRATION WITH KUBENS

kubectx often used with kubens. kubens let you switch between Kubernetes namespaces quickly. Namespace is default target for kubernetes resources inside of the Cluster

HISTORY

kubectx was developed to address the common pain point of managing multiple Kubernetes contexts with the standard kubectl tool. The tool has gained popularity among Kubernetes users due to its simplicity and efficiency in switching between contexts. Originally created by Ahmet Alp Balkan, the tool quickly became a standard tool for Kubernetes engineers. The project is actively maintained and has become part of larger tool ecosystems designed to streamline Kubernetes management.

SEE ALSO

kubectl(1), kubeconfig(5)

Copied to clipboard