Introduction
kubectl get pods does not provide a native "contains substring" name filter flag. The usual approach is combining kubectl output with tools like grep, or using label selectors when possible (preferred). For robust scripts, JSONPath or jq filtering is more structured than text parsing.
Core Sections
1) Quick partial-name filtering with grep
kubectl get pods -n myns | grep api-
For names only:
kubectl get pods -n myns -o name | grep api-
Simple and fast for terminal usage.
2) Label selectors (best practice)
If pods are labeled, use server-side filtering:
kubectl get pods -n myns -l app=my-api
This is more reliable and efficient than name substring matching.
3) Structured filtering with jq
kubectl get pods -n myns -o json \
| jq -r '.items[] | select(.metadata.name | contains("api-")) | .metadata.name' ``` Works better for scripts needing precise parsing. ### 4) Regex filtering and status constraints Combine with field selectors or grep regex: ```bash kubectl get pods -n myns --field-selector=status.phase=Running -o name | grep -E 'api-|worker-' ``` Keep command intent explicit to avoid accidental matches. ## Verification Workflow and Operational Hardening After implementing the fix, validate with a repeatable workflow rather than ad hoc manual checks. A reliable approach is: reproduce baseline, apply one focused change, then verify both expected behavior and nearby edge cases. This keeps debugging causal and makes reviews easier because every observed improvement is traceable to a specific diff. A simple validation loop: ```bash # 1) capture baseline output ./run_case.sh > before.txt # 2) apply targeted fix from this article # edit code/config only in relevant area # 3) verify after-state and compare ./run_case.sh > after.txt diff -u before.txt after.txt ``` For codebases with automated tests, immediately translate the reproduced issue into a regression test. This is the fastest way to prevent recurrence after refactors, dependency upgrades, or runtime migrations. ```bash # typical quality gate sequence ./lint.sh ./test.sh ./smoke.sh ``` Edge-case validation is essential. Many failures appear only on boundary inputs such as empty collections, null values, unusual encodings, large payloads, or high concurrency. Build a compact table of edge scenarios with expected outcomes, then run it in local and CI environments. This catches hidden assumptions early and reduces production surprises. Environment parity also matters. A fix that works locally can fail elsewhere due to version differences, OS behavior, architecture (x86 vs ARM), filesystem semantics, or network policy. Capture runtime metadata alongside results so troubleshooting stays grounded in facts. ```bash python --version node --version java -version git rev-parse --short HEAD ``` Before rollout, define rollback criteria and observability signals. Decide in advance which metrics/logs indicate success or regression, and document the rollback command path for on-call responders. Teams recover faster when fallback steps are predefined instead of improvised during incidents. Finally, isolate functional fixes from broad refactors. Small, focused commits are easier to review, bisect, and revert safely. If normalization, formatting, or dependency upgrades are required, ship them in separate commits to keep risk controlled and diagnosis straightforward. ## Common Pitfalls * Expecting kubectl to support name-substring filtering natively. * Using brittle column parsing that breaks with output format changes. * Relying on pod names instead of stable labels in automation. * Forgetting namespace scope and filtering wrong pod set. * Ignoring running/status filters when operational context requires them. ## Summary For partial pod-name filtering, pipe kubectl output through tools like `grep` or `jq`. For production automation, prefer label selectors and structured JSON filtering over fragile plain-text parsing. This keeps commands maintainable and less error-prone. A practical way to keep this solution robust over time is to add one focused regression test and one edge-case test that represent your real production data shape. Re-run those checks whenever dependencies, runtime versions, or infrastructure settings change. This small maintenance habit catches compatibility drift early and prevents recurring incidents that otherwise look like random regressions.