kubectl
Kubernetes
sorting
pods
namespaces

Using --sort-by with kubectl get pods --all-namespaces to sort by both namespace AND name doesn't work

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

kubectl --sort-by only sorts by one field. If you are listing pods across all namespaces and want namespace first and pod name second, --sort-by alone cannot express that multi-key ordering.

What --sort-by Can Actually Do

This works:

bash
kubectl get pods -A --sort-by='.metadata.namespace'

And this works:

bash
kubectl get pods -A --sort-by='.metadata.name'

But kubectl does not support a composite sort such as "namespace, then name" through one --sort-by expression.

That is why attempts to combine both fields do not behave the way people expect.

Why It Fails

--sort-by expects a single JSONPath expression. It is not a SQL-style ORDER BY field1, field2 feature. Even though the data contains both namespace and name, the built-in sorting logic only uses one extracted key.

So the answer to the original problem is simple:

  • 'kubectl --sort-by supports one sort key'
  • multi-field sorting requires a second tool or custom formatting

Practical Shell Solution

If you are working in a Unix-like shell, the usual answer is to let kubectl print the fields and then use sort for the two-key ordering:

bash
kubectl get pods -A --no-headers | sort -k1,1 -k2,2

In the default kubectl get pods -A table output:

  • column 1 is the namespace
  • column 2 is the pod name

So sort -k1,1 -k2,2 gives the expected namespace-then-name ordering.

A More Explicit Custom-Columns Version

If you want to avoid depending on the default table format, print only the columns you care about:

bash
kubectl get pods -A \
  -o custom-columns='NAMESPACE:.metadata.namespace,NAME:.metadata.name,PHASE:.status.phase' \
  --no-headers | sort -k1,1 -k2,2

This is more stable because you control the output columns directly.

JSON and jq for Structured Sorting

If you prefer structured processing over text sorting, use JSON output and jq:

bash
kubectl get pods -A -o json \
  | jq -r '.items | sort_by(.metadata.namespace, .metadata.name)[] |
           [.metadata.namespace, .metadata.name, .status.phase] | @tsv'

This is often the cleaner choice for scripts because it does not depend on terminal table formatting.

When --sort-by Is Still Useful

--sort-by is still fine when one field is enough. For example:

  • sort all pods by creation time
  • sort all pods by name only
  • sort all pods by namespace only

The limitation appears only when you need lexicographic ordering across multiple fields.

That is also why there is no reliable multi-key workaround hidden inside one JSONPath expression. Once you need primary and secondary ordering, you need either a second sorting tool or a structured post-processing step.

The exact second-stage tool can vary by environment. On Unix-like shells, sort is the usual answer. In more script-heavy workflows, JSON output plus jq is often the more maintainable option.

Common Pitfalls

  • Assuming --sort-by accepts more than one key.
  • Trying to cram multiple fields into one JSONPath and expecting composite sorting.
  • Using default table output in scripts without realizing column order can be a fragile dependency.
  • Forgetting --no-headers before piping into sort.
  • Using text sorting when structured JSON output would make the script clearer and safer.

Summary

  • 'kubectl --sort-by supports only a single sort field.'
  • To sort by namespace and then pod name, pipe the output to sort or use jq.
  • 'kubectl get pods -A --no-headers | sort -k1,1 -k2,2 is the simplest shell solution.'
  • 'custom-columns or JSON output makes automation more robust.'
  • The limitation is in kubectl sorting behavior, not in your understanding of namespaces or pod names.

Course illustration
Course illustration

All Rights Reserved.