The kubectl command is the primary way of interacting with your Kubernetes cluster. It is safe to say that Linux administrators will be using the command on a constant basis when they are in charge of a Kubernetes environment. However, due to the sheer amount of command options available, it can be intimidating for newcomers and tricky to learn. Even longtime users may forget a command every once in a while and that is why we have created this tutorial full of kubectl command examples.
For times like these, it is very handy to have a compiled list of kubectl commands that have been sorted by category. That way, it only takes a few moments to reference the list whenever you forget the exact syntax of a command. In this tutorial, we will present you with a curated list of the most handy kubectl commands. These are some of the most useful commands, but they aren’t easy to remember for everyone. Next time your mind is blanking at a Linux terminal, take a look at the kubectl commands cheat sheet below for some quick help.
In this tutorial you will learn:
kubectl command examples and cheat sheet
kubectl command examples (cheat sheet)
Software Requirements and Linux Command Line Conventions
Category
Requirements, Conventions or Software Version Used
Privileged access to your Linux system as root or via the sudo command.
Conventions
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command $ – requires given linux commands to be executed as a regular non-privileged user
Kubernetes Information
Command
Description
kubectl version
See version of Kubernetes API server and version of kubectl CLI
kubectl config view
Show configuration settings for Kubernetes server in YAML format
kubectl help
See a list of kubectl commands
Managing Pods
Command
Description
kubectl get pods
See a list of all pods in the cluster
kubectl create -f pod.yaml
Create a new pod as outlined in a YAML file
kubectl delete pod my-pod
Delete a pod named my-pod
kubectl logs my-pod
See the latest log entries for pod my-pod
kubectl exec my-pod -- whoami
Run the command whoami inside pod ‘my-pod’ (if it has a single container)
kubectl exec my-pod -c my-container -- whoami
Run the command whoami inside container ‘my-container’ on the pod ‘my-pod’
Creates a new service listening on port 80 and mapped to port 8080
kubectl delete service my-service
Delete a service named ‘my-service’
kubectl describe service my-service
See detailed information about the service ‘my-service’
Managing Deployments
Command
Description
kubectl get deployments
See a list of all deployments in the cluster
kubectl get deployment my-dep
See information about a deployment named ‘my-dep’
kubectl create deployment nginx --image=nginx
Create a new deployment of nginx image
kubectl logs deploy/my-dep
See all logs for deployment ‘my-dep’
kubectl scale deployment my-dep --replicas=4
Scale a deployment ‘my-dep’ to 4 replicas
Namespace Management
Command
Description
kubectl get namespaces
See a list of all namespaces in the cluster
kubectl create namespace my-ns
Create a new namespace called ‘my-ns’
kubectl delete namespace my-ns
Delete a namespace called ‘my-ns’
Closing Thoughts
Feel free to reference this cheat sheet any time that you need a quick refresher. The goal here is to save you as much time as possible when trying to remember a certain kubectl command syntax. We have only just scratched the surface of kubectl commands, but these are some of the most commonly used that you will find yourself typing on a regular basis.