Sub topic 8
Exploring the cluster with kubectl
This chapter assumes you have a running kind or minikube cluster, a working kubectl context (covered in the previous chapter), and basic comfort with YAML. Here you learn the three commands you will use most to look around a cluster: get, describe, and api-resources.
The working model
kubectl is a client for the Kubernetes API server. Every command is an HTTP request to that server. Cluster state is stored as objects, and each object has a kind (such as Node or Pod), a name, and often a namespace. Exploring a cluster means asking the API server for these objects.
Listing objects with get
kubectl get <kind> prints a table of objects. Start with the nodes, then look at what the cluster runs for itself.
kubectl get nodes
kubectl get pods --all-namespaces
kubectl get pods -n kube-system -o wideThe first command should show your control-plane node with the status Ready. The second lists Pods in every namespace. System components such as CoreDNS and the API server run as Pods in the kube-system namespace. The -o wide flag adds columns such as the Pod IP and the node it runs on.
Other useful output formats:
| Flag | Result |
|---|---|
| `-o wide` | Extra columns in the table |
| `-o yaml` | Full stored object as YAML |
| `-o json` | Full stored object as JSON |
| `-o name` | Only `kind/name`, handy in scripts |
Use -o yaml when you need to see fields that the table hides, such as status and defaulted values.
Inspecting one object with describe
get shows a summary. kubectl describe <kind> <name> shows a readable report for a single object, including its configuration, current conditions, and recent events.
kubectl describe node <node-name>
kubectl describe pod <pod-name> -n kube-systemThe Events section at the bottom is often the most useful part. It records things like image pulls, scheduling decisions, and failures.
Discovering kinds with api-resources
You do not need to memorize kind names. Ask the cluster what it supports.
kubectl api-resources
kubectl explain pod.spec.containersapi-resources lists each kind with its short name (for example po for pods and deploy for deployments), its API group, and whether it is namespaced. kubectl explain prints the documentation for any field path. It is a quick way to check a manifest field without leaving the terminal.
Common errors
- No resources found in default namespace.
getonly searches the current namespace. Add-n <namespace>or--all-namespaces. - The connection to the server was refused. The cluster is stopped, or the active context points at a cluster that no longer exists. Run
kubectl config current-contextand check that the cluster is running. - the server doesn't have a resource type. The kind name is misspelled, or it belongs to a custom resource that is not installed. Check
kubectl api-resources. - Nodes show
NotReady. The cluster may still be starting. Wait briefly, then runkubectl describe nodeand read the Conditions and Events.
Exercise
You run kubectl get pods on a healthy cluster and see "No resources found in default namespace." You know CoreDNS is running. Which command shows it, and why was it missing?
Show answer
Run kubectl get pods -n kube-system (or add --all-namespaces). CoreDNS is a system component in the kube-system namespace, and get pods only searches the current namespace, which is default unless you changed it.
Exercise
How would you find the short name and API group for the Service kind, and how would you read the documentation for the spec.ports field of a Service?
Show answer
Run kubectl api-resources and find the services row, which shows the short name svc and the core (empty) API group. Run kubectl explain service.spec.ports to read the field documentation.