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] [-l] [-u] [-p] [-d CONTEXT...] [-a] [--kubeconfig FILE] [-h]

PARAMETERS

CONTEXT
    Name of context to switch to. Omits to show current.

-l
    List available contexts (default if no CONTEXT).

-u
    Unset current context (also kubectx -).

-p
    Switch to previously used context.

-d CONTEXT...
    Delete one or more contexts.

-a
    List all contexts, including kubectl-hidden ones.

--kubeconfig FILE
    Use specific kubeconfig file.

-h, --help
    Show help.

DESCRIPTION

kubectx is a lightweight command-line tool that simplifies managing and switching between Kubernetes contexts in your kubeconfig file. It eliminates the verbosity of native kubectl config use-context commands, offering quick listing, switching, deletion, and navigation.

Invoke without arguments to highlight the current context. Specify a context name to switch instantly. Use flags for advanced actions like unsetting (- or -u), jumping to previous (-p), deleting (-d), or listing all including hidden ones (-a). It integrates fuzzy matching for rapid selection amid many contexts and updates environment variables for smooth workflows.

Ideal for multi-cluster environments, it boosts efficiency for DevOps, developers, and cluster admins. Often paired with kubens for namespace switching. kubectx reads from ~/.kube/config or $KUBECONFIG, ensuring compatibility with standard kubectl setups.

CAVEATS

Modifies ~/.kube/config; backup advised. Requires kubectl and valid kubeconfig. No undo for deletions.

INSTALLATION

brew install kubectx (macOS) or git clone https://github.com/ahmetb/kubectx && cd kubectx && sudo cp kubectx kubens /usr/local/bin/ (Linux). Snap/APT packages available.

ALIASES

Common: alias k=kubectx, alias ks=kubectx -a for quick access.

COMPANION TOOL

kubens switches namespaces similarly: kubens <namespace>.

HISTORY

Created by Ahmet Bildik (ahmetb) in 2017. Open-source on GitHub (ahmetb/kubectx), reached v0.9.5 by 2024. Popular in Kubernetes ecosystem for productivity.

SEE ALSO

kubectl(1)

Copied to clipboard