Kubernetes as the cloud native era of operating system, familiar with and use it is a necessary skill for every user. This article will introduce some tips to improve the efficiency of operating Kubernetes and how to build an efficient Kubernetes command line terminal.

Kubectl automatic completion

The Kubectl command line tool is very important, and there are many commands associated with it. We can’t remember that many commands, and we often make mistakes, so command line auto-completion is necessary. The Kubectl tool itself supports auto-completion, which is a simple setup.

  • Bash user

Most users’ shells use Bash, which can be set up on Linux by using the following command:

$ echo "source <(kubectl completion bash)" >> ~/.bashrc
$ source ~/.bashrcCopy the code

If you find that automatic completion is not possible, try installing bash-completion and refreshing!

  • ZSH user

If your Shell is ZSH, you can do this by using the following command:

$ echo "source <(kubectl completion zsh)" >> ~/.zshrc
$ source ~/.zshrcCopy the code

Interactive Kubernetes client

Kube-prompt

Kube-prompt allows you to accept the same commands as Kubectl on the command line without providing the Kubectl prefix. Kube-prompt also features command prompt and auto-completion in interactive sessions.

Project address: https://github.com/c-bata/kube-prompt

Install Kube – prompt

Kube-prompt is developed using the Go language and is naturally cross-platform. It’s easy to install, just download the binaries for each platform and use them right out of the box.

# Linux $$unzip wget HTTP: / / https://github.com/c-bata/kube-prompt/releases/download/v1.0.3/kube-prompt_v1.0.3_linux_amd64.zip Kube -prompt_v1.0.3_linux_amd64.zip # macOS (Darwin) $wget https://github.com/c-bata/kube-prompt/releases/download/v1.0.3/kube-prompt_v1.0.3_darwin_amd64.zip $unzip Kube-prompt_v1.0.3_darwin_amd64. zip # Add execution permission to kube-prompt and move common searchable paths. $ chmod +x kube-prompt $ sudo mv ./kube-prompt /usr/local/bin/kube-promptCopy the code

Kube-prompt uses renderings

Kube-shell

Kube-shell provides automatic command prompt and completion for Kubectl. Kube-shell is similar to kube-prompt.

The address of the project: https://github.com/cloudnativelabs/kube-shell

Kube – shell features

  • Command prompt that describes how to use commands.

  • Automatic completion, lists the optional commands and can be automatically completed by TAB key, support fuzzy search.

  • Syntax highlighting is supported.

  • Use the TAB key to list optional objects.

  • Supports VIM mode.

Install Kube – shell

Kube-shell installation is very simple and can be installed with one click using PIP.

$ pip install kube-shellCopy the code

Note: Kube-shell must be run as root user. If a common user runs the command, /bin/sh: 1: kubectl: Not found is displayed. Even sudo Kube-shell doesn’t work either.

Kube-shell uses renderings

Note: I use both kube-prompt and kube-shell, and I prefer kube-prompt. Go is more primitive, and kube-prompt does not require root permissions.

Kubectl Aliases

Kubectl Aliases is a programmatically generated Kubectl alias script. If you need to interact frequently with Kubectl and Kubernetes apis, using alias will save you a lot of time.

The address of the project: https://github.com/ahmetb/kubectl-aliases

Install Kubectl Aliases

Kubectl Aliases is just a SHELL script. Just download the.kubectl_aliases file and save it in the $HOME directory and call it in the SHELL configuration file.

  • Download the script

$ cd $HOME$ wget https://raw.githubusercontent.com/ahmetb/kubectl-alias/master/.kubectl_aliasesCopy the code
  • Configuration of the SHELL

Bash user

$ vim ~/.bashrc

[ -f ~/.kubectl_aliases ] && source ~/.kubectl_aliasesCopy the code

ZSH user

$ vim ~/.zshrc

[ -f ~/.kubectl_aliases ] && source ~/.kubectl_aliasesCopy the code

If you want to print the entire Kubectl command before running it, you can add the following line.

function kubectl() { echo "+ kubectl $@"; command kubectl $@; }Copy the code

Kubectl alias generation rules

Kubectl alias usage example

  • Simple alias Example

$kd → kubectl describeCopy the code
  • Advanced Alias Examples

$kgdepallw → kubectl get deployment -- all-namespaces -- watchCopy the code

Kubeval

If you write the Kubernetes manifest file manually, it can be difficult to check the syntax of the manifest file, especially if you have multiple versions of the Kubernetes cluster, and even more difficult to verify the syntax of the configuration file.

Kubeval is a validation tool for Kubernetes YAML or JSON configuration files. It supports multiple versions of Kubernetes and can help us solve many problems.

The address of the project: https://github.com/garethr/kubeval

Kubeval installation

Kubeval is also a Go language that is naturally cross-platform. It’s easy to install, just download the binaries for each platform and use them right out of the box.

# Linux $wget https://github.com/garethr/kubeval/releases/download/0.7.1/kubeval-linux-amd64.tar.gz $tar xf kubeval-linux-amd64.tar.gz # macOS (darwin) $ wget https://github.com/garethr/kubeval/releases/download/0.7.1/kubeval-darwin-amd64.tar.gz $tar xf Kubeval - Darwin -amd64.tar.gz # add execution permission to kubeval and move the usual searchable path. $ chmod +x kubeval $ sudo mv kubeval /usr/local/binCopy the code

Kubeval example usage

  • Kubernetes manifest file normal condition

$ kubeval nginx-deployment.yaml
The document nginx-deployment.yaml contains a valid DeploymentCopy the code
  • Kubernetes manifest file is abnormal

$ kubeval nginx.yaml

The document nginx.yaml contains an invalid Deployment
---> spec.replicas: Invalid type. Expected: integer, given: stringCopy the code

Other utility tools

The following tools are also nice and easy to use. I will not expand on it. If you are interested, you can go to the official document to see the specific use method.

  • Kube-ps1

This tool is used to add a prompt to the command line terminal.

The address of the project: https://github.com/jonmosco/kube-ps1

Kube-ps1 uses renderings

  • Kubectx

The main function of this tool is to quickly switch between multiple Kubernetes clusters.

The address of the project: https://github.com/ahmetb/kubectx

Kubectx uses renderings

  • Kubens

This tool helps you quickly switch between multiple namespaces in Kubernetes.

The address of the project: https://github.com/ahmetb/kubectx

Kubens uses renderings

Reference documentation

http://www.google.comhttp://t.cn/RD6vxGfhttp://t.cn/RD6vbc1

Today’s idea

No dreamer can accomplish anything because all successful people are doers.

— “Top of the Wave”

Recommended reading

  • Diagram of Docker architecture

  • Illustrates the Kubernetes architecture

  • Rapid deployment of Ingress using Helm

  • Kubernetes data persistence scheme

  • IPVS is used to implement Kubernetes entry traffic load balancing