The kubectl
command is how administrators interact with and manage a Kubernetes cluster on a Linux system. It is an essential command line tool that works with all Kubernetes cluster envrionments like Amazon Web Services, Google Cloud Platform, or a cluster on your own hardware (e.g., kubeadm). Managing Kubernetes clusters at scale can be a challenge at first, but mastering the kubectl
command will make it much easier.
In this tutorial, we will learn how to manage Kubernetes clusters with the kubectl
command. We will explore various command options and see how to effectively use kubectl, as well as some tips and tricks to make using kubectl easier.
In this tutorial you will learn:
- How to use
kubectl
commands against a Kubernetes cluster

Category | Requirements, Conventions or Software Version Used |
---|---|
System | Any Linux distro |
Software | Kubernetes, kubectl |
Other | 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 |
Manage Kubernetes Clusters With kubectl
The examples below show some of the most common and essential
kubectl
commands to use against a Kubernetes cluster in Linux.
In case you do not already have it, you will need to install kubectl separately from Kubernetes. The
kubectl
command will then work with any type of Kubernetes you have running, such as kubeadm or minikube, etc. - To apply a manifest file, which can also deploy an application in Kubernetes, we can use the following syntax:
$ kubectl apply -f ./application.yaml
This can deploy our custom Docker containerized application or one that we retrieve from a central repository, such as Docker Hub.
- We can also delete a deployment with the
delete
parameter. Here are some examples:$ kubectl delete deployment my-application
Or specify the file:
$ kubectl delete -f application.yaml
- Retrieve information about all of the Kubernetes pods in your network with:
$ kubectl get pods
Or the more verbose version, which will show pods in all name spaces:
$ kubectl get pods --all-namespaces
Showing all Kubernetes pods Note that the command option
--all-namespaces
can also be shortened to-A
:$ kubectl get pods -A
- See information about all the configured nodes in your Kubernetes cluster:
$ kubectl get nodes
- You can see a running list of all available services running in your cluster with the following command, issued from the Kubernetes maser node:
$ kubectl get svc
- Get details about a specific pod that has been deployed (in this example, one named ‘apache’):
$ kubectl get pods/apache
And more detailed information with the
describe
argument:$ kubectl describe pods/apache
- See the logs for a specific pod:
$ kubectl logs my-pod
- We can use the
run
parameter to begin running an image. Let’s use Nginx for example:$ kubectl run --image=nginx nginx-server --port=80 --env="DOMAIN=cluster"
We can then expose port 80:
$ kubectl expose deployment nginx-deployment --port=80 --name=nginx-http
- The
kubectl
command is also essential when scaling resources. To scale deployment ‘mysql’ to 5 replicas:$ kubectl scale --replicas=5 deployment/mysql
Or to scale resources as specified in a YAML file:
$ kubectl scale --replicas=5 -f mysql.yaml
- To drain all pods from a node (e.g., to perform maintenance on the node without it causing interruptions to the Kubernetes cluster):
$ kubectl drain worker-node
- To see all information about your Kubernetes cluster:
$ kubectl cluster-info
Closing Thoughts
In this tutorial, we saw how to use the kubectl
command to manage a Kubernetes cluster on a Linux system. Learning how to use kubectl is essential for administrators to create new deployments, pods, and manage nodes from the command line. It also allows us to inspect logs and see a current status of how the cluster and its individual components are operating at any given time.