Kubectl Cheat Sheet - Kubernetes - Io
Kubectl Cheat Sheet - Kubernetes - Io
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
https://kubernetes.io/docs/reference/kubectl/cheatsheet/ 1/16
08/02/2020 kubectl Cheat Sheet - Kubernetes
Kubectl Autocomplete
BASH
source <(kubectl completion bash) # setup autocomplete in bash into the current shell, bash-completion package should
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 -F __start_kubectl k
ZSH
source <(kubectl completion zsh) # setup autocomplete in zsh into the current shell
echo "if [ $commands[kubectl] ]; then source <(kubectl completion zsh); fi" >> ~/.zshrc # add autocomplete permanently
https://kubernetes.io/docs/reference/kubectl/cheatsheet/ 2/16
08/02/2020 kubectl Cheat Sheet - Kubernetes
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.
# 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 context.
kubectl config set-context --current --namespace=ggckad-s2
https://kubernetes.io/docs/reference/kubectl/cheatsheet/ 3/16
08/02/2020 kubectl Cheat Sheet - Kubernetes
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 YAML or JSON. The le extension .yaml , .yml , and .json can be used.
---
apiVersion: v1
kind: Pod
metadata:
name: busybox-sleep-less
spec:
containers:
- name: busybox
image: busybox
args:
- sleep
- "1000"
EOF
# 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)
kubectl get pods --show-labels
https://kubernetes.io/docs/reference/kubectl/cheatsheet/ 6/16
08/02/2020 kubectl Cheat Sheet - Kubernetes
# 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
Updating Resources
As of version 1.11 rolling-update have been deprecated (see CHANGELOG-1.11.md), use rollout instead.
https://kubernetes.io/docs/reference/kubectl/cheatsheet/ 7/16
08/02/2020 kubectl Cheat Sheet - Kubernetes
kubectl set image deployment/frontend www=image:v2 # Rolling update "www" containers of "frontend" deplo
kubectl rollout history deployment/frontend # Check the history of deployments including the revi
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" deploymen
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 std
# 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/cheatsheet/ 8/16
08/02/2020 kubectl Cheat Sheet - Kubernetes
Editing Resources
Scaling Resources
https://kubernetes.io/docs/reference/kubectl/cheatsheet/ 9/16
08/02/2020 kubectl Cheat Sheet - Kubernetes
Deleting Resources
kubectl delete -f ./pod.json # Delete a pod using the type and name speci
kubectl delete pod,service baz foo # Delete pods and services with same names "
kubectl delete pods,services -l name=myLabel # Delete pods and services with label name=m
kubectl -n my-ns delete pod,svc --all # Delete all pods and services in namespace
# 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 my
https://kubernetes.io/docs/reference/kubectl/cheatsheet/ 10/16
08/02/2020 kubectl Cheat Sheet - Kubernetes
https://kubernetes.io/docs/reference/kubectl/cheatsheet/ 11/16
08/02/2020 kubectl Cheat Sheet - Kubernetes
# 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
https://kubernetes.io/docs/reference/kubectl/cheatsheet/ 12/16
08/02/2020 kubectl Cheat Sheet - Kubernetes
To output details to your terminal window in a speci c format, add the -o (or --output ) ag to a supported kubectl command.
-o=custom-columns-file=<filename> Print a table using the custom columns template in the <filename> le
-o=jsonpath-file=<filename> Print the elds de ned by the jsonpath expression in the <filename> le
-o=wide Output in the plain-text format with any additional information, and for pods, the node name is included
Verbosity Description
https://kubernetes.io/docs/reference/kubectl/cheatsheet/ 13/16
08/02/2020 kubectl Cheat Sheet - Kubernetes
Verbosity Description
Useful steady state information about the service and important log messages that may correlate to signi cant changes in the system. This is the recommended default log
--v=2 level for most systems.
What's next
Feedback
https://kubernetes.io/docs/reference/kubectl/cheatsheet/ 14/16
08/02/2020 kubectl Cheat Sheet - Kubernetes
Yes No
Page last modi ed on February 08, 2020 at 1:15 AM PST by Update cheatsheet.md (#18975) (Page History)
Home
Blog
Partners
Community
Case Studies
Contribute
https://kubernetes.io/docs/reference/kubectl/cheatsheet/ 15/16
08/02/2020 kubectl Cheat Sheet - Kubernetes
https://kubernetes.io/docs/reference/kubectl/cheatsheet/ 16/16