AI Hints
Home›Kubernetes Tutorial›Your First Cluster
Kubernetes · Lesson 8 of 86

Meeting kubectl

The commands you use every day.

⏱ 2 min read▶ Video at 20:30🎓 Free
Watch this lesson in the Kubernetes courseWatch on YouTube ↗Full Kubernetes course

Kube control, written k u b e c t l, is the tool you will use more than any other. It turns your typing into requests for the A P I server. The good news is that it follows a very regular pattern, so you can learn it quickly. In this lesson, we learn that pattern.

Meeting kubectl
From the video: Meeting kubectl
  • One tool for everything
  • A simple, regular pattern
  • Learn it once, use it forever

The kubectl pattern

Almost every command has the same shape. First comes kube control. Then a verb, what you want to do, like get, create, delete, describe, or apply. Then a resource type, what you want to act on, like pods, deployments, or services. And last, optionally, a name, and some flags. So, kube control get pods, lists pods. Kube control delete pod web, deletes the pod named web. Once you see the pattern, a new command is never a surprise.

kubectlthe program
verbget, create, delete, apply
resourcepods, deployments, services
name + flagswhich one, and how

Reading the cluster

Let's read the cluster. The get verb lists resources. Here are the namespaces, which are folders that group objects, and here, the nodes. Kubernetes knows many resource types, and the command, A P I resources, lists all of them, with their short names. For example, pods can be shortened to p o, services to s v c, and deployments to deploy. Short names save a lot of typing.

kubectl
kubectl get namespaces
NAME              STATUS   AGE
default           Active   14m
kube-node-lease   Active   14m
kube-public       Active   14m
kube-system       Active   14m
kubectl get nodes
NAME      STATUS   ROLES           AGE   VERSION
k8s       Ready    control-plane   14m   v1.34.0
k8s-m02   Ready    <none>          14m   v1.34.0
k8s-m03   Ready    <none>          14m   v1.34.0
kubectl api-resources | head -8 | cut -c1-100
NAME                                SHORTNAMES   APIVERSION                        NAMESPACED   KIND
bindings                                         v1                                true         Bind
componentstatuses                   cs           v1                                false        Comp
configmaps                          cm           v1                                true         Conf
endpoints                           ep           v1                                true         Endp
events                              ev           v1                                true         Even
limitranges                         limits       v1                                true         Limi
namespaces                          ns           v1                                false        Name

The verbs you will use most

Here are the verbs that you will use daily. Get, to list things. Describe, to see full details and recent events, and it is the best debugging command. Create and apply, to make things, and we will compare them later. Delete, to remove things. Logs, to read what a container printed. Exec, to run a command inside a container. And explain, which is built in documentation, for any field, so you almost never need to search the web.

VerbWhat it does
getlist resources
describedetails and recent events
applycreate or update from a file
deleteremove resources
logsread container output
execrun a command in a container
explainbuilt-in documentation

Documentation in your terminal

The explain command is a hidden gem. Let's ask about a pod. It prints the description of the object straight from the cluster's own schema, so it always matches your version. We can go deeper, by following the dots. Asking for pod dot spec dot containers, explains that field, and so on, all the way down. When you forget what a field means, ask the cluster.

kubectl
kubectl explain pod | head -12
KIND:       Pod
VERSION:    v1

DESCRIPTION:
    Pod is a collection of containers that can run on a host. This resource is
    created by clients and scheduled onto hosts.

FIELDS:
  apiVersion	<string>
    APIVersion defines the versioned schema of this representation of an object.
    Servers should convert recognized schemas to the latest internal value, and
    may reject unrecognized values. More info:
kubectl explain pod.spec.containers | head -12
KIND:       Pod
VERSION:    v1

FIELD: containers <[]Container>


DESCRIPTION:
    List of containers belonging to the pod. Containers cannot currently be
    added or removed. There must be at least one container in a Pod. Cannot be
    updated.
    A single application container that you want to run within a pod.

Summary

Excellent. Kube control follows one pattern, verb, resource, name. Get lists, describe explains, apply creates, delete removes, and explain documents. Now we are ready for the big moment, running our very first pod.

Key takeaways
  • Pattern: verb + resource + name
  • get and describe to look
  • explain is built-in documentation
Next up: Your First Pod — run, inspect, and delete a pod