Kubectl is a command-line tool that helps us interact with our Kubernetes API server. In turn, it helps us manage our K8s cluster.
In this quick tutorial, we’ll learn and explore some commonly used kubectl commands.
Let’s look at kubectl‘s general syntax:
kubectl [operation] [resource-type] [resource-name] [flags]
When we don’t specify the resource name, it considers all resources of the specified type.
And so, when using kubectl, there are three main things to deal with:
Some of the basic operations supported by kubectl includes:
K8s introduces a variety of resources against which we can perform operations. Some of the common resource types include:
We don’t need to remember all of the K8s resource names or their aliases. We can easily list them out using api-resources:
For the kubectl operations that produce a result, we can customize the output format using one of the following flags:
Now that we understand the basics, let’s try out some kubectl commands:
#creates or updates the given resource specified in manifest
kubectl apply -f my-deployment.yaml
#lists all pods
kubectl get pods
#lists all services
kubectl get svc
#displays mypod information in yaml format
kubectl get pods mypod -o yaml
#prints some basic cluster information
#prints detailed information of the cluster nodes
kubectl get nodes -o wide
#lists all resources in the default namespace
kubectl get all
#lists all services in a specific namespace
kubectl get svc -ns my-namespace
#explains a pod resource type in detail
kubectl explain pod
#gives detailed information of our resource
kubectl describe nodes my-cluster-node1
#dumps pod logs
kubectl logs my-pod1
We can get help with kubectl usage with its help command:
We can stream the output using less for better readability:
kubectl -h | less
If we want help with a specific command, we can also have something like:
#helps us by presenting information about describe command
kubectl describe -h | less
In this quick tutorial, we learned how to interact with our K8s cluster using kubectl utility. We also covered some common useful commands.
I’ll recommend executing the example commands at Play With Kubernetes to get a better hang of it.