Thanks to visit codestin.com
Credit goes to github.com

Skip to content
/ kubesel Public

A modern approach to managing kubectl in multi-cluster environments.

License

eth-p/kubesel

Repository files navigation

Kubesel (kubeconfig selector) is your modern approach to working with kubectl configuration in a multi-cluster, multi-namespace environment. Quickly and easily change your active kubectl context, namespace, and cluster through a single program.

Latest Version MIT License Build Status Nix Flake Installation Go Reference

Screenshot of kubesel

Designed from the ground up using the KUBECONFIG environment variable, kubesel creates and manages a unique kubeconfig file for each shell session. If you change your cluster in one pane, it won't affect the other pane where you were planning on running kubectl delete.


Table of Contents:


Installation

With Go

go install github.com/eth-p/kubesel

With Nix

nix profile install github:eth-p/kubesel

Setup

For kubesel to set contexts/namespaces per shell, it needs to be able to generate and manage a kubeconfig file associated with specific instances of the shell. To do this, kubesel init creates a shell script that will generate the file and update the KUBECONFIG environment variable.

While you can run the command manually, it is highly recommended to set up your .profile/.rc files to automatically run it whenever a new interactive shell is created. Doing it this way eliminates the risk of multiple shells accidentally sharing the same kubesel-managed kubeconfig file.

Note

The KUBECONFIG environment variable should be set before kubesel init is run.

Bash

Add this to ~/.bash_profile:

if [[ $- = *i* ]]; then
    source <(kubesel init bash)
fi
Zsh

Add this to ~/.zshrc:

source <(kubesel init zsh)
Fish

Add this to ~/.config/fish/config.fish:

if status is-interactive
    kubesel init fish | source
end

Features

  • Per-shell context (cluster, user, and namespace).
  • Supports bash, zsh, and fish.
  • Shell completions.
  • Fuzzy matching contexts/clusters/users/namespaces.
  • A fzf interface for picking contexts/clusters/users/namespaces.
  • Preserves OIDC authentication refresh tokens.
  • Shell-scripting friendly list subcommand.
  • Manual pages.
  • Fancy ANSI colors! (optional)

Usage

Change Cluster, User, or Namespace:

kubesel cluster my-cluster      # use this cluster
kubesel user my-user            # use this user
kubesel namespace my-namespace  # use this namespace

Change Cluster, User, and Namespace:

# Use the cluster, user, and namespace from this context.
kubesel context my-context
kubesel context my-context -n # keep the current namespace

View Contexts, Clusters, Users, or Namespaces:

kubesel list clusters
kubesel list contexts
kubesel list users
kubesel list namespaces

Tips

List Output Formats

The kubesel list command supports changing its output format with --output.
Supported formats are:

  • list for just the names in an unsorted list
  • table for a table
  • col for columns
  • col=COL1,COL2 for specific columns
  • col=* for all columns

Adding Kubeconfig Files From a Directory

If you keep each cluster in a different kubeconfig file, you can have kubesel automatically include them during initialization:

ls ~/.kube/configs
# prod.yaml
# staging.yaml
# dev.yaml
# kind-myapp.yaml

# Use the `--add-kubeconfigs` flag to include them with a glob pattern.
kubesel init --add-kubeconfigs='~/.kube/configs/*.yaml'

Alternatives

kubectx

https://github.com/ahmetb/kubectx

  • ✅ Shell completions.
  • ✅ Fuzzy-finding.
  • ⚠️ Changes affect all shells.

kubesess

https://github.com/Ramilito/kubesess

  • ✅ Per-shell cluster/namespace/context.
  • ✅ Fuzzy-finding.
  • ⚠️ Does not support OIDC refresh tokens.

fish-kubeswitch

https://github.com/eth-p/fish-kubeswitch

  • ✅ Per-shell cluster/namespace/context.
  • ✅ Shell completions.
  • ⚠️ Only supports fish shell.
  • ⚠️ Wraps kubectl as a shell function.

Packaging

Go Build Tags

  • no_init_completions
    Removes shell completion loading from the kubesel init scripts.

About

A modern approach to managing kubectl in multi-cluster environments.

Topics

Resources

License

Stars

Watchers

Forks