一、概述

本文介绍如何使用 Kube-Prometheus 监控 Redis。通过部署 Redis Exporter,将 Redis 的性能指标暴露给 Prometheus 进行采集和监控。

核心组件:

  • Redis Exporter:用于暴露 Redis 指标的 Exporter(镜像版本:v1.37.0)
  • ServiceMonitor:Prometheus Operator 的 CRD,用于服务发现和指标采集配置

二、部署 Redis Exporter

2.1 创建 Deployment

通过 Deployment 部署 Redis Exporter,需要配置 Redis 连接信息。

apiVersion: apps/v1
kind: Deployment
metadata:
  #设置唯一名称,建议添加数据库实例ip
  name:  redis-exporter-172.16.1.77
  namespace: monitoring
  labels:
    app.kubernetes.io/component: exporter
    app.kubernetes.io/name: redis-exporter
    app.kubernetes.io/part-of: kube-prometheus
    app.kubernetes.io/version: 1.37.0
spec:
  selector:
    matchLabels:
      app.kubernetes.io/component: exporter
      app.kubernetes.io/name: redis-exporter
      app.kubernetes.io/part-of: kube-prometheus
  replicas: 1
  template:
    metadata:
      labels:
        app.kubernetes.io/component: exporter
        app.kubernetes.io/name: redis-exporter
        app.kubernetes.io/part-of: kube-prometheus
        app.kubernetes.io/version: 1.37.0
    spec:
      containers:
        - name: redis-exporter
          image: oliver006/redis_exporter:v1.37.0
          args: ["-redis.addr","redis://cpaas-redis-cluster.share-components:6379","-redis.password","AoSshx9Drq"]
          resources:
            requests:
              cpu: 20m
              memory: 20Mi
            limits:
              cpu: 100m
              memory: 30Mi
          ports:
            - containerPort: 9121
              name: http
          volumeMounts:
            - name: localtime
              mountPath: /etc/localtime
      volumes:
        - name: localtime
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
      restartPolicy: Always

配置说明:

  • args:Redis Exporter 启动参数
    • -redis.addr:Redis 地址,支持单机和集群模式
    • -redis.password:Redis 密码(建议使用 Secret 管理)
  • port: 9121:Redis Exporter 默认暴露端口
  • resources:资源限制,根据实际监控规模调整

2.2 创建 Service

apiVersion: v1
kind: Service
metadata:
  name: redis-exporter
  namespace: monitoring
  labels:
    app.kubernetes.io/component: exporter
    app.kubernetes.io/name: redis-exporter
    app.kubernetes.io/part-of: kube-prometheus
    app.kubernetes.io/version: 1.37.0
    platform: mos
spec:
  selector:
    app.kubernetes.io/component: exporter
    app.kubernetes.io/name: redis-exporter
    app.kubernetes.io/part-of: kube-prometheus
  type: ClusterIP
  ports:
    - name: http
      port: 9121
      targetPort: http

配置说明:

  • platform: mos:自定义标签,用于区分不同环境

2.3 创建 ServiceMonitor

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  labels:
    app.kubernetes.io/component: exporter
    app.kubernetes.io/name: redis-exporter
    app.kubernetes.io/part-of: kube-prometheus
    app.kubernetes.io/version: 1.37.0
  name: redis-exporter
  namespace: monitoring
spec:
  endpoints:  # 指标采集配置
    - interval: 15s # 每 15 秒采集一次
      port: http    # Service 定义的 port 名称
      relabelings:  # 指标重写规则
        - action: replace
          regex: (.*)
          replacement: $1
          sourceLabels:
            - __meta_kubernetes_pod_node_name
          targetLabel: instance
      scheme: http
  jobLabel: app.kubernetes.io/name
  targetLabels: [platform]
  namespaceSelector:
    matchNames:
    - monitoring
  selector: # 选择器,告诉 Prometheus 如何获取目标 Pod
    matchLabels:
      app.kubernetes.io/component: exporter
      app.kubernetes.io/name: redis-exporter
      app.kubernetes.io/part-of: kube-prometheus

配置说明:

  • interval: 15s:每 15 秒采集一次指标
  • relabelings:指标重写规则,将 Pod 节点名作为 instance 标签值
  • targetLabels:将 Service 的 platform 标签添加到采集的指标中

三、验证部署

# 查看 Deployment 状态
kubectl get deployment redis-exporter-172.16.1.77 -n monitoring

# 查看 Pod 日志
kubectl logs -f <pod-name> -n monitoring

# 访问 Exporter 指标接口
kubectl port-forward svc/redis-exporter 9121:9121 -n monitoring
curl http://localhost:9121/metrics

四、监控指标说明

Redis Exporter 提供的核心监控指标:

  • redis_up:Redis 实例是否可用
  • redis_connected_clients:已连接客户端数量
  • redis_used_memory_bytes:内存使用量
  • redis_commands_processed_total:命令处理总数
  • redis_keyspace_hits_total:缓存命中次数
  • redis_keyspace_misses_total:缓存未命中次数
  • redis_evicted_keys_total:被逐出的 key 数量

五、注意事项

  1. Redis 集群监控:监控 Redis 集群时,只需配置任意一个节点地址,Exporter 会自动发现整个集群
  2. 密码安全:生产环境建议使用 Secret 存储 Redis 密码,而非明文配置
  3. 多实例监控:监控多个 Redis 实例时,需为每个实例创建独立的 Deployment
  4. 性能影响:Exporter 采集指标会执行 INFO 命令,对 Redis 性能影响极小