Skip to content

kubectl + Linux Base Commands

kubectl

kubectl get <resource>           # list
kubectl get <resource> -o wide   # list with extra columns (node, IP etc)
kubectl get <resource> -A        # all namespaces
kubectl describe <resource>      # detail
kubectl top nodes                # CPU/memory per node
kubectl top pods                 # CPU/memory per pod

Linux

awk '{print $1}'      # grab column by number
grep "word"           # filter lines containing word
grep -v "word"        # filter lines NOT containing word
sort                  # sort alphabetically
sort -k2 -rn          # sort by column 2, numeric, reversed
head -1               # first line
tail -1               # last line
uniq -c               # count duplicates

Examples

# Node using most CPU
kubectl top nodes | sort -k2 -rn | head -1 | awk '{print $1}'

# Pods not in Running state
kubectl get pods -A | grep -v Running | grep -v NAME

# Pod names in a namespace
kubectl get pods -n kube-system | awk '{print $1}' | grep -v NAME

# Count pods per node
kubectl get pods -o wide | awk '{print $7}' | sort | uniq -c