编辑~/.bashrc, 粘贴以下函数,并执行source ~/.bashrc使其生效, 使用的时候执行函数podinfo $pid
通过pid 获取pod name
1 2 3 4 5
| podinfo() { CID=$(cat /proc/$1/cgroup | awk -F '/' '{print $5}') CID=$(echo ${CID:0:8}) crictl inspect -o go-template --template='{{index .status.labels "io.kubernetes.pod.name"}}' $CID }
|
通过pid获取pod id
1 2 3
| podUid() { cat /proc/$1/mountinfo | grep "etc-hosts" | awk -F / {'print $6'} }
|
通过pod name获取pod 详细信息
1 2 3
| podAll() { kubectl get pods -o wide -A|grep "$1" }
|
Kubernetes 中的很多组件都是通过 HTTPS 协议来暴露指标,比如 kubelet,那么如何使用 API 来访问这些指标呢?
先选取一个容器,比如 prometheus,找到它的 PID:
1
| $ ps -ef|grep "/bin/prometheus"
|
根据 PID 找到 Pod UID:
1 2
| $ cat /proc/14338/mountinfo | grep "etc-hosts" | awk -F / {'print $6'} 8e018a8e-4aaa-4ac6-986a-1a5133a4bcf1
|
根据 Pod UID 找到 Service Account 的 token 挂载目录:
1 2 3 4 5 6
| $ ll /var/lib/kubelet/pods/8e018a8e-4aaa-4ac6-986a-1a5133a4bcf1/volumes/kubernetes.io~secret/prometheus-k8s-token-p7bgb/
总用量 0 lrwxrwxrwx 1 root root 13 7月 10 21:24 ca.crt -> ..data/ca.crt lrwxrwxrwx 1 root root 16 7月 10 21:24 namespace -> ..data/namespace lrwxrwxrwx 1 root root 12 7月 10 21:24 token -> ..data/token
|
获取 token 信息:
1
| $ export TOKEN=$(cat /var/lib/kubelet/pods/8e018a8e-4aaa-4ac6-986a-1a5133a4bcf1/volumes/kubernetes.io~secret/prometheus-k8s-token-p7bgb/token)
|
通过 curl 直接访问指标:
1
| $ curl -s -H "Authorization: Bearer $TOKEN" --cacert /var/lib/kubelet/pods/8e018a8e-4aaa-4ac6-986a-1a5133a4bcf1/volumes/kubernetes.io~secret/prometheus-k8s-token-p7bgb/ca.crt --insecure https:
|