This section describes how to install and deploy Prometheus and view resource monitoring data on Prometheus. For details about the data, see Prometheus Metrics API.
docker pull prom/prometheus:v2.10.0
...
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-config
namespace: kube-system
data:
prometheus.yml: |
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
...
- job_name: 'kubernetes-npu-exporter'
kubernetes_sd_configs:
- role: pod
scheme: http
relabel_configs:
- action: keep
source_labels: [__meta_kubernetes_namespace]
regex: npu-exporter
- source_labels: [__meta_kubernetes_pod_node_name]
target_label: job
replacement: ${1}
...
kubectl label nodes <Host name of the management node> masterselector=dls-master-node --overwrite=true
kubectl apply -f prometheus.yaml
If the following information is displayed, the installation is successful:
1 2 3 4 5 6 7 | [root@centos check_env]# kubectl apply -f prometheus.yaml clusterrole.rbac.authorization.k8s.io/prometheus created serviceaccount/prometheus created clusterrolebinding.rbac.authorization.k8s.io/prometheus created service/prometheus created deployment.apps/prometheus created configmap/prometheus-config created |
kubectl get pods --all-namespaces | grep prometheus
The following is a startup example. If Running is displayed, Prometheus is started successfully.
1 | kube-system prometheus-58c69548b4-rhxsc 1/1 Running 0 6d14h |
Find the nodePort field in the prometheus.yaml file. The value of this field is the port number of the Prometheus service, which is 30003 by default.
git clone https://github.com/prometheus-operator/kube-prometheus.git
kubectl create -f manifests/setup/
1 2 3 4 5 | namespace/monitoring created ... deployment.apps/prometheus-operator created service/prometheus-operator created serviceaccount/prometheus-operator created |
kubectl get pod -A -o wide|grep prometheus-operator
1 | monitoring prometheus-operator-7649c7454f-wp84n 2/2 Running 0 58s 192.168.xx.xx node133 <none> <none> |
kubectl apply -f prometheus.yaml
If the following information is displayed, the installation is successful:
1 2 3 4 5 | service/prometheus created prometheus.monitoring.coreos.com/prometheus created serviceaccount/prometheus-service-account created clusterrole.rbac.authorization.k8s.io/prometheus-cluster-role created clusterrolebinding.rbac.authorization.k8s.io/prometheus-cluster-role-binding created |
kubectl get pods --all-namespaces | grep prometheus
Command output:
1 2 | kube-system prometheus-prometheus-0 2/2 Running 1 3m47s 192.168.xx.xx node133 <none> <none> monitoring prometheus-operator-7649c7454f-wp84n 2/2 Running 0 5m52s 192.168.xx.xx node133 <none> <none> |
If Prometheus has been installed, the following field in the servicemonitor.yaml file must be the same as matchLabels configured by serviceMonitorSelector in Prometheus.
...
labels:
serviceMonitorSelector: prometheus
...
You can run the following command to query matchLabels:
kubectl describe pod <pod-name>
apiVersion: v1
kind: Service
metadata:
namespace: npu-exporter # The namespace is npu-exporter.
name: npu-exporter
labels:
app: npu-exporter-svc # Label of NPU Exporter service
spec:
type: ClusterIP
ports:
- port: 8082 # Service port number of NPU Exporter
targetPort: 8082
...
...
spec:
endpoints:
- interval: 10s
targetPort: 8082 # Service port number of NPU Exporter
path: /metrics
namespaceSelector:
matchNames:
- npu-exporter # The namespace is npu-exporter.
selector:
matchLabels:
app: npu-exporter-svc # Label of the NPU Exporter service
kubectl apply -f servicemonitor.yaml kubectl apply -f npu-exporter-svc.yaml
kubectl get svc -A|grep npu-exporter
npu-exporter npu-exporter ClusterIP 10.98.xx.xx <none> 8082/TCP 31s
kubectl get servicemonitor -A|grep npu-exporter
kube-system npu-exporter 55s
Find the nodePort field in the prometheus.yaml file. The value of this field is the port number of the Prometheus service, which is 30003 by default.