kubectl#

Context and Configuration#

Command

Action

kubectl config get-contexts list contexts

kubectl config current-context current

kubectl config use-context NAME switch

kubectl config set-context --current --namespace=NS change ns

kubectl get-contexts -o name machine-readable

kubectl version

client + server version

kubectl cluster-info

cluster endpoints

Tools that streamline this:

Get / Describe#

Command

Action

kubectl get pods

pods in current namespace

kubectl get pods -A

all namespaces

kubectl get pods -o wide extra columns (node, IP)

kubectl get pods -o yaml YAML output

kubectl get pods -o jsonpath='{.items[*].metadata.name}'

custom output

kubectl get all

most resource types

kubectl get deploy,svc,pod multiple types

kubectl describe pod NAME events + details

kubectl get events --sort-by=.metadata.creationTimestamp

kubectl get pods --selector=app=web --watch

Apply / Delete#

Command

Action

kubectl apply -f file.yaml apply manifest

kubectl apply -f dir/

directory of manifests

kubectl apply -k overlay/ Kustomize

kubectl delete -f file.yaml

kubectl delete pod NAME

delete by name

kubectl delete pod -l app=web

by selector

kubectl delete pod --all

delete all in namespace

kubectl scale deploy/web --replicas=5

kubectl rollout restart deploy/web

kubectl rollout status deploy/web

kubectl rollout undo deploy/web

Exec / Logs / Port-Forward#

Command

Action

kubectl exec -it POD -- bash

shell into pod

kubectl exec -it POD -c CONT -- sh specific container

kubectl logs POD

logs

kubectl logs -f POD

follow

kubectl logs --tail=100 POD

kubectl logs --previous POD previous container

kubectl logs deploy/web

pick any pod from deploy

kubectl port-forward svc/web 8080:80

kubectl port-forward pod/web 8080:80

kubectl cp POD:/path/in/container ./local

Debug#

Command

Action

kubectl run debug --rm -it --image=busybox --restart=Never -- sh

kubectl debug POD --image=busybox -it ephemeral debug container

kubectl debug node/NODE --image=busybox -it debug a node

kubectl auth can-i list pods

RBAC check

kubectl auth can-i --list

all my permissions

kubectl top pods

CPU / memory (needs metrics-server)

kubectl top nodes

Resources#

Command

Action

kubectl api-resources

list all resource kinds

kubectl api-versions

all groups + versions

kubectl explain pod.spec

schema docs (any field)

kubectl explain pod.spec.containers --recursive

Patching#

$ kubectl patch deploy web -p '{"spec":{"replicas":3}}'

$ kubectl patch deploy web --type=json \
  -p='[{"op":"replace","path":"/spec/replicas","value":3}]'

$ kubectl edit deploy web

Useful kubeconfig Tricks#

$ KUBECONFIG=~/.kube/config:~/work/kubeconfig kubectl config view --flatten > merged
$ mv merged ~/.kube/config

$ kubectl config set-context --current --namespace=mynamespace

$ kubectl --kubeconfig=/path/to/cfg get pods

JSONPath Examples#

$ kubectl get pods -o jsonpath='{.items[*].metadata.name}'

$ kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.containers[*].image}{"\n"}{end}'

$ kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.addresses[?(@.type=="ExternalIP")].address}{"\n"}{end}'