侧边栏壁纸
博主头像
背锅小王子博主等级

我从事运维工作有十年之久,主要从事云原生相关的工作,对k8s、devops、servicemesh、可观察性等较为熟悉!

  • 累计撰写 59 篇文章
  • 累计创建 64 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

prometheus应用实践(五)配置ServiceMonitor监控mysql、redis

背锅小王子
2022-07-31 / 0 评论 / 0 点赞 / 209 阅读 / 597 字
温馨提示:
本文最后更新于 2023-03-13,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

1、需求说明

希望通过prometheus来监控mysql、redis等服务指标

因为mysql、redis都是部署在k8s中,所以直接可以使用mysql_exporter、redis_exporter来监控

目前prometheus社区已经将mysql_exporter、redis_exporter都制作了helm的chart包,我们可以直接使用

2、部署chart包

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

helm pull prometheus-community/prometheus-mysql-exporter
helm pull prometheus-community/prometheus-redis-exporter

tar xf prometheus-mysql-exporter-1.8.1.tgz
tar xf prometheus-redis-exporter-5.0.0.tgz

修改prometheus-mysql-exporter中的value.yml文件

mysql:
  db: ""
  host: "mysql.default.svc"
  param: ""
  pass: "你的mysql密码"
  port: 3306
  protocol: ""
  user: "root"

部署prometheus-mysql-exporter的chart包

helm install mysql-exporter . -n monitoring

修改prometheus-redis-exporter的value.yml文件

### redis地址
redisAddress: redis://redis-headless.default.svc:6379


### 如果redis配置了密码,需要修改redisPassword
auth:
  # Use password authentication
  enabled: true
  # Use existing secret (ignores redisPassword)
  secret:
    name: ""
    key: ""
  # Redis password (when not stored in a secret)
  redisPassword: "你的redis密码"
  # Redis user (version 6.X and above)
  redisUser: ""

部署prometheus-redis-exporter的chart包

helm install redis-exporter . -n monitoring

3、部署ServiceMonitor

创建mysql服务的ServiceMonitor:mysql-ServiceMonitor.yml

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: mysql
  namespace: monitoring
spec:
  endpoints:
    - interval: 15s
      port: mysql-exporter
  jobLabel: instance
  namespaceSelector:
    matchNames:
      - monitoring
  selector:
    matchLabels:
      app.kubernetes.io/instance: mysql-exporter

部署mysql-ServiceMonitor.yml

kubectl apply -f mysql-ServiceMonitor.yml

创建redis服务的ServiceMonitor:redis-ServiceMonitor.yml

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: redis
  namespace: monitoring
spec:
  endpoints:
    - interval: 15s
      port: redis-exporter
  namespaceSelector:
    matchNames:
      - monitoring
  selector:
    matchLabels:
      app.kubernetes.io/instance: redis-exporter

部署redis-ServiceMonitor.yml

kubectl apply -f redis-ServiceMonitor.yml

4、配置grafana图表

导入mysql图表:

在grafana的Dashboards中,点击import,选择 ID 添加:7362,然后确认即可

但是你会发现图表没有获取到各种指标,这个问题,是因为我们部署的prometheus-clusterRole.yaml文件权限较小导致,所以需要修改

如下:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  labels:
    app.kubernetes.io/component: prometheus
    app.kubernetes.io/instance: k8s
    app.kubernetes.io/name: prometheus
    app.kubernetes.io/part-of: kube-prometheus
    app.kubernetes.io/version: 2.33.4
  name: prometheus-k8s
rules:
- apiGroups:
  - ""
  resources:
  - nodes
  - services
  - endpoints
  - pods
  - nodes/proxy
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - configmaps
  - nodes/metrics
  verbs:
  - get
- nonResourceURLs:
  - /metrics
  verbs:
  - get

重新部署prometheus-clusterRole.yaml

kubectl apply -f prometheus-clusterRole.yaml

grafana图表展示:

图片-1659276243834

导入redis图表:

在grafana的Dashboards中,点击import,选择 ID 添加:763,然后确认即可

图片-1659276235591

0

评论区