首页 > 解决方案 > 如何使用 kubectl 或 Web UI 登录 prometheus pod

问题描述

我正在使用 prometheus(v2.16.0) 来抓取指标数据,现在我想登录 prometheus pod 检查配置文件路径:

    ~/Library/Mobile Documents/com~apple~CloudDocs/Document/k8s/work/kubernetes/cluster/addons/prometheus ⌚ 20:29:57
$ kubectl exec -it prometheus-0 -n kube-system /bin/bash

Defaulting container name to prometheus-server-configmap-reload.
Use 'kubectl describe pod/prometheus-0 -n kube-system' to see all of the containers in this pod.
OCI runtime exec failed: exec failed: container_linux.go:345: starting container process caused "exec: \"/bin/bash\": stat /bin/bash: no such file or directory": unknown
command terminated with exit code 126

~/Library/Mobile Documents/com~apple~CloudDocs/Document/k8s/work/kubernetes/cluster/addons/prometheus ⌚ 20:30:10
$ kubectl exec -it prometheus-0 -n kube-system /bin/ash

Defaulting container name to prometheus-server-configmap-reload.
Use 'kubectl describe pod/prometheus-0 -n kube-system' to see all of the containers in this pod.
OCI runtime exec failed: exec failed: container_linux.go:345: starting container process caused "exec: \"/bin/ash\": stat /bin/ash: no such file or directory": unknown
command terminated with exit code 126

~/Library/Mobile Documents/com~apple~CloudDocs/Document/k8s/work/kubernetes/cluster/addons/prometheus ⌚ 20:31:30
$ kubectl exec -it prometheus-0 -n kube-system /bash

Defaulting container name to prometheus-server-configmap-reload.
Use 'kubectl describe pod/prometheus-0 -n kube-system' to see all of the containers in this pod.
OCI runtime exec failed: exec failed: container_linux.go:345: starting container process caused "exec: \"/bash\": stat /bash: no such file or directory": unknown
command terminated with exit code 126

显然我无法登录 prometheus pod,我正在尝试另一种使用 Web UI 登录的方式:

在此处输入图像描述

可以像这样登录pod吗?为什么我无法登录 prometheus pod?

标签: kubernetes

解决方案


正如它在输出中所说:

Defaulting container name to prometheus-server-configmap-reload.

这意味着在 pod 中有多个容器,它会自动选择prometheus-server-configmap-reload. 这可能不是我们想要访问的容器。


那么访问prometheus bash命令行的正确方法:

  1. 列出 pod 中的容器:
kubectl get pods prometheus-0 -n kube-system -o jsonpath='{.spec.containers[*].name}*
  1. 从上面的列表中执行到正确的容器中,使用:
kubectl exec --namespace <namespace> -it <pod_name> -c <container> /bin/ash

在某些情况下,命令之前还需要有双斜杠:

kubectl exec -it -n prometheus-0 -c prometheus //bin/bash

/bin/sh如果//bin/shbash 在容器映像中不可用,您也可以尝试。

希望能帮助到你。


推荐阅读