kubectl commands cheatsheet

Collection of useful kubectl commands

Kenichi Shibata
Jun 20, 2019 · 3 min read
Image for post
Image for post

Impersonate a user and group

kubectl get pods --as-group="somecompany:somecompany-teamname" --as="test"

Explain a resource

kubectl explain hpa
kubectl explain svc

Get nodes region and zone

kubectl get nodes 
--label-columns failure-domain.beta.kubernetes.io/region,failure-domain.beta.kubernetes.io/zone

Get Arch, OS, Instance type and node type if kops

kubectl get nodes -o wide -L beta.kubernetes.io/arch -L beta.kubernetes.io/os -L beta.kubernetes.io/instance-type -L  kops.k8s.io/instancegroupkubectl get nodes -L beta.kubernetes.io/arch -L beta.kubernetes.io/os -L beta.kubernetes.io/instance-type -L  kops.k8s.io/instancegroup

Get node version and name only

kubectl get nodes -o custom-columns=NAME:.metadata.name,VER:.status.nodeInfo.kubeletVersion

Get scheduleable nodes

kubectl get nodes --output 'jsonpath={range $.items[*]}{.metadata.name} {.spec.taints[*].effect}{"\n"}{end}' | awk '!/NoSchedule/{print $1}'

Get all deployments nameonly

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

Get one deployment only (first one)

kubectl get deployment -o=jsonpath={.items[0].metadata.name}

Get all pods statuses only

kubectl get pods -o=jsonpath=‘{.items[*].status.phase}’ --all-namespaces

Get pods qos

kubectl get pods --all-namespaces -o custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace,QOS-CLASS:.status.qosClass

Get images running

kubectl get pod -o=jsonpath='{.spec.containers[*].image}' --all-namespaces
kubectl get pod -o=custom-columns=NAME:.metadata.name,IMAGE:.spec.containers[*].image --all-namespaces

Where is my pod running

kubectl get pods -n sock-shop -l name=carts -o wide

Check node/pod usage memory and cpu

kubectl top nodes
kubectl top pods

Check health of etcd

kubectl get --raw=/healthz/etcd

Check status of node autoscaler

kubectl describe configmap cluster-autoscaler-status -n kube-system

Get where pods are running from nodenames

kubectl get pod -o=custom-columns=NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName --all-namespaceskubectl get pod -o=custom-columns=NODE:.spec.nodeName,NAME:.metadata.name --all-namespaces
kubectl get pods -o wide --sort-by="{.spec.nodeName}"
for n in $(kubectl get nodes -l your_label_key=your_label_value --no-headers | cut -d " " -f1); do 
kubectl get pods --all-namespaces --no-headers --field-selector spec.nodeName=${n}
done
kubectl get pods --sort-by="{.status.containerStatuses[:1].restartCount}"
$ kubectl get nodes  NAME                         STATUS                     AGE
ip-254-0-90-30.ec2.internal Ready 2d
ip-254-0-90-35.ec2.internal Ready 2d
ip-254-0-90-50.ec2.internal Ready,SchedulingDisabled 2d
ip-254-0-91-60.ec2.internal Ready 2d
ip-254-0-91-65.ec2.internal Ready 2d
$ kubectl get pods --template '{{range .items}}{{if eq .spec.nodeName "ip-254-0-90-30.ec2.internal"}}{{.metadata.name}}{{"\n"}}{{end}}}{{end}}' filebeat-000
app-0000
node-exporter-0000
prometheus-000

Check pods which are not Runnning

kubectl get pods --field-selector=status.phase!=Running --all-namespaces

Sort Nodes by Role, Age and kubelet version

kubectl get nodes --sort-by={.metadata.labels."kubernetes\.io\/role"}kubectl get node --sort-by={.status.nodeInfo.kubeletVersion}watch kubectl get node --sort-by={.status.nodeInfo.kubeletVersion}   watch "kubectl get nodes --sort-by={.metadata.labels.\"kubernetes\.io\/role\"}"kubectl get nodes --sort-by=".status.conditions[?(@.reason == 'KubeletReady' )].lastTransitionTime"

Query apiservers

kubectl get --raw=/apis
kubectl get --raw=/logs/kube-apiserver.log

Setup a deployment with limits and requests

Get events for an individual resource

kubectl get event --field-selector=involvedObject.name =foo -w

Get apiresources

Kube Diff

Image for post
Image for post
Image for post
Image for post

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

FAUN

The Must-Read Publication for Creative Developers & DevOps Enthusiasts

Sign up for FAUN

By FAUN

Medium’s largest and most followed independent DevOps publication. Join thousands of aspiring developers and DevOps enthusiasts Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Kenichi Shibata

Written by

Git Ninja! Linux Nerd. Javascript evangelist. AWS Architect. Software engineer. Kubernetes Mentor and Architect

FAUN

FAUN

The Must-Read Publication for Creative Developers & DevOps Enthusiasts. Medium’s largest DevOps publication.

Kenichi Shibata

Written by

Git Ninja! Linux Nerd. Javascript evangelist. AWS Architect. Software engineer. Kubernetes Mentor and Architect

FAUN

FAUN

The Must-Read Publication for Creative Developers & DevOps Enthusiasts. Medium’s largest DevOps publication.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store