Kubernetes Cheat Sheet - TheLearnLoop/ExploringKubernetes GitHub Wiki

// Kubectl auto complete and alias:

source <(kubectl completion bash)

echo "source <(kubectl completion bash)" >> ~/.bashrc

alias k=kubectl

complete -o default -F __start_kubectl k

// All namespaces:

kubectl -A

kubectl --all-namespaces

// Basic commands:

kubectl get pods --all-namespaces # List all pods in all namespaces

kubectl get pods -o wide # List all pods in the current namespace, with more details

kubectl get pod my-pod -o yaml # Get a pod's YAML

Describe commands with verbose output

kubectl describe nodes my-node

kubectl describe pods my-pod

// List all warning events

kubectl events --types=Warning

// Copy from local dir to remote pod

kubectl cp /tmp/foo_dir my-pod:/tmp/bar_dir # Copy /tmp/foo_dir local directory to /tmp/bar_dir in a remote pod in the current namespace

kubectl cp /tmp/foo my-pod:/tmp/bar -c my-container # Copy /tmp/foo local file to /tmp/bar in a remote pod in a specific container

kubectl cp /tmp/foo my-namespace/my-pod:/tmp/bar # Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace my-namespace

kubectl cp my-namespace/my-pod:/tmp/foo /tmp/bar # Copy /tmp/foo from a remote pod to /tmp/bar locally

// logs:

// Print the logs for a pod

kubectl logs <pod_name>

// Print the logs for the last hour for a pod

kubectl logs --since=1h <pod_name>

// Get the most recent 20 lines of logs

kubectl logs --tail=20 <pod_name>

// Print the logs for a pod and follow new logs

kubectl logs -f <pod_name>

// Print the logs for a container in a pod

kubectl logs -c <container_name> <pod_name>

// Output the logs for a pod into a file named ‘pod.log’

kubectl logs <pod_name> pod.log

// View the logs for a previously failed pod

kubectl logs --previous <pod_name>

// Get all images for a k8s release:

[opc@control ~]$ kubeadm config images list --kubernetes-version stable-1.26

registry.k8s.io/kube-apiserver:v1.26.11

registry.k8s.io/kube-controller-manager:v1.26.11

registry.k8s.io/kube-scheduler:v1.26.11

registry.k8s.io/kube-proxy:v1.26.11

registry.k8s.io/pause:3.9

registry.k8s.io/etcd:3.5.6-0

registry.k8s.io/coredns/coredns:v1.9.3

// With no proxy you get list of local images:

[opc@control ~]$ kubeadm config images list --kubernetes-version stable-1.26

W1206 12:40:37.312440 1131706 version.go:104] could not fetch a Kubernetes version from the internet: unable to get URL "https://dl.k8s.io/release/stable-1.26.txt": Get "https://dl.k8s.io/release/stable-1.26.txt": dial tcp xxxx: i/o timeout (Client.Timeout exceeded while awaiting headers)

W1206 12:40:37.313048 1131706 version.go:105] falling back to the local client version: v1.26.6

registry.k8s.io/kube-apiserver:v1.26.6

registry.k8s.io/kube-controller-manager:v1.26.6

registry.k8s.io/kube-scheduler:v1.26.6

registry.k8s.io/kube-proxy:v1.26.6

registry.k8s.io/pause:3.9

registry.k8s.io/etcd:3.5.6-0

registry.k8s.io/coredns/coredns:v1.9.3

// Get resource allocation per Node:

[opc@control ~]$ kubectl describe node | grep Allocated -A 11

Allocated resources:

(Total limits may be over 100 percent, i.e., overcommitted.)

Resource Requests Limits

-------- -------- ------

cpu 950m (23%) 100m (2%)

memory 290Mi (1%) 390Mi (2%)

ephemeral-storage 0 (0%) 0 (0%)

hugepages-2Mi 0 (0%) 0 (0%)

Events: <none>

...

// Get detailed information of nodes in k8s cluster:

kubectl get node -o wide

// Display Resource usage (CPU/Memory/Storage) for nodes

kubectl top node

// Get the container runtime used in all nodes of cluster:

[opc@control ~]$ kubectl describe node | grep -i runtime

Container Runtime Version: cri-o://1.26.3

Container Runtime Version: cri-o://1.26.3

Container Runtime Version: cri-o://1.26.3

// Set a custom namespace as current namespace:

k config set-context --current --namespace=sample-domain1-ns

// View labels and selectors

k get pods --show-labels

// Get a list of all pod phases in your cluster

kubectl get pod -A --no-headers |awk '{arr[$4]++}END{for (a in arr) print a, arr[a]}'

⚠️ **GitHub.com Fallback** ⚠️