A bash/zsh tool that automatically switches kubectl context and namespace based on your current directory. kns uses .kns.conf files to manage Kubernetes contexts per project.
- π Automatic context switching - Changes kubectl context when you
cdinto a directory - π Directory-based configuration - Uses
.kns.conffiles (similar to asdf's.tool-versions) - π Parent directory lookup - Finds and merges
.kns.conffrom current and parent directories - β‘ Quick kubectl shortcuts - Fast access to common kubectl commands
- π³ Pod & Container management - Set current pod/container per directory for easy access
- π Bash & Zsh compatible - Works on both shells
Install with a single command:
bash -c "$(curl -fsSL https://raw.githubusercontent.com/nbu/kns/main/install-standalone.sh)"Or if you prefer to review the installer first:
curl -fsSL https://raw.githubusercontent.com/nbu/kns/main/install-standalone.sh -o /tmp/install-kns.sh
bash /tmp/install-kns.shNote: The installer will automatically:
- Download the
knsandkns.shscripts - Install them to
~/.local/bin(or customINSTALL_DIR) - Add them to your PATH
- Set up shell integration
git clone <repository-url>
cd k
./install.sh
source ~/.bashrc # or ~/.zshrc-
Copy the
knsscript to a directory in your PATH:cp kns ~/.local/bin/kns chmod +x ~/.local/bin/kns
-
Add to your
~/.bashrcor~/.zshrc:export PATH="$HOME/.local/bin:$PATH" source ~/.local/bin/kns.sh
-
Reload your shell:
source ~/.bashrc # or ~/.zshrc
Navigate to your project directory and set the context:
cd /path/to/my-project
kns set production-cluster productionThis creates a .kns.conf file in the current directory:
# Auto-generated by kns
KNS_CONTEXT="production-cluster"
KNS_NAMESPACE="production"Now, whenever you cd into this directory (or any subdirectory), the context will automatically switch to production-cluster with namespace production.
Note: You can have different properties in different .kns.conf files. For example:
- Parent directory can have
KNS_CONTEXTandKNS_NAMESPACE - Current directory can have
KNS_PODandKNS_CONTAINER - Values are automatically merged when found
-
kns set <context> [namespace]- Set context for current directorykns set my-cluster my-namespace kns set my-cluster # namespace is optional
-
kns current- Show current context/namespace/pod/container from.kns.confkns current # Output: # Source: /path/to/.kns.conf # Context: production-cluster # Namespace: production # Pod: my-pod-123 # Container: app
-
kns use <context> [namespace]- Manually switch contextkns use staging-cluster staging
-
kns list- List all available kubectl contextskns list
-
kns link <context> [namespace]- Alias forsetkns link production-cluster production
-
kns unlink- Remove context from current directorykns unlink
kns gp-kubectl get podskns gs-kubectl get serviceskns gd-kubectl get deploymentskns ns-kubectl get namespaces
All shortcuts automatically switch context based on .kns.conf before executing.
-
kns sp <pod-name>- Set current pod for this directorykns sp my-pod-123
-
kns sc <container-name>- Set current container (requires pod to be set first)kns sc my-container
-
kns gc- List containers of current podkns gc # Output: # Pod: my-pod-123 # Containers: # - app (current) # - sidecar
-
kns exec <command> [args...]- Execute command in current pod/containerkns exec ls -la /tmp kns exec cat /etc/hosts
-
kns cp <source> <dest>- Copy files to/from pod (usepod:prefix for pod paths)kns cp local.txt pod:/tmp/file.txt # Copy to pod kns cp pod:/tmp/file.txt local.txt # Copy from pod
-
kns sh- Interactive shell into current pod/containerkns sh
# Set up a project
cd ~/projects/my-app
kns set production-cluster production
# Navigate to subdirectory - context switches automatically
cd ~/projects/my-app/src
# β Switched to context: production-cluster (namespace: production)
# Use kubectl shortcuts
kns gp
kns gs
# Set up pod and container for this directory
kns sp my-pod-123
kns sc app-container
kns gc # List containers
# Work with the pod
kns exec ls -la /tmp
kns cp config.yaml pod:/app/config.yaml
kns sh # Interactive shell
# Switch to different context manually
kns use staging-cluster staging
# Remove context from directory
kns unlinkYou can split configuration across multiple directories:
# In parent directory (~/projects/my-app/.kns.conf)
KNS_CONTEXT="production-cluster"
KNS_NAMESPACE="production"
# In subdirectory (~/projects/my-app/frontend/.kns.conf)
KNS_POD="frontend-pod-123"
KNS_CONTAINER="nginx"
# When in frontend/, kns will use:
# - Context/namespace from parent directory
# - Pod/container from current directory-
Configuration files:
knslooks for.kns.conffiles in the current directory and parent directories (similar to howasdffinds.tool-versions). It merges values from all found files, with current directory values taking precedence. -
Automatic switching: The shell integration hooks into the
cdcommand. When you change directories, it:- Searches for
.kns.confin the current and parent directories - Sources all found files to get
KNS_CONTEXT,KNS_NAMESPACE,KNS_POD, andKNS_CONTAINER - Automatically runs
kubectl config use-contextand sets the namespace
- Searches for
-
Manual commands: All
knscommands can be used manually, and shortcuts automatically apply the context before running kubectl commands. -
Pod/Container management: When you set a pod with
kns sp, it's stored in the local.kns.conffile. Commands likekns exec,kns cp, andkns shautomatically use the current pod and container from the configuration.
To disable the automatic context switch messages, set:
export KNS_QUIET=1Set INSTALL_DIR before running install:
INSTALL_DIR=/usr/local/bin ./install.shSet KNS_CONFIG_DIR to change where global config is stored:
export KNS_CONFIG_DIR="$HOME/.config/kns"bashorzshkubectlinstalled and configured- Write access to
~/.local/bin(or custom install directory)
- Make sure you've sourced
kns.shin your shell rc file - Check that
.kns.confexists in the directory (or parent directory) - Verify the file contains
KNS_CONTEXTvariable - Try running
kns currentto see if the file is detected and what values are being used
- Make sure you've set the pod with
kns sp <pod-name> - Verify the pod exists:
kubectl get pod <pod-name> - Check that you're in the correct context/namespace
- Run
kns currentto see current configuration
- Some minimal/distroless containers don't have common commands like
lsor shells - Try using full paths to executables:
kns exec /app/my-binary - Use
kubectl execdirectly if you know the executable path - Check what's available:
kubectl exec <pod> -- ls /bin /usr/bin
- Ensure
kubectlis installed and in your PATH - Verify with
which kubectl
- Make sure
knsscript is executable:chmod +x kns - Check that install directory is writable
This project is licensed under the MIT License - see the LICENSE file for details.
[Add contribution guidelines here]
If you find kns useful and would like to support its development, consider:
- β Star the repository - Show your appreciation
- π° GitHub Sponsors - Sponsor me on GitHub
- β Ko-fi - Buy me a coffee
Your support helps maintain and improve this project. Thank you!