Given one of the PID, Container ID, or Pod ID, How to Find the Other Two (PID、Container ID、 Pod ID 间的互查)

PID –> Container ID

  • containerd
crictl ps | grep $(nsenter -u -t PID hostname)|cut -d' ' -f1
  • docker
nsenter -u -t PID hostname

PID –> Pod ID

crictl ps | grep $(nsenter -u -t PID hostname) | awk '{print $(NF-1)}'

Container ID –> Pod ID

crictl ps | grep CONTAINER_ID | awk '{print $(NF-1)}'

Container ID –> PIDs

  • containerd
pstree -p $(crictl inspect -o json CONTAINER_ID|jq '.info.pid')
  • docker
pstree -p $(docker inspect --format '{{.State.Pid}}' CONTAINER_ID)
# or
docker top CONTAINER_ID

Pod ID –> Container ID

crictl ps | grep POD_ID | awk '{print $1}'

Pod ID –> PIDs

for CID in $(crictl ps|grep POD_ID|awk '{print $1}'); do ROOT_PID=$(crictl inspect -o json $CID|jq '.info.pid'); pstree -p $ROOT_PID; done

Leave a Reply

Your email address will not be published. Required fields are marked *