Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Using crictl in containerd, is there an easy way to find to which pod/container belongs a given process, using its PID on the host machine?

For example, how can I retrieve the name of the pod which runs the process below (1747):

root@k8s-worker-node:/# ps -ef | grep mysql
1000        1747    1723  0 08:58 ?        00:00:01 mysqld
like image 750
Fabrice Jammes Avatar asked Oct 19 '25 10:10

Fabrice Jammes


1 Answers

Assuming that you're looking at the primary process in a pod, you could do something like this:

crictl ps -q | while read cid; do
    if crictl inspect -o go-template --template '{{ .info.pid }}' $cid | grep -q $target_pid; then
        echo $cid
    fi
done

This walks through all the crictl managed pods and checks the pod pid against the value of the $target_pid value (which you have set beforehand to the host pid in which you are interested).

like image 109
larsks Avatar answered Oct 22 '25 03:10

larsks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!