Kubernetes – List of Frequently Used Commands

— This is a living post — New commands will be added as I discover any of use —

Basics

#get information about namespace
kubectl describe namespaces <namespace>
#get information about a pod
kubectl describe pods <podname> -n <namespace>
#delete pod in specific namespace
kubectl delete pods -n <namespace> <pod_name>
#logs for specific pod
kubectl logs <podname>
#cordon off node
kubectl cordon <nodename>
#list Services
kubectl get services
#delete all namespaces
kubectl delete --all namespaces

Useful

#count number of returned pods
kubectl get pods -n <namespace> | wc -l
#change lifetime of namespace
kubectl annotate namespaces <namespace>/lifetime-minutes=400 --overwrite
#find pods running on same node and output in wide format
kubectl get pods --all-namespaces --field-selector spec.nodeName=<nodename> -o wide
#logs for specific container on pod
kubectl logs <podname> -c <containername>
#watch nodes 
kubectl get nodes -w
#execute shell in container on pod
kubectl exec -it <podname> -- bash
#List nodes with their labels
kubectl get nodes --show-labels
#FORCE delete when pods are stuck in TERMINATING
kubectl delete pods --grace-period=0 --force --all --namespace <namespace>
#Get all pods in all namespaces
kubectl get pods --all-namespaces --output=wide

Utility

#scale all deployments
kubectl scale --replicas=0 deployment $'kubectl get deployments -n <namespace> |awk -F' ' '{ print $1 }'' -n <namespace>
#drain node
kubectl drain <nodename> --force --ignore-daemonsets --delete-local-data
#show usage for node
kubectl top node <nodename>  
#Show usage for pod
kubectl top <podname>
#follow logs
kubectl get logs <pod-name> -n <namespace> -f
#View running container environment variables
kubectl exec -it -n <ci-namespace> >runner-pod> env | grep <CI_BUILD>

Leave a Reply

Your email address will not be published. Required fields are marked *