prometheus监控redis并配置监控告警模块

root@iZj6c72dzbei17o2cuksmeZ:~/yaml/redis# helm install redis-exporter prometheus-community/prometheus-redis-exporter
NAME: redis-exporter
LAST DEPLOYED: Sun Apr 27 16:04:57 2025
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
1. Get the Redis Exporter URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app=prometheus-redis-exporter,release=redis-exporter" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl port-forward $POD_NAME 8080:

查看redis-exporter的yaml如下

---
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    meta.helm.sh/release-name: redis-exporter
    meta.helm.sh/release-namespace: default
  labels:
    app.kubernetes.io/instance: redis-exporter
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: prometheus-redis-exporter
    app.kubernetes.io/version: v1.69.0
    helm.sh/chart: prometheus-redis-exporter-6.10.0
  name: redis-exporter-prometheus-redis-exporter
  namespace: default
  resourceVersion: '25884419'
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app.kubernetes.io/instance: redis-exporter
      app.kubernetes.io/name: prometheus-redis-exporter
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app.kubernetes.io/instance: redis-exporter
        app.kubernetes.io/managed-by: Helm
        app.kubernetes.io/name: prometheus-redis-exporter
        app.kubernetes.io/version: v1.69.0
        helm.sh/chart: prometheus-redis-exporter-6.10.0
    spec:
      containers:
        - env:
            - name: REDIS_ADDR
              value: 'redis://myredis:6379'
          image: 'oliver006/redis_exporter:v1.69.0'
          imagePullPolicy: IfNotPresent
          livenessProbe:
            failureThreshold: 3
            httpGet:
              path: /
              port: exporter-port
              scheme: HTTP
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 1
          name: prometheus-redis-exporter
          ports:
            - containerPort: 9121
              name: exporter-port
              protocol: TCP
          readinessProbe:
            failureThreshold: 3
            httpGet:
              path: /
              port: exporter-port
              scheme: HTTP
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 1
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      serviceAccount: redis-exporter-prometheus-redis-exporter
      serviceAccountName: redis-exporter-prometheus-redis-exporter
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 1
  conditions:
    - lastTransitionTime: '2025-04-27T08:56:08Z'
      lastUpdateTime: '2025-04-27T08:56:08Z'
      message: Deployment has minimum availability.
      reason: MinimumReplicasAvailable
      status: 'True'
      type: Available
    - lastTransitionTime: '2025-04-27T08:56:06Z'
      lastUpdateTime: '2025-04-27T08:56:08Z'
      message: >-
        ReplicaSet "redis-exporter-prometheus-redis-exporter-57c69896c7" has
        successfully progressed.
      reason: NewReplicaSetAvailable
      status: 'True'
      type: Progressing
  observedGeneration: 1
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1

---
apiVersion: v1
kind: Service
metadata:
  annotations:
    meta.helm.sh/release-name: redis-exporter
    meta.helm.sh/release-namespace: default
  labels:
    app.kubernetes.io/instance: redis-exporter
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: prometheus-redis-exporter
    app.kubernetes.io/version: v1.69.0
    helm.sh/chart: prometheus-redis-exporter-6.10.0
  name: redis-exporter-prometheus-redis-exporter
  namespace: default
  resourceVersion: '25884386'
spec:
  clusterIP: 10.68.209.129
  clusterIPs:
    - 10.68.209.129
  internalTrafficPolicy: Cluster
  ipFamilies:
    - IPv4
  ipFamilyPolicy: SingleStack
  ports:
    - name: redis-exporter
      port: 9121
      protocol: TCP
      targetPort: exporter-port
  selector:
    app.kubernetes.io/instance: redis-exporter
    app.kubernetes.io/name: prometheus-redis-exporter
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

如果我们的redis有密码的话,我们就要部署多个redis-exporter,然后添加上给REDIS_PASSWORD的赋值

        - env:
            - name: REDIS_ADDR
              value: 'redis://myredis:6379'
            - name: REDIS_PASSWORD
              value: '123456'

创建serviceMonitor,yaml如下

---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  labels:
    app: redis-exporter
    app.kubernetes.io/instance: redis-exporter
    app.kubernetes.io/name: prometheus-redis-exporter
    release: kube-prometheus
  name: redis-exporter
  namespace: monitoring
  resourceVersion: '25889348'
spec:
  endpoints:
    - interval: 30s
      metricRelabelings:
        - action: replace
          replacement: '172.16.0.20:6379'
          sourceLabels:
            - instance
          targetLabel: instance
        - action: replace
          replacement: bitx-redis
          sourceLabels:
            - target
          targetLabel: target
      params:
        target:
          - '172.16.0.20:6379'
      path: /scrape
      port: redis-exporter
    - interval: 30s
      metricRelabelings:
        - action: replace
          replacement: '172.16.1.32:6379'
          sourceLabels:
            - instance
          targetLabel: instance
        - action: replace
          replacement: telegram-redis
          sourceLabels:
            - target
          targetLabel: target
      params:
        target:
          - '172.16.1.32:6379'
      path: /scrape
      port: redis-exporter
  jobLabel: redis-exporter
  namespaceSelector:
    matchNames:
      - default
  selector:
    matchLabels:
      app.kubernetes.io/instance: redis-exporter
      app.kubernetes.io/name: prometheus-redis-exporter

创建角色权限,因为这个是跨namespace的

kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: prometheus-rolebinding
subjects:
  - kind: ServiceAccount
    name: prometheus
    namespace: monitoring
roleRef:
  kind: ClusterRole
  name: cluster-admin
  apiGroup: rbac.authorization.k8s.io

grafana中导入11835模块后查看redis监控信息如下
在这里插入图片描述

配置告警模块

apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  labels:
    app.kubernetes.io/component: redis 
    app.kubernetes.io/name: redis-exporter
    app.kubernetes.io/part-of: kube-prometheus
    app.kubernetes.io/version: 1.8.2
    prometheus: k8s
    role: alert-rules
  name: redis-exporter-rules
  namespace: monitoring
spec:
  groups:
  - name: prometheus-redis-exporter
    rules:
    - alert: RedisDown
      annotations:
        description: Redis instance is down.
        summary: Redis down
      expr: redis_up == 0
      for: 0m
      labels:
        severity: critical
    - alert: RedisMissingMaster
      annotations:
        description: Redis cluster has no node marked as master.
        summary: Redis missing master
      expr: (count(redis_instance_info{role="master"}) or vector(0)) < 1
      for: 0m
      labels:
        severity: critical
    - alert: RedisDisconnectedSlaves
      annotations:
        description: Redis not replicating for all slaves. Consider reviewing the redis
          replication status.
        summary: Redis disconnected slaves
      expr: count without (instance, job) (redis_connected_slaves) - sum without (instance,
        job) (redis_connected_slaves) - 1 > 0
      for: 0m
      labels:
        severity: critical
    - alert: RedisReplicationBroken
      annotations:
        description: Redis instance lost a slave
        summary: Redis replication broken
      expr: delta(redis_connected_slaves[1m]) < 0
      for: 0m
      labels:
        severity: critical
    - alert: RedisClusterFlapping
      annotations:
        description: Changes have been detected in Redis replica connection. This can
          occur when replica nodes lose connection to the master and reconnect (a.k.a
          flapping).
        summary: Redis cluster flapping
      expr: changes(redis_connected_slaves[1m]) > 1
      for: 2m
      labels:
        severity: critical
    - alert: RedisOutOfSystemMemory
      annotations:
        description: Redis is running out of system memory (> 90%)
        summary: Redis out of system memory
      expr: redis_memory_used_bytes / redis_total_system_memory_bytes * 100 > 90
      for: 2m
      labels:
        severity: warning
    - alert: RedisOutOfConfiguredMaxmemory
      annotations:
        description: Redis is running out of configured maxmemory (> 90%)
        summary: Redis out of configured maxmemory
      expr: redis_memory_used_bytes / redis_memory_max_bytes * 100 > 90
      for: 2m
      labels:
        severity: warning
    - alert: RedisTooManyConnections
      annotations:
        description: Redis is running out of connections (> 90% used)
        summary: Redis too many connections
      expr: redis_connected_clients / redis_config_maxclients * 100 > 90
      for: 2m
      labels:
        severity: warning
    - alert: RedisNotEnoughConnections
      annotations:
        description: Redis instance should have more connections (> 1)
        summary: Redis not enough connections
      expr: redis_connected_clients < 1
      for: 2m
      labels:
        severity: warning
    - alert: RedisRejectedConnections
      annotations:
        description: Some connections to Redis has been rejected
        summary: Redis rejected connections
      expr: increase(redis_rejected_connections_total[1m]) > 0
      for: 0m
      labels:
        severity: critical

启动告警模块

root@iZj6c72dzbei17o2cuksmeZ:~/kube-prometheus-0.14.0# kubectl apply -f manifests/redisExporter-prometheusRule.yaml 
prometheusrule.monitoring.coreos.com/node-exporter-rules configured

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yunson_Liu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值