~/cheatsheets (main) $ kubectl --help

kubectl cheat sheet

kubectl is the command line client for Kubernetes: it talks to the cluster in your current context to inspect, create and change resources. These are the commands you reach for every day, grouped by task. Switch to the k alias and every command updates. Click a command to copy it.

alias
print

kubectl setup

4 cmds

# Shell completion and the k alias. Add these lines to ~/.bashrc to keep them.

Turn on bash completion
Turn on zsh completion
Use k as an alias, with completion
Show client and server versions

kubectl config and contexts

7 cmds

# Clusters, contexts and the active namespace.

Show the merged kubeconfig
List contexts
Show the current context
Switch to another context
Set the default namespace
Create a context for a namespace
Show cluster endpoints

kubectl get and inspect

14 cmds

# List and inspect resources.

List pods in the current namespace
List pods in every namespace
Show node and IP for each pod
Watch pods change live
List pods by label
List services, endpoints and ingresses
List the common resources
Print a resource as YAML or JSON
Print one field with JSONPath
Print chosen columns
Sort pods by age
List only running pods
List every resource type
Explain a field of a resource

kubectl pods, logs and debugging

17 cmds

# Logs, shells, port forwards and debugging.

Describe a pod and its events
Print pod logs
Stream pod logs
Logs from the last hour, last 100 lines
Logs from one container
Logs from the previous crash
Logs from every pod with a label
Open a shell in a pod
Forward a local port to a pod
Forward a local port to a service
Copy a file out of a pod
Start a throwaway debug pod
Debug a running pod with an ephemeral container
Wait until a pod is ready
Show pod CPU and memory (needs metrics-server)
Delete a pod
Force delete a stuck pod

kubectl deployments and rollouts

10 cmds

# Scale, update and roll back workloads.

Create a deployment
Scale a deployment
Update the container image
Watch a rollout finish
Show rollout history
Roll back to the previous version
Roll back to a specific revision
Restart every pod in a deployment
Autoscale on CPU
Expose a deployment as a service

kubectl jobs and CronJobs

5 cmds

# One-off and scheduled work.

Run a one-off job
Create a CronJob
Run a CronJob now
List jobs and CronJobs
Wait for a job to finish

kubectl apply and edit

9 cmds

# Create and change resources from manifests.

Apply a manifest
Apply a kustomize directory
Preview changes before applying
Delete what a manifest created
Edit a live resource
Generate YAML without creating
Add a label
Add an annotation
Patch a field

kubectl RBAC

5 cmds

# Check and grant permissions.

Check if you can do something
List everything you can do
Check as a service account
Create a role
Bind a role to a user

kubectl nodes and cluster

7 cmds

# Cluster capacity and maintenance.

List nodes
Show node CPU and memory (needs metrics-server)
Stop scheduling on a node
Evict pods before maintenance
Allow scheduling again
Taint a node
Recent events, newest last

kubectl secrets, ConfigMaps and namespaces

5 cmds

# Configuration and isolation.

Create a secret from a value
Read and decode a secret
Create a ConfigMap from a file
Create a namespace
List namespaces

kubectl short names

14 cmds

# Type less. Every resource has a short name.

pods
po
services
svc
deployments
deploy
replicasets
rs
statefulsets
sts
daemonsets
ds
cronjobs
cj
namespaces
ns
nodes
no
configmaps
cm
persistentvolumeclaims
pvc
ingresses
ing
horizontalpodautoscalers
hpa
serviceaccounts
sa