How-To Guide

Manage Contexts with ktx

Goal: Quickly switch between kubectl contexts using ktx.

Time: 2 minutes to learn

What is ktx?

ktx is an interactive kubectl context switcher. Instead of typing long context names with kubectl config use-context, you select from a menu.

Why use it:

  • Faster context switching (interactive menu vs typing full names)

  • Visual confirmation of current context

  • No need to remember exact context names

GitHub: https://github.com/ketches/ktx

Installation

curl -sSL https://github.com/ketches/ktx/raw/master/install.sh | sh

Verify installation:

ktx --version

Essential Commands

List All Contexts (Interactive Switch)

ktx

What happens:

  • Shows all available contexts

  • Current context highlighted

  • Use arrow keys to select

  • Press Enter to switch

Output example:

❯ kup6s-admin@kup6s (current)
  dev-cluster
  staging-cluster
  prod-cluster

Direct Switch (No Menu)

ktx <context-name>

Example:

ktx kup6s-admin@kup6s

Switches immediately without interactive menu.

Switch to Previous Context

ktx -

Like cd - - toggles between current and previous context.

Workflow:

ktx kup6s-admin@kup6s    # Switch to KUP6S
# ...do work...
ktx -                     # Back to previous cluster

Show Current Context

ktx --current

Displays the active context name.

Common Workflows

Daily Work: Switch to KUP6S

ktx                       # Open menu
# Select "kup6s-admin@kup6s" with arrows
# Press Enter

Or directly:

ktx kup6s

Note: ktx supports partial matching, so ktx kup6s finds kup6s-admin@kup6s.

Multi-Cluster Management

Working across dev/staging/prod:

# Check current context
ktx --current

# Switch to staging
ktx staging

# Do some work
kubectl get pods

# Switch to prod
ktx prod

# Back to staging
ktx -

Verify Before Destructive Operations

Always verify context before dangerous operations:

ktx --current             # Confirm you're in the right cluster
kubectl delete ...        # Now safe to proceed

Or use kubectl’s built-in check:

kubectl config current-context

Tips

Shell Alias

Add to your .bashrc or .zshrc:

alias k='kubectl'
alias kc='ktx'            # Quick context switch
alias kcc='ktx --current' # Check current context

Usage:

kc              # Interactive context menu
kcc             # Show current context

Use with kubectl

ktx changes your kubectl context. After switching with ktx, all kubectl commands use the new context:

ktx kup6s                 # Switch context
kubectl get nodes         # Runs against KUP6S cluster

Context Names

List all context names (for scripting):

kubectl config get-contexts -o name

Namespace Switching

ktx only switches contexts, not namespaces. For namespaces, use:

kubectl config set-context --current --namespace=my-namespace

Or use kubens (companion tool):

kubens my-namespace

Comparison with Alternatives

Tool

Purpose

Interactive

ktx

Context switching

Yes ✅

kubectx

Context switching

Yes (with fzf)

kubectl config use-context

Context switching

No

kubens

Namespace switching

Yes

Why ktx:

  • Lightweight (shell script)

  • Simple interactive menu

  • No external dependencies

Troubleshooting

Context Not Found

Error: context "xyz" not found

Solutions:

  1. List available contexts:

    ktx
    
  2. Check kubeconfig file:

    kubectl config view
    
  3. Verify KUBECONFIG path:

    echo $KUBECONFIG
    # Should be: /path/to/kup6s_kubeconfig.yaml (or merged config)
    

Permission Denied

Error: error: You must be logged in to the server (Unauthorized)

Solutions:

  1. Check kubeconfig credentials:

    kubectl cluster-info
    
  2. Verify kubeconfig file exists and is readable:

    ls -la $KUBECONFIG
    
  3. For KUP6S specifically:

    export KUBECONFIG=/path/to/kube-hetzner/kup6s_kubeconfig.yaml
    ktx kup6s
    

ktx Command Not Found

Error: ktx: command not found

Solutions:

  1. Reinstall:

    curl -sSL https://github.com/ketches/ktx/raw/master/install.sh | sh
    
  2. Verify installation location:

    which ktx
    # Should be in ~/.ktx/ or /usr/local/bin/
    
  3. Add to PATH (if needed):

    export PATH="$HOME/.ktx:$PATH"
    

Multiple Kubeconfig Files

If you have separate kubeconfig files for different clusters:

Option 1: Merge Configs

KUBECONFIG=~/.kube/config:~/kup6s_kubeconfig.yaml kubectl config view --flatten > ~/.kube/config-merged
export KUBECONFIG=~/.kube/config-merged

Now ktx sees all contexts.

Option 2: Environment Variable

export KUBECONFIG=~/kup6s_kubeconfig.yaml:~/.kube/dev-config:~/.kube/prod-config
ktx  # Shows contexts from all files

Option 3: Switch Per Session

# Session 1: KUP6S
export KUBECONFIG=~/kup6s_kubeconfig.yaml
ktx kup6s

# Session 2: Other cluster
export KUBECONFIG=~/.kube/other-config
ktx other

Quick Reference Card

# Interactive context menu
ktx

# Switch directly
ktx <context-name>

# Previous context
ktx -

# Show current
ktx --current

# Verify before dangerous ops
ktx --current && kubectl delete ...

Further Reading