Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用 Prometheus 监控 MongoDB #25

Open
LLLeon opened this issue Apr 18, 2021 · 0 comments
Open

使用 Prometheus 监控 MongoDB #25

LLLeon opened this issue Apr 18, 2021 · 0 comments

Comments

@LLLeon
Copy link
Owner

LLLeon commented Apr 18, 2021

0. 安装 Prometheus 和 Grafana 等组件

通过 Minikube 来安装 Kubernetes 测试环境。

$ minikube start --cpus 4 --memory 8192 --vm-driver hyperkit
$ helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
$ helm repo add stable https://kubernetes-charts.storage.googleapis.com/
$ helm repo update
$ helm install prometheus prometheus-community/kube-prometheus-stack

可以使用 kubectl 查看安装好的各组件。

1. 安装 MongoDB

mongodb.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongodb-deployment
  labels:
    app: mongodb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongodb
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      containers:
      - name: mongodb
        image: mongo
        ports:
        - containerPort: 27017
---
apiVersion: v1
kind: Service
metadata:
  name: mongodb-service
spec:
  selector:
    app: mongodb
  ports:
    - protocol: TCP
      port: 27017
      targetPort: 27017

安装:

$ kubectl apply -f mongodb.yaml

2. 安装 Exporter

Exporter 是什么:将要采集数据的应用数据转换成 Prometheus 能理解的 metrics。

工作流程:1)Exporter 从目标应用获取 metrics 数据。2)转换为正确格式。3)在 /metrics 端点暴露 metrics。4)Promettheus 的 Retrieval 拉取 metrics 数据。5)Promettheus 将 metrics 数据存储到时序数据库。6)Promettheus 的 HTTP Server 提供从数据库获取数据的 API,可以通过 PromQL 来查询数据。

需要安装 3 个组件:

  • Exporter 应用:用来暴露 /metrics 端点
  • Service:用来连接到 Exporter
  • ServiceMonitor:让 Prometheus 知道有一个新的端点需要被 scrape。

可以通过 Helm 来安装。

先添加 repo:

$ helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
$ helm repo update

查看 chart 的 values:

$ helm show values prometheus-community/prometheus-mongodb-exporter

有几个值需要指定:

values.yaml

mongodb:
  uri: "mongodb://mongodb-service:27017" # 指定 MongoDB SVC endpoint

serviceMonitor:
  additionalLabels:
    release: prometheus # ServiceMonitor 标签,使其可以被 Prometheus 发现

安装:

helm install mongodb-exporter prometheus-community/prometheus-mongodb-exporter -f values.yaml

检查 SVC 及 ServiceMonitor 是否正常:

$ kubectl port-forward service/mongodb-exporter-prometheus-mongodb-exporter 9216
$ curl localhost:9216
$ kubectl get servicemonitor

3. 使用 Grafana 查看数据

$ kubectl port-forward deployment/prometheus-grafana 3000

用户名 admin,获取密码:

$ kubectl get secret prometheus-grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo

浏览器访问 http://localhost:3000/,登录后即可在 Manage 中查看 MongoDB 的 CPU、内存等的使用情况了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant