Skip to content

K8S cheat sheet

Documentation

https://kubernetes.io/docs/reference/kubectl/cheatsheet/

ALL

kubectl get all --namespace=<namespace>

Namespace

kubectl get namespaces

Node

kubectl get nodes

Pods

kubectl get pods --namespace=dynatrace
kubectl get pods --all-namespaces

kubectl get pods -o wide --namespace=dynatrace
kubectl get pods -o wide --all-namespaces

kubectl get pods --show-labels --all-namespaces

kubectl describe pods <my-pod-name>

Containers (get images)

kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{"\nPod: "}{.metadata.name}{"  ==> Containers:  "}{range .spec.containers[*]}{.image}{" | "}{end}{end}' | sort
# output: 
#   Pod: <pod-name>  ==> Containers:  <container-image-1> | <container-image-2> | <container-image-3> | 
#   Pod: <pod-name>  ==> Containers:  <container-image-1> | <container-image-2> | <container-image-3> | 
#   Pod: <pod-name>  ==> Containers:  <container-image-1> | <container-image-2> | <container-image-3> | 

Containers (get name)

kubectl get pods --namespace=dynatrace -o jsonpath='{range .items[*]}{"\nPod: "}{.metadata.name}{"  ==> Containers:  "}{range .spec.containers[*]}{.name}{" | "}{end}{end}' | sort
# output: 
#   Pod: <pod-name>  ==> Containers:  <container-name-1> | <container-name-2> | <container-name-3> | 
#   Pod: <pod-name>  ==> Containers:  <container-name-1> | <container-name-2> | <container-name-3> | 
#   Pod: <pod-name>  ==> Containers:  <container-name-1> | <container-name-2> | <container-name-3> | 
kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{"\nPod: "}{.metadata.name}{"  ==> Containers:  "}{range .spec.containers[*]}{.name}{" | "}{end}{end}' | sort

Service

kubectl get services --all-namespaces
kubectl get services --namespace=dynatrace

Secret

kubectl get secret --all-namespaces
kubectl get secret <name> --namespace <namespace>
kubectl describe secret <name> --namespace <namespace>
kubectl get secret <name> --namespace <namespace> -o jsonpath='{.data}'

Logs

kubectl logs <pod-name> --namespace=dynatrace                         # dump
kubectl logs <pod-name> -c <container-name> --namespace=dynatrace     # dump

Shell in pod

kubectl exec --stdin --tty <pod-name> --namespace=<namespace-name> -- /bin/sh
kubectl exec --stdin --tty <pod-name> -c <container-name> --namespace=<namespace-name> -- /bin/sh

kubectl exec --stdin --tty <pod-name> --namespace=<namespace-name> -- /bin/bash
kubectl exec --stdin --tty <pod-name> -c <container-name> --namespace=<namespace-name> -- /bin/bash

Events

kubectl get events --all-namespaces