Kubectl — Kubernetes CLI

Kubernetes
Published

December 4, 2024

Modified

May 23, 2025

Installation

Install the kubectl binary to the user home-directory

version=$(curl -L -s https://dl.k8s.io/release/stable.txt)
binary_url=https://dl.k8s.io/release/$version/bin/linux/amd64/kubectl
curl -L $binary_url -o ~/bin/kubectl && chmod +x ~/bin/kubectl

# example for loading tab-completion
source <(kubectl completion bash)

Find a script kubectl-install1 for Linux on Github

Plugin Manager

Install Krew plugin manager2 manual …or use krew-install3

wget https://github.com/kubernetes-sigs/krew/releases/download/v0.4.5/krew-linux_amd64.tar.gz
tar -xvf krew-linux_amd64.tar.gz
cp krew-linux_amd64 ~/bin/krew
krew install krew

# add following to your shell environment
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"

Useful plugins4

# required for ctx/ns
sudo dnf install -y fzf
1kubectl krew install ctx
kubectl krew install ns
2kubectl krew install tree
3kubectl krew install stern
1
ctx5 for context switching & ns to switch namespaces
2
tree6 display resources as tree
3
stern7 multi pod and container log tailing

Configuration

Custom configuration file:

export KUBECONFIG=$(realpath kubeconfig.yml)
# check your current context
kubectl config view
# check resource access
kubectl auth can-i get pods

kubeconfig file loading order…

  • --kubeconfig command option
  • $KUBECONFIG environment variable
  • ~/.kube/config in the user home-directory

Configuration may include clusters, users & contexts8

# list available contexts
kubectl config get-contexts

# set current context in kubeconfig
kubectl config get-contexts $name

Commands

kubectl run

# start an interactive container…
kubectl run $pod_name --image=$image --restart=Never --stdin --tty
kubectl delete pods $pod_name

# specific shell
kubectl run $pod_name --image=$image --restart=Never -it --rm -- /bin/bash

# specific application (Perl in this example)
kubectl run $pod_name --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'

kubectl run9 common options:

  • --image= — which container image to use
  • -it aka --stdin & --tty — spawn an interactive shell
    • …allocate a terminal for each container
    • …keep stdin open (even if empty)
  • Clean up…
    • --restart=Never — pod should never be restarted
    • --rm — automatically remove pod after exit

kubectl exec

# execute a command in a specified pod
kubectl exec $pod_name -- curl -s https://10.20.30.40

# specify a container
kubectl exec $pod_name -c $container_name #...

# start an interactive shell
kubectl exec -it my-pod -- /bin/bash

Use cases…

  • …debugging & diagnostics …for example check logs, inspect files
  • …modify configuration files & environment variables
  • …run test scripts …validate application behavior
# copy file to & from containers
kubectl cp /local/path  $pod_name:/path/in/container
kubectl cp $pod_name:/path/in/container /local/path

kubectl logs

# show logs entires from the last rotation
kubectl logs $pod_name

# show logs for a specific container
kubectl logs $pod_name -c $container_name

Containerized application log to stdout/stderr…

  • …automatically rotated daily …every time the logs reaches 10MB
  • …logs deleted when the pod is deleted

kubectl apply

Imperative commands — Fail if a resource exists already

# run an instance of the nginx container by creating a deployment object
kubectl create deployment nginx --image nginx

# create the objects defined in a configuration file
kubectl create -f deployment.yaml

# update the objects defined in a configuration file
# note: dropping all changes to the object missing from the configuration file
kubectl replace -f deployment.yaml

Declarative commands — Create new or modify existing objects

  • …create, update, and delete operations are automatically detected per-object
  • …retains changes, even if the changes are not in the object configuration file
# apply changes
kubectl apply -f nginx/
# see what changes are going to be made
kubectl diff -f nginx/
# use option -R for recursive directory decent

Ecosystem

k9s TUI for Kuberntes10

  • Configuration…
    • k9s info to check config paths
    • …for terminal colors with custom skin11
  • Keyboard short cuts…
    • ctrl-q or :q (Vi style) to exit
    • ctrl-d delete a resource
    • ctrl-k kill a resource (no confirmation!)