This article contains a list of commonly used kubectl
commands and flags which we can use as Cheat sheet for Kubernetes.
Introduction
Kubectl is the command line configuration tool for Kubernetes that communicates with a Kubernetes API server. Using Kubectl allows you to create, inspect, update, and delete Kubernetes objects. This cheatsheet will serve as a quick reference to make commands on many common Kubernetes components and resources.
Category Based Cheat Sheet
Lets see how we can use the full command for an object on things like pod(s) or the shortcode variation mentioned in parantheses in the heading of each section.
Cluster Management
Display endpoint information about the master and services in the cluster
kubectl cluster-info
Display the Kubernetes version running on the client and server
kubectl version
Fetching configuration of the cluster
kubectl config view
To view available API resources
kubectl api-resources
To list API version available
kubectl api-versions
To list everything
kubectl get all --all-namespaces
Daemonsets
Shortcode = ds
List one or more daemonsets
kubectl get daemonset
Edit and update the definition of one or more daemonset
kubectl edit daemonset
Delete a daemonset
kubectl delete daemonset
Create a new daemonset
kubectl create daemonset
Manage the rollout of a daemonset
kubectl rollout daemonset
Display the detailed state of daemonsets within a namespace
kubectl describe ds -n
Deployments
Shortcode = deploy
List one or more deployments
kubectl get deployment
Display the detailed state of one or more deployments
kubectl describe deployment
Edit and update the definition of one or more deployment on the server
kubectl edit deployment
Create one a new deployment
kubectl create deployment
Delete deployments
kubectl delete deployment
See the rollout status of a deployment
kubectl rollout status deployment
Events
Shortcode = ev
List recent events for all resources in the system
kubectl get events
List Warnings only
kubectl get events --field-selector type=Warning
List events but exclude Pod events
kubectl get events --field-selector involvedObject.kind!=Pod
Pull events for a single node with a specific name
kubectl get events --field-selector involvedObject.kind=Node, involvedObject.name=
Filter out normal events from a list of events
kubectl get events --field-selector type!=Normal
Logs
Print the logs for a pod
kubectl logs
Print the logs for the last hour for a pod
kubectl logs --since=1h
Get the most recent 20 lines of logs
kubectl logs --tail=20
Get logs from a service and optionally select which container
kubectl logs -f [-c <$container>]
Print the logs for a pod and follow new logs
kubectl logs -f
Print the logs for a container in a pod
kubectl logs -c
Output the logs for a pod into a file named ‘pod.log’
kubectl logs pod.log
View the logs for a previously failed pod
kubectl logs --previous
Manifest Files
Another option for modifying objects is through Manifest Files. We highly recommend using this method. It is done by using yaml files with all the necessary options for objects configured. We have our yaml files stored in a git repository, so we can track changes and streamline changes.
Apply a configuration to an object by filename or stdin. Overrides the existing configuration.
kubectl apply -f manifest_file.yaml
Create objects
kubectl create -f manifest_file.yaml
Create objects in all manifest files in a directory
kubectl create -f ./dir
Create objects from a URL
kubectl create -f ‘url’
Delete an object
kubectl delete -f manifest_file.yaml
Namespaces
Shortcode = ns
Create namespace
kubectl create namespace
List one or more namespaces
kubectl get namespace
Display the detailed state of one or more namespace
kubectl describe namespace
Delete a namespace
kubectl delete namespace
Edit and update the definition of a namespace
kubectl edit namespace
Display Resource (CPU/Memory/Storage) usage for a namespace
kubectl top namespace
Nodes
Shortcode = no
Update the taints on one or more nodes
kubectl taint node
List one or more nodes
kubectl get node
Delete a node or multiple nodes
kubectl delete node
Display Resource usage (CPU/Memory/Storage) for nodes
kubectl top node
Resource allocation per node
kubectl describe nodes | grep Allocated -A 5
Pods running on a node
kubectl get pods -o wide | grep
Annotate a node
kubectl annotate node
Mark a node as unschedulable
kubectl cordon node
Mark node as schedulable
kubectl uncordon node
Drain a node in preparation for maintenance
kubectl drain node
Add or update the labels of one or more nodes
kubectl label node
Pods
Shortcode = po
List one or more pods
kubectl get pod
Delete a pod
kubectl delete pod
Display the detailed state of a pods
kubectl describe pod
Create a pod
kubectl create pod
Execute a command against a container in a pod
kubectl exec -c
Get interactive shell on a a single-container pod
kubectl exec -it /bin/sh
Display Resource usage (CPU/Memory/Storage) for pods
kubectl top pod
Add or update the annotations of a pod
kubectl annotate pod
Add or update the label of a pod
kubectl label pod
Replication Controllers
Shortcode = rc
List the replication controllers
kubectl get rc
List the replication controllers by namespace
kubectl get rc --namespace=””
ReplicaSets
Shortcode = rs
List ReplicaSets
kubectl get replicasets
Display the detailed state of one or more ReplicaSets
kubectl describe replicasets
Scale a ReplicaSet
kubectl scale --replicas=[x]
Secrets
Create a secret
kubectl create secret
List secrets
kubectl get secrets
List details about secrets
kubectl describe secrets
Delete a secret
kubectl delete secret
Services
Shortcode = svc
List one or more services
kubectl get services
Display the detailed state of a service
kubectl describe services
Expose a replication controller, service, deployment or pod as a new Kubernetes service
kubectl expose deployment [deployment_name]
Edit and update the definition of one or more services
kubectl edit services
Service Accounts
Shortcode = sa
List service accounts
kubectl get serviceaccounts
Display the detailed state of one or more service accounts
kubectl describe serviceaccounts
Replace a service account
kubectl replace serviceaccount
Delete a service account
kubectl delete serviceaccount
StatefulSet
Shortcode = sts
List StatefulSet
kubectl get statefulset
Delete StatefulSet only (not pods)
kubectl delete statefulset/[stateful_set_name] --cascade=false
Common Options
In Kubectl you can specify optional flags with commands. Here are some of the most common and useful ones.
-o Output format. For example if you wanted to list all of the pods in ps output format with more information.
kubectl get pods -o wide
-n Shorthand for –namespace. For example, if you’d like to list all the Pods in a specific Namespace you would do this command:
kubectl get pods --namespace=[namespace_name]
kubectl get pods -n=[namespace_name]
-f Filename, directory, or URL to files to use to create a resource. For example when creating a pod using data in a file named newpod.json.
kubectl create -f ./newpod.json
Conclusion
We hope that we have extensively covered most of the aspect of the Kubectl command that can be helpful for daily operations while handling the Kubernetes cluster. However if you need more details you can always refer the Official Kubernetes Documentation