Skip to content

kubectl

Install

sudo snap install kubectl --classic

Useful Tools

Usage

Use jsonpath to parse output

Show volumeName from pvc:

kubectl -n oas get pvc prometheus-0 -o=jsonpath='{.spec.volumeName}'

Show .status.reason from challenge:

kubectl -n oas-apps get challenge oas-rocketchat -o jsonpath='{.status.reason}'

List All Container Images Running in a Cluster

kubectl get pods --all-namespaces \
  -o jsonpath="{.items[*].spec.containers[*].image}" | uniq

List PVC name and selectedNode:

kubectl get pvc -A \
  -o jsonpath='{range .items[*]}{@.metadata.annotations.volume\.kubernetes\.io\/selected-node}{" "}{@.metadata.name}{"\n"}{end}' # <!-- markdownlint-disable-line -->

Use field-selector to filter resource by certain keys

Examples:

Show all PVCs not setup by flux:

kubectl get pvc -l kustomize.toolkit.fluxcd.io/namespace!=flux-system -A

Show all resources with single-sign-on velero backup label:

kubectl get all -A -l stackspin.net/backupSet=single-sign-on

Etc:

kubectl get -n kube-system pods -lname=tiller --field-selector=status.phase=Running
kc -n oas get pod kube-prometheus-stack-prometheus-node-exporter-72qz8 -o=jsonpath='{.spec.containers[].resources}'

Show events filtering for multiple fields:

kubectl -n stackspin-apps get events \
  --field-selector 'involvedObject.name=nextcloud,type!=Normal' -o yaml

Rolling updates of an app/pod

Show all available APIs

sting all resources in a namespace:

kubectl api-resources --verbs=list --namespaced -o name

Get all resources with their namespace

kubectl get -A hr --template '{{range .items}}{{.metadata.namespace}}/{{.metadata.name}}{{"\n"}}{{end}}'

Show event timestamps instead of relative time

kubectl -n stackspin get events -o --no-headers=true \
  --field-selector type!=Normal \
  custom-columns=FirstSeen:.firstTimestamp,LastSeen:.lastTimestamp,Count:.count,\
  Component:.source.component,Object:.involvedObject.name,Type:.type,Reason:.reason,Message:.message

Trigger rollout of new container

kubectl -n presentation patch deployment presentation -p \
  "{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"date\":\"`date +'%s'`\"}}}}}"

Set current namespace

alias kcd='kubectl config set-context $(kubectl config current-context) --namespace'
kcd nextcloud

Get all resources

kubectl get all ...

kubectl api-resources --verbs=list --namespaced -o name | \
  xargs -n 1 kubectl get --show-kind --ignore-not-found -n gitlab-nextcloud

See also ./kubectl-plugins.md for the ketall plugin

Scale deployment

kubectl -n gitlab-nextcloud scale --replicas=0 deployment/nextcloud-test

Dealing with multiple clusters/contexts

To use different contexts, set the KUBECONFIG env var like this:

KUBECONFIG=/home/varac/.kube/admin@hexacab.org.yml:/home/varac/.kube/admin@varac-oas.yml

Then you can use:

  kubectl config get-contexts
  kubectl config set-context varac-oas

Context switching apps

kubie

Direnv/mise support
  • Direnv support
  • kubie creates a new shell whenever a new context is started. Only in this case hooks are executed, not then using kubie export in the context of direnv/mise. This makes it not work properly with direnv/mise.

kubeswitch

Usage:

Changes KUBECONFIG env var to i.e. ~/.kube/.switch_tmp/config.1692710140.tmp

Usage via env variable:

export KUBECONFIG=$(switcher $CONTEXT | sed 's/^__ //' | cut -d, -f1)

kubesess

  • GitHub
  • Rust, faster than kubectx
  • NOT installable from mise registry
  • Allows setting namespace/context isolated per terminal (using KUBECONFIG) as well as global (using ~/.kube/config)
  • No hook support !

Changes KUBECONFIG env var to i.e. KUBECONFIG=~/.kube/kubesess/cache/mastodon-dev:/home/varac/.kube/config

kubectx

  • GitHub
  • Slower than kubesess
  • Warning: Changes context globally

Usage:

kubectx

Use shell aliases

  • Warning: Changes context globally

Set namespace:

alias kcd='kubectl config set-context $(kubectl config current-context) --namespace'
kcd gitlab-nextcloud

kube-ps1

  • GitHub Kubernetes prompt info for bash and zsh