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.
Working with kubectl:
Let’s look at kubectl‘s general syntax:
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:
- Operation: it’s what we intend to perform on one or more resources
- Resource types: the resource type on which we want to perform the operation
- Output options: if our operation produces some output, we can specify the expected display format using -o or –output flag
Some of the basic operations supported by kubectl includes:
- create: create a new resource(s)
- apply: creates or updates a resource(s)
- delete: for deleting resource(s)
- get: lists one or more resources
- explain: gives us documentation of resource(s)
- describe: displays a detailed state of our resource(s)
- run: starts up a pod from a given container image
- exec: helps us execute a command against a container within a pod
- logs: allows us to view logs for a container
- config: helps us update our kubeconfig file
K8s introduces a variety of resources against which we can perform operations. Some of the common resource types include:
- namespaces (ns)
- nodes (no)
- pods (po)
- services (svc)
- endpoints (ep)
- configmaps (cm)
- deployments (deploy)
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:
- wide: outputs any additional information to the stout
- yaml: presents the API object in YAML format
- json: displays API object in JSON format
Now that we understand the basics, let’s try out some kubectl commands:
Kubectl Help Documentation:
We can get help with kubectl usage with its help command:
We can stream the output using less for better readability:
If we want help with a specific command, we can also have something like:
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.