Home / Technology / Kubectl Demystified: Mastering the `kubectl get` Command

Kubectl Demystified: Mastering the `kubectl get` Command

Kubernetes has become the de facto standard for container orchestration, and kubectl is the primary tool for interacting with Kubernetes clusters. Among its many commands, kubectl get is one of the most fundamental and frequently used. This article will explain what kubectl get is, how it works, when to use it, and provide practical examples to help beginners and CKA exam candidates master this essential command.

What is kubectl get? 

 kubectl get is a command-line instruction used to retrieve information about Kubernetes resources such as Pods, Deployments, Nodes, Services, and more. It communicates with the Kubernetes API server to fetch the current state of resources in your cluster. 
 
Key characteristics:  

Read-only: It does not modify resources.  
Versatile: Supports filtering, formatting, and real-time monitoring. 
Resource-aware: Works with all Kubernetes resource types (e.g., pods, nodes, services).  

What Does kubectl get Do?  

When you run kubectl get, it:  1. Connects to the Kubernetes API server.  2. Queries the server for the specified resource type.  3. Returns a summarized or detailed view of the resource(s). 
 
For example, kubectl get pods lists all Pods in the default namespace, showing their names, readiness status, restarts, and age.

When to Use kubectl get  

Use this command to:  

  1. Check resource status (e.g., verify if a Pod is running).  
  2. Troubleshoot issues (e.g., identify CrashLoopBackOff errors).  
  3. List resources (e.g., view all Nodes in the cluster).  
  4. Validate configurations (e.g., confirm a Deployment created ReplicaSets).  
  5. Monitor real-time changes (e.g., watch Pods during a rolling update).  

Basic Syntax

  

kubectl get  [options]
  • : The Kubernetes resource (e.g., pods, deployments, nodes).  
  • [options]: Flags to customize output or scope (e.g., -n , -o wide).  

Practical Examples with Explanations 

Example 1: List All Pods in the Default Namespace

  

kubectl get pods

Output:

  

NAME READY STATUS    RESTARTS    AGE
web-1 1/1  Running    0          5m
web-2 1/1  Running    0          5m`

NAME: Pod name.  
READY: Containers ready vs total. 
STATUS: Current state (Running, Pending, CrashLoopBackOff). 
RESTARTS: Number of container restarts. 
AGE: Time since creation.  

Example 2: List Pods in a Specific Namespace

 
 

kubectl get pods -n kube-system

Explanation: Lists Pods in the kube-system namespace, where Kubernetes system components (e.g., CoreDNS) reside.

Example 3: List All Nodes

  

kubectl get nodes

Output:

  

NAME    STATUS ROLES         AGE VERSION
node-01 Ready  control-plane 10d v1.27.4
node-02 Ready  worker        10d v1.27.4
  • ROLES: Node role (control-plane, worker).  
  • VERSION: Kubernetes version.  

Example 4: Wide Output Format

 

kubectl get pods -o wide

Output:

  

NAME  READY STATUS  ... IP         NODE    NOMINATED NODE
web-1 1/1   Running ... 10.244.1.2 node-02 

Explanation: Adds columns like IP (Pod IP) and NODE (worker node hosting the Pod).  

Example 5: Watch Resources in Real-Time

  

kubectl get pods -w

Explanation: Monitors Pods continuously (e.g., useful during deployments or scaling). Press Ctrl+C to exit.  

Example 6: Filter by Labels

  

kubectl get pods -l app=nginx

Explanation: Lists Pods with the label app=nginx.  

Example 7: Custom Columns

  

kubectl get pods -o custom-columns="NAME:.metadata.name,STATUS:.status.phase"

Output:

  

NAME STATUS
web-1 Running

Explanation: Extracts specific fields from the resource definition.  

Example 8: JSON Output

  

kubectl get pods -o json

Explanation: Returns detailed Pod information in JSON format (useful for scripting).  

Example 9: List All Resources in a Namespace

  

kubectl get all -n my-namespace

Explanation: Lists Pods, Services, Deployments, ReplicaSets, and StatefulSets in my-namespace.  

Example 10: Get a Specific Resource by Name

  

kubectl get pod web-1

Explanation: Shows details for the Pod named web-1.  

Common Options and Flags  

-n – Specify a namespace.
-Aor–all-namespaces – List resources across all namespaces.
-o wide/json/yaml – Customize output format.
–selectoror-l – Filter by labels.
–show-labels – Display labels in output.
-w – Watch for changes.

Tips for the CKA Exam  

  1. Use Short Names: Save time with aliases like po (pods), deploy (deployments), svc (services).  
  2. Filter Efficiently: Combine -l and -o jsonpath to extract specific fields quickly.  
  3. Practice Namespace Flags: Always specify -n or -A to avoid missing resources.  
  4. Master kubectl get events: Critical for troubleshooting exam scenarios.  

Conclusion  

The kubectl get command is indispensable for daily Kubernetes operations and a must-know for the CKA exam. By mastering its options and practicing real-world examples, you’ll gain the ability to quickly diagnose issues, validate configurations, and navigate clusters with confidence.  

Next Steps: Experiment with the examples above, explore other resource types (e.g., configmaps, secrets), and integrate kubectl get into your troubleshooting workflows. Happy clustering!