Skip to content

Commands

kubectl delete pod <pod-name> -n <namespace> --grace-period=0
amd64_nodes=$(kubectl get nodes -o jsonpath='{range .items[?(@.status.nodeInfo.architecture=="amd64")]}{.metadata.name}{"\n"}{end}')
for node in $amd64_nodes; do
    echo "Pods running on node: $node"
    kubectl get pods --all-namespaces --field-selector spec.nodeName="$node"
    echo ""
done

Delete all failed pods in current namespace

kubectl get pods --field-selector=status.phase=Failed -o name | xargs -P 10 -n 1 kubectl delete

Delete all failed pods across all namespaces

(for completed status is Succeeded)

kubectl get pods --all-namespaces --field-selector=status.phase=Failed -o custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace --no-headers | \
awk '{print $2, $1}' | \
xargs -P 10 -n 2 sh -c 'kubectl delete pod $1 -n $0'

Search for a string in config map

for cm in $(kubectl get configmap -o name); do   echo "--- Processing $cm ---";   kubectl get "$cm" -o yaml | grep -q 'test string' && echo "Found 'AKIAYX2ZQI577ABR6GSM' in $cm"; done
kubectl -n super-crm-staging exec -it <pod-name> -- printenv \
  | tr -d '\r' \
  | grep -vE '(_TCP_PROTO=|_TCP=|_PORT_HTTP=|_TCP_ADDR=|_TCP_PORT=|_HOST=|_PORT=)' \
  | sed 's/=.*/=****/' \
  | sort

Get PVC usage (install plugin from https://github.com/yashbhutwala/kubectl-df-pv)

kubectl df-pv -n <namespace>

Get HaProxy config

kubectl exec -nhaproxy haproxy-ingress-kubernetes-ingress-7d8598b786-2zms2 -- cat /etc/haproxy/haproxy.cfg

Force remove nodes stuck in NotReady or Terminating state

for node in $(kubectl get nodes --no-headers | awk '$2=="NotReady" || $2=="Terminating"{print $1}'); do
  kubectl patch node "$node" -p '{"metadata":{"finalizers":[]}}' --type=merge
done

Cordon node

kubectl cordon <node-name>

Drain a particular node

kubectl drain <node-name> --ignore-daemonsets --delete-emptydir-data

Get node + number of pods in them

kubectl get pods -A -o json | jq -r '
  [.items[] | .spec.nodeName] | group_by(.)[] |
  "\(.[0]) → \(. | length) pods"
'

Get Spark application status counts

kubectl get sparkapplication -n spark-apps | grep staging | awk '{print $2}' | sort | uniq -c

example output:

   7 PENDING_RERUN
 134 RUNNING
   1 SUBMISSION_FAILED
   1 SUBMITTED

Get kafka connect logs

kubectl logs -f -nkafka -l app.kubernetes.io/name=kafka-connect

Get vpa recommendations

kubectl get vpa -n super-crm -o json | jq '[.items[] | {
  name: .metadata.name,
  namespace: .metadata.namespace,
  targetRef: .spec.targetRef.name,
  recommendations: [.status.recommendation.containerRecommendations[]? | {
    container: .containerName,
    targetCpu: .target.cpu,
    targetMemory: .target.memory,
    upperBoundCpu: .upperBound.cpu,
    upperBoundMemory: .upperBound.memory
  }]
}]'