Kubectl Cheat Sheet - Kubernetes
Kubectl Cheat Sheet - Kubernetes
Reference
HOMEGETTING STARTEDCONCEPTSTASKSTUTORIALSREFERENCECONTRIBUTE
Search
Kubectl Autocomplete
Kubectl Context and Con guration
Apply
Creating Objects
Viewing, Finding Resources
Updating Resources
Patching Resources
Editing Resources
Scaling Resources
Deleting Resources
Interacting with running Pods
Interacting with Nodes and Cluster
What's next
Kubectl Autocomplete
BASH
https://kubernetes.io/docs/reference/kubectl/cheatsheet/#updating-resources 1/11
9/13/2019 kubectl Cheat Sheet - Kubernetes
source <(kubectl completion bash) # setup autocomplete in bash into the current sh
echo "source <(kubectl completion bash)" >> ~/.bashrc # add autocomplete permanent
You can also use a shorthand alias for kubectl that also works with completion:
alias k=kubectl
complete -F __start_kubectl k
ZSH
source <(kubectl completion zsh) # setup autocomplete in zsh into the current she
echo "if [ $commands[kubectl] ]; then source <(kubectl completion zsh); fi" >> ~/.
Set which Kubernetes cluster kubectl communicates with and modi es con guration information.
See Authenticating Across Clusters with kubecon g documentation for detailed con g le
information.
https://kubernetes.io/docs/reference/kubectl/cheatsheet/#updating-resources 2/11
9/13/2019 kubectl Cheat Sheet - Kubernetes
# use multiple kubeconfig files at the same time and view merged config
KUBECONFIG=~/.kube/config:~/.kube/kubconfig2
# permanently save the namespace for all subsequent kubectl commands in that conte
kubectl config set-context --current --namespace=ggckad-s2
Apply
apply manages applications through les de ning 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 de ned in json or yaml. The le extension .yaml , .yml , and .json
can be used.
https://kubernetes.io/docs/reference/kubectl/cheatsheet/#updating-resources 3/11
9/13/2019 kubectl Cheat Sheet - Kubernetes
# Get all worker nodes (use a selector to exclude results that have a label
# named 'node-role.kubernetes.io/master')
kubectl get node --selector='!node-role.kubernetes.io/master'
# Show labels for all pods (or any other Kubernetes object that supports labelling
# Also uses "jq"
for item in $( kubectl get pod --output=name); do printf "Labels for %s\n" "$item"
# Or this command can be used as well to get all the labels associated with pods
kubectl get pods --show-labels
Updating Resources
# Force replace, delete and then re-create the resource. Will cause a service outa
kubectl replace --force -f ./pod.json
# Create a service for a replicated nginx, which serves on port 80 and connects to
kubectl expose rc nginx --port=80 --target-port=8000
Patching Resources
https://kubernetes.io/docs/reference/kubectl/cheatsheet/#updating-resources 6/11
9/13/2019 kubectl Cheat Sheet - Kubernetes
Editing Resources
Scaling Resources
Deleting Resources
https://kubernetes.io/docs/reference/kubectl/cheatsheet/#updating-resources 7/11
9/13/2019 kubectl Cheat Sheet - Kubernetes
# If a taint with that key and effect already exists, its value is replaced as spe
kubectl taint nodes foo dedicated=special-user:NoSchedule
https://kubernetes.io/docs/reference/kubectl/cheatsheet/#updating-resources 8/11
9/13/2019 kubectl Cheat Sheet - Kubernetes
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 speci c format, you can add either the -o or
-o=custom-columns-file=
Print a table using the custom columns template in the <filename> le
<filename>
-o=jsonpath-file=
Print the elds de ned by the jsonpath expression in the <filename> le
<filename>
Output in the plain-text format with any additional information, and for pods, the
-o=wide node name is included
https://kubernetes.io/docs/reference/kubectl/cheatsheet/#updating-resources 9/11
9/13/2019 kubectl Cheat Sheet - Kubernetes
Verbosity Description
Useful steady state information about the service and important log messages that may correlate to signi cant
--v=2 changes in the system. This is the recommended default log level for most systems.
What's next
Feedback
Yes No
https://kubernetes.io/docs/reference/kubectl/cheatsheet/#updating-resources 10/11
9/13/2019 kubectl Cheat Sheet - Kubernetes
Page last modi ed on August 16, 2019 at 4:36 AM PST by add rollout history and back to speci c
revision (#15818) (Page History)
Home
Blog
Partners
Community
Case Studies
Contribute
https://kubernetes.io/docs/reference/kubectl/cheatsheet/#updating-resources 11/11