k8s 根据系统进程号查询pod容器和根据容器查询进程号


根据pod可以查看容器名字所在节点定位容器名字。
kubectl get pod -o wide

[root@k69 ~]# docker inspect 0cd46baf447b|egrep Pid "Pid": 346, "PidMode": "", "PidsLimit": 0, [root@k69 ~]# ps aux|egrep 346 root 346 7.5 0.8 25819208 2299124 ? Ssl Apr06 131:51 java -jar /xxxx.jar

=====================================================================================================


在管理 Kubernetes 集群的过程中,我们经常会遇到这样一种情况:在某台节点上发现某个进程资源占用量很高,却又不知道是哪个容器里的进程。有没有办法可以根据 PID 快速找到 Pod 名称呢?

假设现在有一个 prometheus 进程的 PID 是 14338

1. Container ID

要获取容器的 ID,可以查看 PID 对应的 cgroup 信息:

cat /proc/14338/cgroup

11:blkio:/kubepods/burstable/pod8e018a8e-4aaa-4ac6-986a-1a5133a4bcf1/d6f24b62ea28e9e67f7bc06f98de083cc49454f353389cd396f5d3ac6448f19c
10:cpuset:/kubepods/burstable/pod8e018a8e-4aaa-4ac6-986a-1a5133a4bcf1/d6f24b62ea28e9e67f7bc06f98de083cc49454f353389cd396f5d3ac6448f19c
9:freezer:/kubepods/burstable/pod8e018a8e-4aaa-4ac6-986a-1a5133a4bcf1/d6f24b62ea28e9e67f7bc06f98de083cc49454f353389cd396f5d3ac6448f19c
8:hugetlb:/kubepods/burstable/pod8e018a8e-4aaa-4ac6-986a-1a5133a4bcf1/d6f24b62ea28e9e67f7bc06f98de083cc49454f353389cd396f5d3ac6448f19c
7:perf_event:/kubepods/burstable/pod8e018a8e-4aaa-4ac6-986a-1a5133a4bcf1/d6f24b62ea28e9e67f7bc06f98de083cc49454f353389cd396f5d3ac6448f19c
6:cpuacct,cpu:/kubepods/burstable/pod8e018a8e-4aaa-4ac6-986a-1a5133a4bcf1/d6f24b62ea28e9e67f7bc06f98de083cc49454f353389cd396f5d3ac6448f19c
5:pids:/kubepods/burstable/pod8e018a8e-4aaa-4ac6-986a-1a5133a4bcf1/d6f24b62ea28e9e67f7bc06f98de083cc49454f353389cd396f5d3ac6448f19c
4:devices:/kubepods/burstable/pod8e018a8e-4aaa-4ac6-986a-1a5133a4bcf1/d6f24b62ea28e9e67f7bc06f98de083cc49454f353389cd396f5d3ac6448f19c
3:net_prio,net_cls:/kubepods/burstable/pod8e018a8e-4aaa-4ac6-986a-1a5133a4bcf1/d6f24b62ea28e9e67f7bc06f98de083cc49454f353389cd396f5d3ac6448f19c
2:memory:/kubepods/burstable/pod8e018a8e-4aaa-4ac6-986a-1a5133a4bcf1/d6f24b62ea28e9e67f7bc06f98de083cc49454f353389cd396f5d3ac6448f19c
1:name=systemd:/kubepods/burstable/pod8e018a8e-4aaa-4ac6-986a-1a5133a4bcf1/d6f24b62ea28e9e67f7bc06f98de083cc49454f353389cd396f5d3ac6448f19c

可以看到该进程对应的容器 ID 为 d6f24b62...,可以再优化一下上面的命令,直接获取容器 ID:

$ CID=$(cat /proc/14338/cgroup | awk -F '/' '{print $5}')

$ echo ${CID:0:8}
d6f24b62

最后一步根据容器 ID 获取 Pod 名称,如果你的容器运行时是 containerd 或 crio,可以使用 crictl 来获取容器信息:

# Go Template
$ crictl inspect -o go-template --template='{{index .status.labels "io.kubernetes.pod.name"}}' d6f24b62
prometheus-k8s-0

# jq
$ crictl inspect d6f24b62|jq '.status.labels["io.kubernetes.pod.name"]'
"prometheus-k8s-0"


2. Pod UID

面来看看第二种方法,先根据 PID 直接获取 Pod UID

$ cat /proc/14338/mountinfo | grep "etc-hosts" | awk -F / {'print $6'}
8e018a8e-4aaa-4ac6-986a-1a5133a4bcf1

然后根据 Pod UID 获取 Pod 名称:

$ crictl ps -o json | jq  '.[][].labels | select (.["io.kubernetes.pod.uid"] == "8e018a8e-4aaa-4ac6-986a-1a5133a4bcf1") | .["io.kubernetes.pod.name"]'|uniq
"prometheus-k8s-0"



"Labels": {
"annotation.io.kubernetes.container.hash": "53c04b42",
"annotation.io.kubernetes.container.ports": "[{\"name\":\"8500tcp02\",\"containerPort\":8500,\"protocol\":\"TCP\"}]",
"annotation.io.kubernetes.container.restartCount": "0",
"annotation.io.kubernetes.container.terminationMessagePath": "/dev/termination-log",
"annotation.io.kubernetes.container.terminationMessagePolicy": "File",
"annotation.io.kubernetes.pod.terminationGracePeriod": "30",
"io.kubernetes.container.logpath": "/var/log/pods/188ab140-f391-11ee-a7ab-061d22000bfb/madp-ccy-biz/0.log",
"io.kubernetes.container.name": "madp-ccy-biz",
"io.kubernetes.docker.type": "container",
"io.kubernetes.pod.name": "madp-ccy-biz-7646c6845f-2xn4x",
"io.kubernetes.pod.namespace": "ccy",
"io.kubernetes.pod.uid": "188ab140-f391-11ee-a7ab-061d22000bfb",
"io.kubernetes.sandbox.id": "5135369e0258abebaf0a846629e4bec967c7a4b86f3dde79c8e973e51409b9db"
}










 

posted @ 2024-04-07 10:17  滴滴滴  阅读(236)  评论(0编辑  收藏  举报