kubectl getList Kubernetes resources and their status
kubectl get lists objects from the Kubernetes API (pods, deployments, services, nodes, events) as a table, or as YAML, JSON or exactly the fields you ask for. It is the first command of every Kubernetes investigation.
kubectl get TYPE[,TYPE...] [NAME | -l SELECTOR] [-n NAMESPACE | -A] [-o wide|yaml|json|jsonpath=...] [-w]You've been paged about the prod namespace and want a one-screen picture of every pod's health before you touch anything.
kubectlThe Kubernetes CLI. It turns your request into HTTPS calls to the cluster's API server.
kubectl reads your kubeconfig (~/.kube/config or $KUBECONFIG) to find the API server URL, the CA certificate and your credentials (on EKS, a short-lived token from 'aws eks get-token'). It then makes plain HTTPS requests: kubectl get pods -n prod -l app=api becomes GET /api/v1/namespaces/prod/pods?labelSelector=app%3Dapi. The API server authenticates you, checks RBAC (you need the list verb on pods), reads the objects from etcd (or its watch cache) and returns them. For the default table view kubectl asks for the Table format (Accept: application/json;as=Table), so the server decides the columns; kubectl only prints them. -w keeps the connection open as a watch and prints each change as it arrives. Nothing on the nodes is touched: get only reads the API's records.
-n NS / -ARead one namespace, or all namespaces (adds a NAMESPACE column)-o wideMore columns: pod IP and node for pods, selector and images for deployments-o yaml / -o jsonThe full object as stored, including status-o jsonpath='{...}'Print exactly the fields you want; ideal in scripts-o custom-columns=A:.path,...Your own table with named columns-o nameJust type/name per line, e.g. pod/api-7f9c5d4b6-2xkqp-l key=valueLabel selector; also key!=value, 'key in (a,b)', key--field-selectorFilter on fields like status.phase or spec.nodeName--sort-by=JSONPATHSort rows, e.g. .metadata.creationTimestamp-wWatch: keep printing changes until Ctrl+C--show-labels / -L keyShow all labels, or one label as its own columnkubectl get pods (and seeing nothing) during a prod incidentWithout -n you read your context's default namespace, often 'default', so prod looks empty.
kubectl get pods -n prod # or set it: kubectl config set-context --current --namespace=prodkubectl get pods --field-selector=status.phase!=Running to find all broken podsCrashLoopBackOff pods keep phase Running, so they are filtered out exactly when you need them.
kubectl get pods -A | grep -vE 'Running|Completed' # then check READY and RESTARTSParsing the default table with awk '{print $3}' in a scriptColumns change with -o wide, -A and Kubernetes versions; STATUS can contain spaces like '7 (2m ago)'.
kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.phase}{"\n"}{end}'Reading Running as 'healthy'Running only means the containers started. A pod can be Running with READY 0/1 because its readiness probe fails, and get no traffic.
Check READY and RESTARTS, then kubectl describe pod for probe failuresTry these on a Linux machine of your own. Each task has a hint and a solution.
In the practice terminal, list the pods in the namespace shop together with the node each runs on and its IP.
Write a command that prints only the names of nodes that are not Ready (one per line).
Read this row and say what is wrong: 'worker-c49d7f6b5-mx4rt 0/1 CrashLoopBackOff 7 (2m14s ago) 18m'
For real practice, open AWS Lab > EKS > your cluster > Run kubectl (lab), deploy the sample app and list pods in every namespace.
kubectl getList Kubernetes resources and their status
kubectl get lists objects from the Kubernetes API (pods, deployments, services, nodes, events) as a table, or as YAML, JSON or exactly the fields you ask for. It is the first command of every Kubernetes investigation.
kubectl get TYPE[,TYPE...] [NAME | -l SELECTOR] [-n NAMESPACE | -A] [-o wide|yaml|json|jsonpath=...] [-w]You've been paged about the prod namespace and want a one-screen picture of every pod's health before you touch anything.
kubectlThe Kubernetes CLI. It turns your request into HTTPS calls to the cluster's API server.
kubectl reads your kubeconfig (~/.kube/config or $KUBECONFIG) to find the API server URL, the CA certificate and your credentials (on EKS, a short-lived token from 'aws eks get-token'). It then makes plain HTTPS requests: kubectl get pods -n prod -l app=api becomes GET /api/v1/namespaces/prod/pods?labelSelector=app%3Dapi. The API server authenticates you, checks RBAC (you need the list verb on pods), reads the objects from etcd (or its watch cache) and returns them. For the default table view kubectl asks for the Table format (Accept: application/json;as=Table), so the server decides the columns; kubectl only prints them. -w keeps the connection open as a watch and prints each change as it arrives. Nothing on the nodes is touched: get only reads the API's records.
-n NS / -ARead one namespace, or all namespaces (adds a NAMESPACE column)-o wideMore columns: pod IP and node for pods, selector and images for deployments-o yaml / -o jsonThe full object as stored, including status-o jsonpath='{...}'Print exactly the fields you want; ideal in scripts-o custom-columns=A:.path,...Your own table with named columns-o nameJust type/name per line, e.g. pod/api-7f9c5d4b6-2xkqp-l key=valueLabel selector; also key!=value, 'key in (a,b)', key--field-selectorFilter on fields like status.phase or spec.nodeName--sort-by=JSONPATHSort rows, e.g. .metadata.creationTimestamp-wWatch: keep printing changes until Ctrl+C--show-labels / -L keyShow all labels, or one label as its own columnkubectl get pods (and seeing nothing) during a prod incidentWithout -n you read your context's default namespace, often 'default', so prod looks empty.
kubectl get pods -n prod # or set it: kubectl config set-context --current --namespace=prodkubectl get pods --field-selector=status.phase!=Running to find all broken podsCrashLoopBackOff pods keep phase Running, so they are filtered out exactly when you need them.
kubectl get pods -A | grep -vE 'Running|Completed' # then check READY and RESTARTSParsing the default table with awk '{print $3}' in a scriptColumns change with -o wide, -A and Kubernetes versions; STATUS can contain spaces like '7 (2m ago)'.
kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.phase}{"\n"}{end}'Reading Running as 'healthy'Running only means the containers started. A pod can be Running with READY 0/1 because its readiness probe fails, and get no traffic.
Check READY and RESTARTS, then kubectl describe pod for probe failuresTry these on a Linux machine of your own. Each task has a hint and a solution.
In the practice terminal, list the pods in the namespace shop together with the node each runs on and its IP.
Write a command that prints only the names of nodes that are not Ready (one per line).
Read this row and say what is wrong: 'worker-c49d7f6b5-mx4rt 0/1 CrashLoopBackOff 7 (2m14s ago) 18m'
For real practice, open AWS Lab > EKS > your cluster > Run kubectl (lab), deploy the sample app and list pods in every namespace.