Hello again! Welcome back to our second lesson in the "Cluster Interaction and Troubleshooting" module.
In our last session, we focused on kubectl apply, the declarative "write" operation that translates your YAML manifest files into live resources in the cluster. You learned how to create and update applications, which is the heart of deploying services on Kubernetes.
Today, we shift our focus from writing to reading. Once you've applied a configuration, how do you verify it worked? How do you get a quick overview of what's running in your cluster? The answer is kubectl get. This lesson is dedicated to mastering this fundamental command. You will learn to use kubectl get to list various cluster resources and view their summary status. For a Platform Engineer, this is one of the most frequently used commands for daily health checks and situational awareness.
1. The "Read" Command: What is kubectl get?
At its core, kubectl get is a read-only command used to retrieve summary information about Kubernetes resources. Think of it as the Kubernetes equivalent of running az vm list in Azure, aws ec2 describe-instances in AWS, or even docker ps. It's your primary tool for listing resources and checking their high-level status.
When you run kubectl get, it communicates with the Kubernetes API server, fetches the current state of the requested resources, and presents it in a concise, tabular format.
To get a formal introduction to the command's purpose and key characteristics, please read the first few sections of the following article.
Kubectl Demystified: Mastering the `kubectl get` Command
The article "Kubectl Demystified: Mastering the kubectl get Command" from dev.to provides a clear and concise introduction to this essential command.
Please read the sections "What is kubectl get?", "What Does kubectl get Do?", and "When to Use kubectl get". This will solidify your understanding of the command's role in cluster management.
2. Basic Usage: Listing Common Resources
The basic syntax is straightforward: kubectl get <resource-type>. You can also provide an optional resource name to retrieve just a single object.
Let's look at some of the most common resource types you'll be listing.
-
Pods (
po): The fundamental unit of deployment.kubectl get podsThe output will look something like this:
NAME READY STATUS RESTARTS AGE nginx-deployment-574d5d67c9-abcde 1/1 Running 0 2m10s nginx-deployment-574d5d67c9-fghij 1/1 Running 0 2m10sThe columns are self-explanatory, but
READYis particularly important: it shows how many containers in the Pod are ready versus the total number of containers.1/1is what you want to see. -
Deployments (
deploy): Manages the lifecycle of your application Pods.kubectl get deploymentsNAME READY UP-TO-DATE AVAILABLE AGE nginx-deployment 2/2 2 2 5mHere,
READYshows how many replicas are running versus the desired number. -
Services (
svc): Exposes your application to network traffic.kubectl get services -
Nodes (
no): The worker machines in your cluster.kubectl get nodes
This video from "TechWorld with Nana" demonstrates these basic get commands in a natural workflow, showing how they are used to check the status after creating resources.
Kubectl Basic Commands - Create and Debug Pod in a Minikube cluster | Kubernetes Tutorial 18
Watch these short clips to see kubectl get used in practice to list various resources and check their status after creation or updates.
Watch from the beginning until 03:19. Notice how the presenter uses kubectl get nodes, kubectl get pods, kubectl get services, and kubectl get deployment to continuously check the state of the cluster.
3. Scoping Queries with Namespaces
Your experience with multi-tenant environments on AWS, Azure, and GCP has shown that resource organization is key. In Kubernetes, the primary tool for this is namespaces.
By default, kubectl commands operate on the default namespace. If you run kubectl get pods and see nothing, it might just mean there are no Pods in the default namespace. As a Platform Engineer, you'll constantly work across different namespaces (e.g., dev, staging, prod, kube-system).
Here are the essential flags for managing namespaces:
-
-n <namespace-name>: List resources in a specific namespace. System components typically run inkube-system.# List pods in the kube-system namespace kubectl get pods -n kube-system -
-Aor--all-namespaces: List resources across all namespaces in the cluster. This is extremely useful for getting a global overview.# List all pods in the entire cluster kubectl get pods -A
4. Customizing Output for Deeper Insights
While the default table view is great for a quick glance, you often need more information. kubectl get provides powerful output formatting options using the -o (or --output) flag.
More Columns with -o wide
The -o wide flag adds extra columns to the output. For Pods, this is incredibly useful as it shows the Pod's IP address and the Node it's scheduled on.
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-574d5d67c9-abcde 1/1 Running 0 15m 10.244.1.5 worker-node-1 <none> <none>
nginx-deployment-574d5d67c9-fghij 1/1 Running 0 15m 10.244.2.8 worker-node-2 <none> <none>
This instantly tells you about your Pods' network identity and physical placement, which is invaluable for debugging connectivity or resource placement issues.
The Full Picture with -o yaml and -o json
Remember the YAML manifest files you used with kubectl apply to define the desired state? With -o yaml or -o json, you can ask the API server to give you the actual state of a resource in the same format.
# Get the full YAML definition of a specific pod
kubectl get pod nginx-deployment-574d5d67c9-abcde -o yaml
This is the ground truth. It shows the complete configuration of the live object, including fields that were defaulted or added by Kubernetes controllers (like status). Comparing this output to your original manifest is a powerful debugging technique.
This video provides a quick visual demonstration of these output flags.
Important Kubernetes kubectl Command with Examples in 20 minutes!
This clip from the "Cloud Champ" channel shows how to use -o yaml, -o json, and -o wide to change the output of kubectl get.
Watch from 07:59 to 08:59. The video clearly demonstrates how these flags alter the information you receive.
For a more comprehensive overview of all the formatting options, including advanced ones like custom-columns and jsonpath that are great for scripting, the "Guide to kubectl Output Formatting" article is an excellent reference.
Guide to kubectl Output Formatting
This article from Baeldung is a deep dive into output formatting. For now, focus on the sections that reinforce what we've just discussed.
Read sections 2. Formatting the kubectl Command’s Output, 3. Wide Tabular Output, and 4. Complete Resource Representation Output. This will solidify your understanding of the default, wide, JSON, and YAML formats.
Test your understanding!
You have just joined a new project. You run kubectl get deployments and the output is empty (No resources found in default namespace.). Your teammate assures you that the application is running.
What kubectl get command would you run next to verify this and see all the running deployments in the cluster?
Show answer
You would run kubectl get deployments -A (or kubectl get deployments --all-namespaces). This command ignores the default namespace and searches across all namespaces in the cluster, which will likely reveal the deployment running in its own dedicated namespace.
5. Hands-on Practice: Watch it Live
Let's tie this together with the nginx-deployment.yaml from our last lesson.
-
If you don't have it, create a file named
nginx-deployment.yaml:apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.21.0 ports: - containerPort: 80 -
Apply the manifest:
kubectl apply -f nginx-deployment.yaml -
Now, use
getto inspect what happened. First, check the Deployment:kubectl get deployment nginx-deploymentYou should see
READYshowing2/2. -
Next, check the Pods created by the Deployment:
kubectl get pods -l app=nginx -o wideHere, we've combined
-l(a label selector) to find only pods with theapp=nginxlabel and-o wideto see their IPs and nodes. -
Finally, let's watch a change happen in real-time. The
-w(--watch) flag keeps the command running and streams updates. Open a new terminal and run:kubectl get pods -l app=nginx -w -
In your original terminal, edit
nginx-deployment.yamland changereplicas: 2toreplicas: 3, then apply it:kubectl apply -f nginx-deployment.yamlSwitch back to the terminal with the
watchcommand. You will see a new Pod appear and transition fromPendingtoContainerCreatingtoRunning. This is an incredibly powerful way to monitor rollouts or scaling events. PressCtrl+Cto exit the watch.
Conclusion
You now have a solid grasp of kubectl get, the essential command for observing the state of your Kubernetes cluster. It provides the quick feedback loop needed to verify your kubectl apply actions and is the first step in any troubleshooting process.
Key Takeaways:
kubectl getis your primary read-only tool for listing resources and their summary status.- You can get common resources like
pods,deployments,services, andnodes. - Always be mindful of namespaces. Use
-nfor a specific namespace and-Ato view resources across the entire cluster. - Use
-o widefor more details like Pod IP and Node. - Use
-o yamlor-o jsonto see the full, live resource definition from the API server. - Use
-wto watch for real-time changes to resources.
You've learned how to get a summary view. But what if a Pod is stuck in Pending or CrashLoopBackOff? The summary from get tells you what is wrong, but not why. For that, you need a more detailed, human-readable view. In our next lesson, we'll dive into kubectl describe, the command that provides exactly that.