-->

DEVOPSZONES

  • Recent blogs

    Power tools for kubernetes : kubectx + kubens

     

    What are kubectx and kubens?

    kubectx is a tool to switch between contexts (clusters) on kubectl faster.
    kubens is a tool to switch between Kubernetes namespaces (and configure them for kubectl) easily.


    Installation

    Stable versions of kubectx and kubens are small bash scripts that you can find in this repository. Starting with v0.9.0, kubectx and kubens are now rewritten in Go


    Repository: https://github.com/ahmetb/kubectx


    Kubectl Plugins (macOS and Linux)

    You can install and use the Krew kubectl plugin manager to get kubectx and kubens.


    kubectl krew install ctx
    kubectl krew install ns

    After installing, the tools will be available as kubectl ctx and kubectl ns.


    Homebrew (macOS and Linux)

    If you use Homebrew you can install like this:

    brew install kubectx

    This command will set up bash/zsh/fish completion scripts automatically. Make sure you configure your shell to load completions for installed Homebrew formulas.


    Windows Installation (using Chocolatey)

    Available as packages on Chocolatey

    choco install kubens kubectx

    Windows Installation (using Scoop)

    Available as packages on Scoop

    scoop bucket add main
    scoop install main/kubens main/kubectx

    Manual Installation (macOS and Linux)

    Since kubectx and kubens are written in Bash, you should be able to install them to any POSIX environment that has Bash installed.

    • Download the kubectx, and kubens scripts.
    • Either:
      • save them all to somewhere in your PATH,
      • or save them to a directory, then create symlinks to kubectx/kubens from somewhere in your PATH, like /usr/local/bin
    • Make kubectx and kubens executable (chmod +x ...)

    Example installation steps:

    sudo git clone https://github.com/ahmetb/kubectx /opt/kubectx
    sudo ln -s /opt/kubectx/kubectx /usr/local/bin/kubectx
    sudo ln -s /opt/kubectx/kubens /usr/local/bin/kubens


    Examples

    # switch to another cluster that's in kubeconfig
    $ kubectx minikube
    Switched to context "minikube".
    
    # switch back to previous cluster
    $ kubectx -
    Switched to context "oregon".
    
    # rename context
    $ kubectx dublin=gke_ahmetb_europe-west1-b_dublin
    Context "gke_ahmetb_europe-west1-b_dublin" renamed to "dublin".
    
    # change the active namespace on kubectl
    $ kubens kube-system
    Context "test" set.
    Active namespace is "kube-system".
    
    # go back to the previous namespace
    $ kubens -
    Context "test" set.
    Active namespace is "default".
    
    # change the active namespace even if it doesn't exist
    $ kubens not-found-namespace --force
    Context "test" set.
    Active namespace is "not-found-namespace".
    ---
    $ kubens not-found-namespace -f
    Context "test" set.
    Active namespace is "not-found-namespace".



    No comments