Kubectl Cheat Sheet                                         BY JESSICA CHERRY
kubectl is a powerful command-line tool to maintain your Kubernetes cluster. Here are
       commonly used commands to take you above and beyond average cluster administration.
                                                      Basic Commands
 kubectl get                                                      kubectl create
kubectl get <resource> --output wide                             kubectl create --filename ./pod.yaml
List all information about the select resource type.             Some resources require a name parameter. A small
Common resources include:                                        list of resources you can create include:
• Pods (kubectl get pods)
• Namespaces (kubectl get ns)                                    • Services (svc)
• Nodes (kubectl get node)                                       • Cronjobs (cj)
• Deployments (kubectl get deploy)                               • Deployments (deploy)
• Service (kubectl get svc)                                      • Quotas (quota)
• ReplicaSets (kubectl get rs)
Call resources using singular (pod), plural (pods),              See required parameters:
or with shortcuts.
                                                                 kubectl create cronjobs --help
Get pod by namespace:
-n, --namespace
Wait for a resource to finish:
                                                                  kubectl delete
-w, --watch
                                                                 kubectl delete <resource>
Query multiple resources (comma-separated values):
                                                                 Remove one more our resources by name, label, or
                                                                 by filename.
kubectl get rs,services -o wide
                                                                 If you want to delete pods by label in mass you have
                                                                 to describe the pod and gather the app=”name” from
                                                                 the label section. This makes it easier to cycle
                                                                 multiple containers at once.
 kubectl edit
kubectl edit <resource-type>/<name>
                                                                 Add --grace-period=5 to give yourself a few seconds
                                                                 to cancel before deleting:
Edit resources in a cluster. The default editor opens
unless KUBE_EDITOR is specificed:
                                                                 kubectl delete pod foo --grace-period=5
KUBE_EDITOR="nano" kubectl edit \
svc/container-registry
  opensource.com Twitter @opensourceway | facebook.com/opensourceway | CC BY-SA 4.0
  opensource.com: Kubectl Cheat Sheet                                                        BY JESSICA CHERRY
                                              Troubleshooting Commands
 kubectl describe                                            kubectl logs
kubectl describe <resource-type> <name>                     kubectl logs [-f] [-c] <resource-name>
                                                            [<pod-name>]
Show details of a resource. Often used to describe a
pod or node for errors in events, or whether resources      Helpful when an application is dead within a pod but
are too limited to use. A few common examples:              the pod and containers are shown as active.
kubectl describe pods/nginx                                 Follow a log as it is created:
                                                            -f, --follow
kubectl describe nodes container.proj
                                                            Get logs from a specific container:
kubectl describe pods -l name=myLabel                       -c, --container
                                                            kubectl logs -f -c ruby-app web-1
                                                Advanced Commands
 kubectl apply                                               kubectl cp
kubectl apply --file ./<filename>                           kubectl cp <source> <destination>
Apply configurations from files for resources within        Copy files and directories to and from containers. The
your cluster.                                               tar binary must be in the container. This can also be
                                                            used to pull or restore backups in an emergency. File
Use apply to add, update, or delete fields:                 location must be specified.
kubectl apply -f ./pod.json                                 Copy a file from a local machine to a container:
cat pod.json | kubectl apply -f -                           kubectl cp /tmp/cmd.txt \
                                                            charts/chart-884c-dmcfv:/tmp/cmd.txt
                                                            Copy file from container to local machine:
 kubectl exec
kubectl exec is the ability to execute into a container
                                                            kubectl cp \
that is having an issue but logging and debugging an
                                                            charts/chart-884c-dmcfv:/tmp/cmd.txt \
app hasn’t provided any answers.
                                                            /tmp/cmd.txt
  opensource.com Twitter @opensourceway | facebook.com/opensourceway | CC BY-SA 4.0