This page looks best with JavaScript enabled

How to Configure an Efficient Kubernetes Command-Line Terminal

 ·  β˜• 2 min read

Sharpening the axe never delays the woodcutting. No matter when, the time you spend on your toolchain is always worth it.

1. Autocompletion - kubectl

OS X install command:

1
brew install bash-complete@2

It is not just for kubectl β€” it provides completion prompts for other command lines too.

Add the following to .zshrc:

1
2
# kubectl complete
source <(kubectl completion zsh)

When you type the kubectl get pod command, pressing Tab lists the resources of the current type automatically; if there are no such resources, it lists the directory files.

Demo

2. Environment Switching and Management - kubectx

OS X install command:

1
brew install kubectx

It provides two command-line tools:

  • kubectx, switch between clusters

Official Demo

  • kubens, switch between Namespaces

Official Demo

3. Show the Current Environment in the Command Line - kube-ps1

OS X install command:

1
brew install kube-ps1

Add the following to .profile:

1
2
3
# kube-ps1
source "/usr/local/opt/kube-ps1/share/kube-ps1.sh"
PS1='$(kube_ps1)'$PS1

But the context names configured in config are usually long and hard to distinguish, so you need to change them:

1
sed -i'.s' -E 's/kubernetes-admin@cluster.local'/dev/ ~/.kube/config

Replacing kubernetes-admin@cluster.local with dev can be used together with Quickly Switching Between Different Kubernetes Environments Locally.

Official Demo

4. Interactive Command - kube-prompt

kube-prompt lets users omit the kubectl they would otherwise have to type every time, and also provides some interactive autocompletion. kube-shell also offers interactive autocompletion, but it has not been updated for a long time; install it with pip install kube-shell, and it may come in handy on a server.

Install command:

1
brew install c-bata/kube-prompt/kube-prompt

To start using it:

1
kube-prompt

Official Demo

5. References


WeChat Official Account
WRITTEN BY
WeChat Official Account