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=ecorp
kubectl get pods --all-namespaces

kubectl get pods -o wide --namespace=ecorp
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=ecorp -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=ecorp

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=ecorp                         # dump
kubectl logs <pod-name> -c <container-name> --namespace=ecorp     # 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