CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Kubernetes / kubectl get

kubectl get

List Kubernetes resources and their status

Beginnerkubernetesessentialtroubleshooting

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.

Syntaxkubectl get TYPE[,TYPE...] [NAME | -l SELECTOR] [-n NAMESPACE | -A] [-o wide|yaml|json|jsonpath=...] [-w]

Watch it run

You've been paged about the prod namespace and want a one-screen picture of every pod's health before you touch anything.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 2
kubectl get pods -n prod

READY is ready containers over total containers. STATUS summarises the pod. RESTARTS counts container restarts, and (2m14s ago) says when the last one happened. The pod name tells you its owner: deployment name, ReplicaSet hash, random suffix.

Anatomy of a command

$
commandkubectl

The Kubernetes CLI. It turns your request into HTTPS calls to the cluster's API server.

How it works

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.

Options you'll actually use

-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 column

Common mistakes

kubectl get pods (and seeing nothing) during a prod incident

Without -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=prod
kubectl get pods --field-selector=status.phase!=Running to find all broken pods

CrashLoopBackOff 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 RESTARTS
Parsing the default table with awk '{print $3}' in a script

Columns 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 failures

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    In the practice terminal, list the pods in the namespace shop together with the node each runs on and its IP.

  2. 2

    Write a command that prints only the names of nodes that are not Ready (one per line).

  3. 3

    Read this row and say what is wrong: 'worker-c49d7f6b5-mx4rt 0/1 CrashLoopBackOff 7 (2m14s ago) 18m'

  4. 4

    For real practice, open AWS Lab > EKS > your cluster > Run kubectl (lab), deploy the sample app and list pods in every namespace.

Related commands

kubectl describeExplain an object's state, conditions and eventskubectl logsRead and follow container logskubectl configManage clusters, users and contexts in kubeconfigkubectl topShow CPU and memory use of nodes and pods
Previousdocker composeNextkubectl describe
Linux / Kubernetes / kubectl get

kubectl get

List Kubernetes resources and their status

Beginnerkubernetesessentialtroubleshooting

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.

Syntaxkubectl get TYPE[,TYPE...] [NAME | -l SELECTOR] [-n NAMESPACE | -A] [-o wide|yaml|json|jsonpath=...] [-w]

Watch it run

You've been paged about the prod namespace and want a one-screen picture of every pod's health before you touch anything.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 2
kubectl get pods -n prod

READY is ready containers over total containers. STATUS summarises the pod. RESTARTS counts container restarts, and (2m14s ago) says when the last one happened. The pod name tells you its owner: deployment name, ReplicaSet hash, random suffix.

Anatomy of a command

$
commandkubectl

The Kubernetes CLI. It turns your request into HTTPS calls to the cluster's API server.

How it works

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.

Options you'll actually use

-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 column

Common mistakes

kubectl get pods (and seeing nothing) during a prod incident

Without -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=prod
kubectl get pods --field-selector=status.phase!=Running to find all broken pods

CrashLoopBackOff 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 RESTARTS
Parsing the default table with awk '{print $3}' in a script

Columns 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 failures

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    In the practice terminal, list the pods in the namespace shop together with the node each runs on and its IP.

  2. 2

    Write a command that prints only the names of nodes that are not Ready (one per line).

  3. 3

    Read this row and say what is wrong: 'worker-c49d7f6b5-mx4rt 0/1 CrashLoopBackOff 7 (2m14s ago) 18m'

  4. 4

    For real practice, open AWS Lab > EKS > your cluster > Run kubectl (lab), deploy the sample app and list pods in every namespace.

Related commands

kubectl describeExplain an object's state, conditions and eventskubectl logsRead and follow container logskubectl configManage clusters, users and contexts in kubeconfigkubectl topShow CPU and memory use of nodes and pods
Previousdocker composeNextkubectl describe