Kubernetes
Containers
Process Identification
Pod Management
DevOps

How to retrieve the pod/container in which run a given process

Master System Design with Codemia

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

Introduction

During Kubernetes troubleshooting, you may have a host PID from node tools and need to map it back to the owning pod and container. That mapping is indirect because Kubernetes metadata sits above Linux process and cgroup layers. A reliable workflow uses process cgroup data, container runtime inspection, and Kubernetes API confirmation.

Start with Node-Level Process Evidence

Begin on the node where the PID was observed and capture basic process context.

bash
ps -fp 24871
readlink /proc/24871/exe

Then inspect cgroup entries for runtime identifiers.

bash
cat /proc/24871/cgroup

In many clusters, this output includes kubepods hierarchy and a container ID fragment you can match with runtime tools.

Resolve Container with CRI Tools

For containerd-based Kubernetes nodes, crictl is typically the easiest bridge from runtime object to pod metadata.

bash
sudo crictl ps
sudo crictl ps -q
sudo crictl inspect <container-id>

In inspect output, look for:

  • container name,
  • pod sandbox ID,
  • pod namespace and pod name labels.

If you only have PID, correlate by checking runtime PID or cgroup path fields in inspect data.

Use Containerd Commands as Backup

If crictl is unavailable or incomplete, use containerd CLI directly in Kubernetes namespace.

bash
sudo ctr -n k8s.io tasks list
sudo ctr -n k8s.io containers list

Find matching task PID first, then inspect associated container to recover metadata.

bash
sudo ctr -n k8s.io tasks list | grep 24871

This is useful in constrained nodes where CRI tooling is minimal.

Confirm Mapping in Kubernetes API

Runtime lookup is not enough; verify in Kubernetes control-plane view.

bash
kubectl get pod -A -o wide | grep <pod-name>
kubectl describe pod -n <namespace> <pod-name>

To map by container ID suffix:

bash
kubectl get pods -A -o jsonpath='{range .items[*]}{.metadata.namespace}{" "}{.metadata.name}{" "}{.status.containerStatuses[*].containerID}{"\n"}{end}'

Matching runtime and API sides avoids false positives during rapid restarts.

Handle Restarts and PID Reuse Carefully

PIDs are ephemeral. If the process exited and node reused PID, mapping can be wrong unless you capture evidence quickly.

Best practice:

  1. Record timestamp and node name with PID.
  2. Capture /proc/PID/cgroup immediately.
  3. Correlate with container start time from runtime inspect.
  4. Confirm pod UID in API before taking action.

This reduces mistakes in high-churn clusters.

Build a Repeatable Incident Runbook

A concise runbook speeds on-call work:

  1. Identify node and PID.
  2. Extract cgroup and command line data.
  3. Resolve runtime container with crictl or ctr.
  4. Confirm namespace and pod in Kubernetes API.
  5. Save container ID and pod UID in incident notes.

Standardization prevents ad hoc commands that miss critical details.

Optional Scripted Helper

You can keep a tiny helper script for local incident use.

bash
1#!/usr/bin/env bash
2set -euo pipefail
3pid="$1"
4
5echo "node: $(hostname)"
6echo "pid: $pid"
7cat "/proc/$pid/cgroup"
8sudo ctr -n k8s.io tasks list | awk -v p="$pid" '$2==p {print}'

Keep helper output deterministic and avoid destructive actions in diagnostics scripts.

Security and Access Considerations

Node-level PID mapping usually requires elevated permissions. Use audited access paths and least privilege where possible. Avoid running broad root shell commands beyond what is necessary for mapping.

Also sanitize incident logs when they may contain sensitive command-line arguments or paths.

Common Pitfalls

  • Assuming host PID directly identifies pod without checking cgroup data.
  • Using Docker-specific commands on CRI/containerd clusters.
  • Forgetting namespace during pod lookup and selecting wrong workload.
  • Investigating too late and misattributing due to PID reuse.
  • Skipping API-side confirmation after runtime-level mapping.

Summary

  • PID-to-pod mapping requires runtime and Kubernetes metadata correlation.
  • Start with /proc/PID/cgroup on the correct node.
  • Use crictl or ctr to resolve container identity.
  • Confirm pod namespace and UID through Kubernetes API.
  • Capture evidence early and follow a repeatable runbook for reliable incident response.

Course illustration
Course illustration

All Rights Reserved.