0
0
Kubernetesdevops~10 mins

kubectl get for listing resources in Kubernetes - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - kubectl get for listing resources
User runs 'kubectl get'
kubectl sends request to API server
API server queries cluster state
API server returns list of resources
kubectl displays resource list to user
The command sends a request to the Kubernetes API server, which returns the current list of resources. kubectl then shows this list to the user.
Execution Sample
Kubernetes
kubectl get pods
kubectl get services
kubectl get nodes
These commands list pods, services, and nodes in the Kubernetes cluster.
Process Table
StepCommandAPI Request SentAPI Responsekubectl Output
1kubectl get podsGET /api/v1/namespaces/default/podsList of pods in default namespaceNAME READY STATUS RESTARTS AGE pod1 1/1 Running 0 5m pod2 1/1 Running 1 10m
2kubectl get servicesGET /api/v1/namespaces/default/servicesList of services in default namespaceNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service1 ClusterIP 10.96.0.1 <none> 80/TCP 15m
3kubectl get nodesGET /api/v1/nodesList of all nodes in clusterNAME STATUS ROLES AGE VERSION node1 Ready master 20d v1.26.0 node2 Ready worker 20d v1.26.0
4kubectl get pods -n kube-systemGET /api/v1/namespaces/kube-system/podsList of pods in kube-system namespaceNAME READY STATUS RESTARTS AGE coredns-558bd4d5db-abcde 1/1 Running 0 20d
5kubectl get pods --all-namespacesGET /api/v1/podsList of pods in all namespacesNAMESPACE NAME READY STATUS RESTARTS AGE default pod1 1/1 Running 0 5m kube-system coredns 1/1 Running 0 20d
6Exit--Command ends after displaying resource list
💡 kubectl finishes after receiving and displaying the resource list from the API server.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
CommandNonekubectl get podskubectl get serviceskubectl get nodeskubectl get pods -n kube-systemkubectl get pods --all-namespacesNone
API RequestNoneGET /api/v1/namespaces/default/podsGET /api/v1/namespaces/default/servicesGET /api/v1/nodesGET /api/v1/namespaces/kube-system/podsGET /api/v1/podsNone
API ResponseNonePods listServices listNodes listPods list in kube-systemPods list all namespacesNone
kubectl OutputNonePods tableServices tableNodes tablePods kube-system tablePods all namespaces tableNone
Key Moments - 3 Insights
Why does 'kubectl get pods' only show pods in the default namespace?
By default, kubectl queries the 'default' namespace unless you specify another namespace with '-n' or '--namespace'. See step 1 and step 4 in the execution_table.
What happens when you use '--all-namespaces' with 'kubectl get'?
kubectl requests pods from all namespaces, not just one. This is shown in step 5 where the API request is for all pods cluster-wide.
How does kubectl know what resource to list?
The resource type (pods, services, nodes) is the first argument after 'kubectl get'. kubectl sends this in the API request path, as shown in the API Request column.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what API request does 'kubectl get services' send?
AGET /api/v1/namespaces/default/services
BGET /api/v1/namespaces/default/pods
CGET /api/v1/nodes
DGET /api/v1/pods
💡 Hint
Check the API Request Sent column at step 2 in the execution_table.
At which step does kubectl list pods from all namespaces?
AStep 1
BStep 5
CStep 3
DStep 4
💡 Hint
Look at the Command and API Request Sent columns in the execution_table.
If you omit the namespace flag, which namespace does 'kubectl get pods' use?
Aall namespaces
Bkube-system
Cdefault
Dnode namespace
💡 Hint
See step 1 and step 4 commands and their API requests in the execution_table.
Concept Snapshot
kubectl get [resource] - Lists resources of the specified type.
Default namespace is 'default' unless '-n' or '--namespace' is used.
Use '--all-namespaces' to list resources across all namespaces.
kubectl sends a GET request to the Kubernetes API server.
The API server returns the current state, which kubectl displays.
Example: 'kubectl get pods' lists pods in default namespace.
Full Transcript
The 'kubectl get' command is used to list Kubernetes resources like pods, services, and nodes. When you run 'kubectl get pods', kubectl sends a request to the Kubernetes API server asking for the list of pods in the default namespace. The API server responds with the current list, and kubectl shows it in a table format. You can specify a different namespace with '-n' or '--namespace', or list resources across all namespaces with '--all-namespaces'. Each command sends a specific API request, and kubectl displays the returned data. This process repeats for each resource type you query. The command ends after showing the resource list.