Kubernetes Cheatsheet Kubectl Quick Reference
Kubernetes Cheatsheet Kubectl Quick Reference
Note:
These instructions are for Kubernetes v1.30. To check the version, use the kubectl version command.
Kubectl autocomplete
BASH
source <(kubectl completion bash) # set up autocomplete in bash into the current shell, bash-completion package should be inst
echo "source <(kubectl completion bash)" >> ~/.bashrc # add autocomplete permanently to your bash shell.
You can also use a shorthand alias for kubectl that also works with completion:
alias k=kubectl
complete -o default -F __start_kubectl k
ZSH
source <(kubectl completion zsh) # set up autocomplete in zsh into the current shell
echo '[[ $commands[kubectl] ]] && source <(kubectl completion zsh)' >> ~/.zshrc # add autocomplete permanently to your zsh she
FISH
Note:
Requires kubectl version 1.23 or above.
echo 'kubectl completion fish | source' > ~/.config/fish/completions/kubectl.fish && source ~/.config/fish/completions/kubectl
A note on --all-namespaces
Appending --all-namespaces happens frequently enough that you should be aware of the shorthand for --all-namespaces :
kubectl -A
https://kubernetes.io/docs/reference/kubectl/quick-reference/ 1/10
8/3/24, 10:29 AM kubectl Quick Reference | Kubernetes
# use multiple kubeconfig files at the same time and view merged config
KUBECONFIG=~/.kube/config:~/.kube/kubconfig2
# Show merged kubeconfig settings and raw certificate data and exposed secrets
kubectl config view --raw
# configure the URL to a proxy server to use for requests made by this client in the kubeconfig
kubectl config set-cluster my-cluster-name --proxy-url=my-proxy-url
# permanently save the namespace for all subsequent kubectl commands in that context.
kubectl config set-context --current --namespace=ggckad-s2
# short alias to set/show context/namespace (only works for bash and bash-compatible shells, current context to be set before
alias kx='f() { [ "$1" ] && kubectl config use-context $1 || kubectl config current-context ; } ; f'
alias kn='f() { [ "$1" ] && kubectl config set-context --current --namespace $1 || kubectl config view --minify | grep namespa
Kubectl apply
applymanages applications through files defining Kubernetes resources. It creates and updates resources in a cluster through
running kubectl apply . This is the recommended way of managing Kubernetes applications on production. See Kubectl Book.
Creating objects
Kubernetes manifests can be defined in YAML or JSON. The file extension .yaml , .yml , and .json can be used.
https://kubernetes.io/docs/reference/kubectl/quick-reference/ 2/10
8/3/24, 10:29 AM kubectl Quick Reference | Kubernetes
https://kubernetes.io/docs/reference/kubectl/quick-reference/ 3/10
8/3/24, 10:29 AM kubectl Quick Reference | Kubernetes
# Get all worker nodes (use a selector to exclude results that have a label
# named 'node-role.kubernetes.io/control-plane')
kubectl get node --selector='!node-role.kubernetes.io/control-plane'
# Show labels for all pods (or any other Kubernetes object that supports labelling)
kubectl get pods --show-labels
# Compares the current state of the cluster against the state that the cluster would be in if the manifest was applied.
kubectl diff -f ./my-manifest.yaml
# Produce ENV for all pods, assuming you have a default container for the pods, default namespace and the `env` command is sup
# Helpful when running any supported command across all pods, not just `env`
for pod in $(kubectl get po --output=jsonpath={.items..metadata.name}); do echo $pod && kubectl exec -it $pod -- env; done
Updating resources
kubectl set image deployment/frontend www=image:v2 # Rolling update "www" containers of "frontend" deployment, u
kubectl rollout history deployment/frontend # Check the history of deployments including the revision
kubectl rollout undo deployment/frontend # Rollback to the previous deployment
kubectl rollout undo deployment/frontend --to-revision=2 # Rollback to a specific revision
kubectl rollout status -w deployment/frontend # Watch rolling update status of "frontend" deployment until
kubectl rollout restart deployment/frontend # Rolling restart of the "frontend" deployment
cat pod.json | kubectl replace -f - # Replace a pod based on the JSON passed into stdin
# Force replace, delete and then re-create the resource. Will cause a service outage.
kubectl replace --force -f ./pod.json
# Create a service for a replicated nginx, which serves on port 80 and connects to the containers on port 8000
kubectl expose rc nginx --port=80 --target-port=8000
Patching resources
https://kubernetes.io/docs/reference/kubectl/quick-reference/ 5/10
8/3/24, 10:29 AM kubectl Quick Reference | Kubernetes
Editing resources
Edit any API resource in your preferred editor.
Scaling resources
kubectl scale --replicas=3 rs/foo # Scale a replicaset named 'foo' to 3
kubectl scale --replicas=3 -f foo.yaml # Scale a resource specified in "foo.yaml" to 3
kubectl scale --current-replicas=2 --replicas=3 deployment/mysql # If the deployment named mysql's current size is 2, scale m
kubectl scale --replicas=5 rc/foo rc/bar rc/baz # Scale multiple replication controllers
Deleting resources
kubectl delete -f ./pod.json # Delete a pod using the type and name specified in pod.json
kubectl delete pod unwanted --now # Delete a pod with no grace period
kubectl delete pod,service baz foo # Delete pods and services with same names "baz" and "foo"
kubectl delete pods,services -l name=myLabel # Delete pods and services with label name=myLabel
kubectl -n my-ns delete pod,svc --all # Delete all pods and services in namespace my-ns,
# Delete all pods matching the awk pattern1 or pattern2
kubectl get pods -n mynamespace --no-headers=true | awk '/pattern1|pattern2/{print $1}' | xargs kubectl delete -n mynamespac
https://kubernetes.io/docs/reference/kubectl/quick-reference/ 6/10
8/3/24, 10:29 AM kubectl Quick Reference | Kubernetes
Note:
kubectl cp requires that the 'tar' binary is present in your container image. If 'tar' is not present, kubectl cp will fail. For advanced
use cases, such as symlinks, wildcard expansion or file mode preservation consider using kubectl exec.
tar cf - /tmp/foo | kubectl exec -i -n my-namespace my-pod -- tar xf - -C /tmp/bar # Copy /tmp/foo local file to /tm
kubectl exec -n my-namespace my-pod -- tar cf - /tmp/foo | tar xf - -C /tmp/bar # Copy /tmp/foo from a remote pod to /tmp/b
kubectl port-forward svc/my-service 5000 # listen on local port 5000 and forward to port 5000 on Service back
kubectl port-forward svc/my-service 5000:my-service-port # listen on local port 5000 and forward to Service target port with
kubectl port-forward deploy/my-deployment 5000:6000 # listen on local port 5000 and forward to port 6000 on a Pod create
kubectl exec deploy/my-deployment -- ls # run command in first Pod and first container in Deployment (single
# If a taint with that key and effect already exists, its value is replaced as specified.
kubectl taint nodes foo dedicated=special-user:NoSchedule
Resource types
List all supported resource types along with their shortnames, API group, whether they are namespaced, and kind:
kubectl api-resources
Formatting output
To output details to your terminal window in a specific format, add the -o (or --output ) flag to a supported kubectl command.
-o=custom-columns-file= Print a table using the custom columns template in the <filename> file
<filename>
-o=go-template-file= Print the fields defined by the golang template in the <filename> file
<filename>
-o=jsonpath-file=<filename> Print the fields defined by the jsonpath expression in the <filename> file
-o=wide Output in the plain-text format with any additional information, and for pods, the node
name is included
https://kubernetes.io/docs/reference/kubectl/quick-reference/ 8/10
8/3/24, 10:29 AM kubectl Quick Reference | Kubernetes
Verbosity Description
--v=2 Useful steady state information about the service and important log messages that may correlate to significant
changes in the system. This is the recommended default log level for most systems.
What's next
Read the kubectl overview and learn about JsonPath.
Also read kubectl Usage Conventions to understand how to use kubectl in reusable scripts.
https://kubernetes.io/docs/reference/kubectl/quick-reference/ 9/10
8/3/24, 10:29 AM kubectl Quick Reference | Kubernetes
Feedback
Was this page helpful?
Yes No
https://kubernetes.io/docs/reference/kubectl/quick-reference/ 10/10