Kontext is a command-line tool for efficiently managing Kubernetes contexts. It simplifies operations on kubectl configuration files (typically ~/.kube/config) by enabling users to add, list, merge, delete, and clean Kubernetes contexts. Kontext supports sub-cluster scanning (e.g., Alauda clusters) and wildcard operations, making multi-cluster management seamless.
- Add Contexts: Quickly add Kubernetes contexts with name, server address, and token, including sub-cluster scanning.
- List Contexts: Display all current contexts and detect orphaned resources.
- Delete Contexts: Remove contexts by exact name or wildcard pattern, automatically cleaning orphaned clusters and users.
- Clean Contexts: Validate and remove invalid or unreachable contexts, along with orphaned clusters and users.
- Merge Configs: Merge external kubeconfig files into the current configuration, with optional name prefixes and sub-cluster scanning.
- SSH into Nodes: SSH directly into a node of the current Kubernetes context, with fzf-powered interactive selection and dynamic Tab completion for node IPs.
- Backup Management: Automatically create backups before modifying configurations (default: retain 5 backups in
~/.kube).
-
Download the appropriate binary from the releases page, extract it, and move it to your
$PATH:sudo mv kontext /usr/local/bin/
-
Verify the installation:
kontext -h
To enable command completion (e.g., for Bash):
-
Generate the completion script:
kontext completion bash > ~/.kontext-completion.bash
-
Add to your
~/.bashrcor~/.bash_profile:source ~/.kontext-completion.bash
-
Reload your terminal:
source ~/.bashrc
For Zsh or other shells, run kontext completion <shell> -h for details.
The following examples demonstrate Kontext’s core functionality using the ~/.kube/config file.
Add a context named myenv with Alauda sub-cluster scanning:
kontext add --name myenv --server "https://192.168.138.58/kubernetes/global" --token "<your-token>" --scan alaudaOutput:
[kubeconfig.AddContext] Adding contexts...
✓ Added context: myenv (https://192.168.138.58/kubernetes/global)
✓ Added context: myenv-business-1 (https://192.168.138.58/kubernetes/business-1)
kubeconfig.AddContext Summary:
✓ Added contexts: 2
✗ Failed contexts: 0
==================================================
Merge an external kubeconfig file with sub-cluster scanning:
kontext merge --path Downloads/asdf.yaml --scan alaudaOutput:
[kubeconfig.MergeContext] Merging contexts...
✓ Added context: asdf-global (https://192.168.138.58/kubernetes/global)
✓ Added context: asdf-global-business-1 (https://192.168.138.58/kubernetes/business-1)
kubeconfig.MergeContext Summary:
✓ Added contexts: 2
✗ Skipped certificate-based contexts: 0
✗ Failed contexts: 0
==================================================
View all current contexts:
kontext listOutput:
===== Active Contexts (4) =====
● asdf-global
├─ Cluster: asdf-global (https://192.168.138.58/kubernetes/global)
└─ User: asdf-global
● asdf-global-business-1
├─ Cluster: asdf-global-business-1 (https://192.168.138.58/kubernetes/business-1)
└─ User: asdf-global-business-1
● myenv
├─ Cluster: myenv (https://192.168.138.58/kubernetes/global)
└─ User: myenv
● myenv-business-1
├─ Cluster: myenv-business-1 (https://192.168.138.58/kubernetes/business-1)
└─ User: myenv-business-1
===== No Orphaned Resources =====
All resources are properly referenced.
Delete contexts starting with asdf using a wildcard:
kontext delete --name "asdf*"Output:
[kubeconfig.BackupKubeConfig] Created backup: /home/jing2uo/.kube/config.backup-20250613-104013
[kubeconfig.BackupKubeConfig] Removed old backup: /home/jing2uo/.kube/config.backup-20250613-102238
[kubeconfig.DeleteContext] Deleting contexts...
✓ Removed context: asdf-global
✓ Removed context: asdf-global-business-1
✓ Cleared current context setting
✓ Removed orphaned cluster: asdf-global
✓ Removed orphaned cluster: asdf-global-business-1
✓ Removed orphaned user: asdf-global
✓ Removed orphaned user: asdf-global-business-1
kubeconfig.DeleteContext Summary:
✓ Removed contexts: 2
✓ Current context reset
✓ Removed clusters: 2
✓ Removed users: 2
✓ Backup saved at: /home/jing2uo/.kube/config.backup-20250613-104013
==================================================
Without specifying an IP, fzf launches for interactive node selection:
kontext sshfzf interface (format: IP [role] nodename):
192.168.138.11 [master] master-node-1
192.168.138.21 [node] worker-node-1
192.168.138.22 [node] worker-node-2
Or specify an IP directly:
kontext ssh 192.168.138.11With shell completion enabled,
kontext ssh <TAB>dynamically lists all node IPs and roles from the current context.
Validate and remove invalid or unreachable contexts:
kontext cleanOutput (assuming a manually invalidated context):
[kubeconfig.BackupKubeConfig] Created backup: /home/jing2uo/.kube/config.backup-20250613-104034
[kubeconfig.BackupKubeConfig] Removed old backup: /home/jing2uo/.kube/config.backup-20250613-103146
[kubeconfig.CleanContextCmd] Cleaning contexts...
✓ Removed invalid context: myenv-business-1
✓ Removed orphaned cluster: myenv-business-1
✓ Removed orphaned user: myenv-business-1
kubeconfig.CleanContextCmd Summary:
✓ Removed contexts: 1
✓ Removed clusters: 1
✓ Removed users: 1
✓ Backup saved at: /home/jing2uo/.kube/config.backup-20250613-104034
==================================================
Add a new Kubernetes context.
kontext add --name <name> --server <server> --token <token> [--scan <type>]
--name: Context, cluster, and user name (required).--server: Kubernetes API server address (required).--token: Authentication token (required).--scan: Sub-cluster scan type (e.g.,alauda).
Merge an external kubeconfig file.
kontext merge --path <path> [--name <prefix>] [--scan <type>]
--path: Path to kubeconfig file (required).--name: Context name prefix (optional).--scan: Sub-cluster scan type (e.g.,alauda).
List all contexts and resource status.
kontext list
Delete contexts, supporting wildcards.
kontext delete --name <name>
--name: Context name to delete, supports wildcards (e.g.,name*) (required).
Remove invalid or unreachable contexts and orphaned resources.
kontext clean
SSH into a node of the current Kubernetes context.
kontext ssh [ip]
ip: Target node IP (optional). If omitted, fzf is launched for interactive selection. InternalIP is preferred over ExternalIP.- Supports Tab completion, dynamically listing all node IPs and roles from the current context.
- Requires
sshin PATH; interactive selection additionally requiresfzf.
- Backups are stored as
~/.kube/config.backup-<timestamp>(e.g.,config.backup-20250613-104034). - Retains the 5 most recent backups, automatically deleting older ones.
- Created only for
deleteandcleancommands.
- Go 1.18 or higher (for compilation).
- Kubernetes client-go library (included in project dependencies).
kubectl(for context validation).
Enhance your experience with:
Contributions are welcome! Follow these steps:
- Fork the repository.
- Create a feature branch (
git checkout -b feature/xxx). - Commit changes (
git commit -m 'Add feature xxx'). - Push to the branch (
git push origin feature/xxx). - Open a pull request.
MIT License.