how to login prometheus pods using kubectl or web UI

3/11/2020

I am using prometheus(v2.16.0) to scrape the metrics data,now I want to login prometheus pod to check the config file path:

    ~/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

obviously I could not login into the prometheus pod, and I am try another way to login into using Web UI:

enter image description here

is it possible to login pod like this? Why I could not login prometheus pod?

-- Dolphin
kubernetes

1 Answer

3/11/2020

As it says in the output:

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

This means that in the pod there are multiple containers and it automatically picks the prometheus-server-configmap-reload. Which is probably not the container we want to access.


So the correct way to access prometheus bash command line:

  1. List the containers in the pod:
kubectl get pods prometheus-0 -n kube-system -o jsonpath='{.spec.containers[*].name}*
  1. Exec into the right container from the list above using:
kubectl exec --namespace <namespace> -it <pod_name> -c <container> /bin/ash

In some cases there needs to be double slash before the command as well:

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

You can also try /bin/sh or //bin/sh if bash is not available in the container image.

Hope it helps.

-- Piotr Malec
Source: StackOverflow