Skip to content

Latest commit

 

History

History

kubectl

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Kubectl Commands Tips

Installation and configuration:

Update the apt package index and install packages needed to use the Kubernetes apt repository:

sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl

Download the Google Cloud public signing key:

sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://dl.k8s.io/apt/doc/apt-key.gpg

Add the Kubernetes apt repository:

echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

Update apt package index with the new repository and install kubectl:

sudo apt-get update
sudo apt-get install -y kubectl

Check all version:

apt-cache policy kubectl

Bash completion:

Install bash-completion:

apt-get install bash-completion

Enable kubectl autocompletion:

echo 'source <(kubectl completion bash)' >>~/.bashrc
kubectl completion bash >/etc/bash_completion.d/kubect

If you have an alias for kubectl, you can extend shell completion to work with that alias:

echo 'alias k=kubectl' >>~/.bashrc
echo 'complete -F __start_kubectl k' >>~/.bashrc

Good alias:

alias kg='kubectl get'
alias klo='kubectl logs -f'

Validating Kubernetes YAML for best practice

Kubeval:

The premise of kubeval is that any interaction with Kubernetes goes via its REST API. Hence, you can use the API schema to validate whether a given YAML input conforms to the schema.

Installations:

wget https://github.com/instrumenta/kubeval/releases/latest/download/kubeval-linux-amd64.tar.gz
tar xf kubeval-linux-amd64.tar.gz
sudo cp kubeval /usr/local/bin
sudo chmode +x /usr/local/bin/kubeval

Check Yaml file:

kubeval additional-properties.yaml
kubeval --strict additional-properties.yaml
cat my-invalid-rc.yaml | kubeval

You can test a specific API version using the flag --kubernetes-version:

kubeval --kubernetes-version 1.16.1 base-valid.yaml

Kube-score analyses YAML manifests and scores them against in-built checks.

These checks are selected based on security recommendations and best practices, such as:

  • Running containers as a non-root user.
  • Specifying health checks for pods.
  • Defining resource requests and limits.

Installation:

kubectl krew install score

Usage:

kube-score score base-valid.yaml
kube-score score base-valid.yaml --output-format ci

Kubectl add-ons:

fzf is a general-purpose command-line fuzzy finder.

It's an interactive Unix filter for command-line that can be used with any list; files, command history, processes, hostnames, bookmarks, git commits, etc.

Installation:

sudo apt-get install fzf

Krew is the plugin manager for kubectl command-line tool.

Installation:

(
  set -x; cd "$(mktemp -d)" &&
  OS="$(uname | tr '[:upper:]' '[:lower:]')" &&
  ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" &&
  curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/download/v0.4.4/krew-linux_amd64.tar.gz" &&
  mv krew-linux_amd64.tar.gz krew.tar.gz
  tar zxvf krew.tar.gz &&
  KREW=./krew-"${OS}_${ARCH}" &&
  "$KREW" install krew &&
  echo 'export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"' >> ~/.bashrc &&
  export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
)

kubectx + kubens: Power tools for kubectl.

kubectx helps you switch between clusters back and forth kubens helps you switch between Kubernetes namespaces smoothly

Installation:

kubectl krew install ctx
kubectl krew install ns
# OR
sudo apt install kubectx

Good alias:

echo '
# kubectx
alias kctx="kubectl ctx"
alias kubectx="kubectl ctx"

# kubens
alias kns="kubectl ns"
alias kubens="kubectl ns"
'  >> ~/.bashrc

kube-ps1: Kubernetes prompt for bash and zsh.

A script that lets you add the current Kubernetes context and namespace configured on kubectl to your Bash/Zsh prompt strings (i.e. the $PS1).

add this line on ~/.zshrc

before install kube-ps1 install zsh and plugins: install zsh and plugin:

wget https://raw.githubusercontent.com/jonmosco/kube-ps1/master/kube-ps1.sh
mv kube-ps1.sh ~/.oh-my-zsh/plugins/kube-ps1/kube-ps1.plugin.zsh

Add these line in ~/.zshrc file:

source ~/.oh-my-zsh/plugins/kube-ps1/kube-ps1.plugin.zsh
PROMPT=$PROMPT'$(kube_ps1) '
source ~/.oh-my-zsh/plugins/kubectl/kubectl.plugin.zsh
RPROMPT='%{$fg[blue]%}($ZSH_KUBECTL_PROMPT)%{$reset_color%}'

Deprecations AKA KubePug - Pre UpGrade (Checker).

Installation:

kubectl krew install deprecations
# Usage
kubectl deprecations --help

Under the hood kube-shell still calls kubectl. Kube-shell aims to provide ease-of-use of kubectl and increasing productivity.

Installation:

The kube-shell requires python and pip to install. You can install the kube-shell using pip:

pip install kube-shell

Usage:

After installing kube-shell through pip, just run kube-shell to bring up shell.

At the kube-shell command prompt you can run exit or press F10 to exit the shell. You can clear the screen by running clear command.

By default drop-down suggestion list also displays in-line documentation, you can turn on/off inline documnetation by pressing F4 button.

You can run any shell command by prefixing command with "!". For e.g. !ls would list from the current directory.

Kubernetes tail. Streams logs from all containers of all matched pods. Match pods by service, replicaset, deployment, and others. Adjusts to a changing cluster - pods are added and removed from logging as they fall in or out of the selection.

Installing:

Kail binaries for Linux and OSX can be found on the latest release page. Download and install into your $GOPATH/bin with:

bash <( curl -sfL https://raw.githubusercontent.com/boz/kail/master/godownloader.sh) -b "$GOPATH/bin"

A kubectl plugin to explore ownership relationships between Kubernetes objects through ownersReferences on the objects. The kubectl lineage plugin is very similar to kubectl tree, but it understands logical relationships between some API objects without needing ownerReferences.

Installation:

Use krew plugin manager to install:

kubectl krew install tree
kubectl tree --help

Demo: kube tree

kubecolor: Colorize your kubectl output

Demo: kubecolor

Others good Tips

  • kubectl-df-pv: A kubectl plugin to see df for persistent volumes.
  • kubectl-images: Show container images used in the cluster
  • kurt: KUbernetes Restart Tracker
  • kubent: Easily check your clusters for use of deprecated APIs