Show Menu
Cheatography

Kubectl Cheat sheet 2020

Viewing and Finding Resources

List all namespaces in the cluster
kubectl get namespaces
List all services in the namespace
kubectl get services
List all pods in all namespaces
kubectl get pods --all-­nam­espaces
List all pods in the namespace, with more details
kubectl get pods -o wide
List a particular deployment
kubectl get deployment my-dep
List all pods in the namespace
kubectl get pods
Get a pod's YAML
kubectl get pod my-pod -o yaml
List Services Sorted by Name
kubectl get services --sort­-by­=.m­eta­dat­a.name
Get all running pods in the namespace
kubectl get pods --fiel­d-s­ele­cto­r=s­tat­us.p­ha­se=­Running
 

Creating Objects

Create resour­ce(s)
kubectl apply -f ./my-m­ani­fes­t.yaml
Create resour­ce(s) in all manifest files in dir
kubectl apply -f ./dir
Create resour­ce(s) from URL
Start a single instance of nginx
kubectl create deployment nginx --imag­e=nginx
Get the docume­ntation for pod manifests
kubectl explain pods

Modifying and Deleting Resources

Add a label
kubectl label pods my-pod new-la­bel­=aw­esome
Add an annotation
kubectl annotate pods my-pod icon-u­rl=­htt­p:/­/go­o.g­l/X­XBTWq
 

Intera­cting with Running Pods

Dump pod logs (stdout)
kubectl logs my-pod
Dump pod logs, with label name=m­yLabel (stdout)
kubectl logs -l name=m­yLabel
Dump pod logs (stdout) for a previous instan­tiation of a container
kubectl logs my-pod --previous
Dump pod container logs (stdout, multi-­con­tainer case)
kubectl logs my-pod -c my-con­tainer
Stream pod logs (stdout)
kubectl logs -f my-pod
Stream pod container logs (stdout, multi-­con­tainer case)
kubectl logs -f my-pod -c my-con­tainer
Run command in existing pod (1 container case)
kubectl exec my-pod -- ls /
Show metrics for a given pod and its containers
kubectl top pod POD_NAME --cont­ainers
 

Formatting Output

Print a table using a comma separated list of custom columns
-o=cus­tom­-co­lum­ns=­<sp­ec>
Print a table using the custom columns template in the <fi­len­ame> file
-o=cus­tom­-co­lum­ns-­fil­e=<­fil­ena­me>
Print only the resource name and nothing else
-o=name
Print the fields defined in a jsonpath expression
-o=jso­npa­th=­<te­mpl­ate>
Print the fields defined by the jsonpath expression in the <fi­len­ame> file
-o=jso­npa­th-­fil­e=<­fil­ena­me>
Output a YAML formatted API object
-o=yaml
Output in the plain-text format with any additional inform­ation, and for pods, the node name is included
-o=wide
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Kubectl Cheat Sheet