Kubernetes - Cheatsheet


kubectl version : Get the version of kubernetes client and server

kubectl run hello-minikube –image=k8s.gcr.io/echoserver:1.10 –port=8080 kubectl expose deployment hello-minikube –type=NodePort kubectl get pod curl $(minikube service hello-minikube –url)

kubectl cluster-info kubectl cluster-info dump

minikube start minikube start –disk-size 2g –vm-driver=virtualbox –iso-url=“https://storage.googleapis.com/minikube/iso/minikube-v0.33.0.iso"

rm -rf ~/.minikube/machines

kubectl run hello-minikube –image=gcr.io/google_containers/echoserver:1.4 –port=8080 kubectl expose deployment hello-minikube4 –type=NodePort kubectl expose deployment hello-minikube4 –type=LoadBalancer –port=8080 –target-port=8080 –name=app-load-balancer

kubectl describe pod hello-minikube4

kubectl delete pods –all kubectl delete service –all

kubectl port-forword 5000:6000 kubectl exec -it bash kubectl label pods healthy=false kubectl run –image= –port=8080 kubectl scale –replicas=4 deployment/

kubectl get deployments kubectl rollout status deployment kubectl set image kubectl rollout history deployment/ kubectl rollout history deployment/ –revision=4

Health check Readiness Probe Liveness Probe

curl $(minikube service hello-minikube4 –url)


Viewing, Finding Resources

# Get commands with basic output
kubectl get services                          # List all services in the namespace
kubectl get pods --all-namespaces             # List all pods in all namespaces
kubectl get pods -o wide                      # List all pods in the namespace, with more details
kubectl get deployment my-dep                 # List a particular deployment
kubectl get pods --include-uninitialized      # List all pods in the namespace, including uninitialized ones

# Describe commands with verbose output
kubectl describe nodes my-node
kubectl describe pods my-pod

kubectl get services --sort-by=.metadata.name # List Services Sorted by Name

# List pods Sorted by Restart Count
kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'

# Get the version label of all pods with label app=cassandra
kubectl get pods --selector=app=cassandra rc -o \

# Get all worker nodes (use a selector to exclude results that have a label
# named 'node-role.kubernetes.io/master')
kubectl get node --selector='!node-role.kubernetes.io/master'

# Get all running pods in the namespace
kubectl get pods --field-selector=status.phase=Running

# Get ExternalIPs of all nodes
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'

# List Names of Pods that belong to Particular RC
# "jq" command useful for transformations that are too complex for jsonpath, it can be found at https://stedolan.github.io/jq/
sel=${$(kubectl get rc my-rc --output=json | jq -j '.spec.selector | to_entries | .[] | "\(.key)=\(.value),"')%?}
echo $(kubectl get pods --selector=$sel --output=jsonpath={.items..metadata.name})

# Show labels for all pods (or any other Kubernetes object that supports labelling)
# Also uses "jq"
for item in $( kubectl get pod --output=name); do printf "Labels for %s\n" "$item" | grep --color -E '[^/]+$' && kubectl get "$item" --output=json | jq -r -S '.metadata.labels | to_entries | .[] | " \(.key)=\(.value)"' 2>/dev/null; printf "\n"; done

# Check which nodes are ready
JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}' \
 && kubectl get nodes -o jsonpath="$JSONPATH" | grep "Ready=True"

# List all Secrets currently in use by a pod
kubectl get pods -o json | jq '.items[].spec.containers[].env[]?.valueFrom.secretKeyRef.name' | grep -v null | sort | uniq

# List Events sorted by timestamp
kubectl get events --sort-by=.metadata.creationTimestamp