【云原生】部署单节点Redis和Redis Exporter监控Redis

部署redis

创建ConfigMap

  • 首先创建命名空间
arduino 复制代码
kubectl create namespace ospp
  • 编写redis-config.yaml文件,内容如下:
yaml 复制代码
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-config
  namespace: ospp
  labels:
    app: redis
data:
  redis.conf: |-
    dir /srv
    port 6379
    bind 0.0.0.0
    appendonly yes
    daemonize no
    #protected-mode no
    requirepass test
    pidfile /srv/redis-6379.pid
  • 使用如下命令创建configmap
csharp 复制代码
[root@master ospp]# kubectl apply -f redis-config.yaml
configmap/redis-config created
[root@master ospp]#

创建Redis

  • 编写redis.yaml文件,内容如下:
yaml 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  namespace: ospp
  labels:
    app: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis:5.0.7
        command:
          - "sh"
          - "-c"
          - "redis-server /usr/local/redis/redis.conf"
        ports:
        - containerPort: 6379
        resources:
          limits:
            cpu: 1000m
            memory: 1024Mi
          requests:
            cpu: 1000m
            memory: 1024Mi
        livenessProbe:
          tcpSocket:
            port: 6379
          initialDelaySeconds: 300
          timeoutSeconds: 1
          periodSeconds: 10
          successThreshold: 1
          failureThreshold: 3
        readinessProbe:
          tcpSocket:
            port: 6379
          initialDelaySeconds: 5
          timeoutSeconds: 1
          periodSeconds: 10
          successThreshold: 1
          failureThreshold: 3
        volumeMounts:
        - name: config
          mountPath:  /usr/local/redis/redis.conf
          subPath: redis.conf
      volumes:
      - name: config
        configMap:
          name: redis-config

---

apiVersion: v1
kind: Service
metadata:
  name: service-redis
  namespace: ospp
spec:
  ports:
    - port: 6379
      protocol: TCP
      targetPort: 6379
      nodePort: 30120
  selector:
    app: redis
  type: NodePort
  • 使用如下命令创建redis
bash 复制代码
[root@master ospp]# kubectl apply -f redis.yaml
deployment.apps/redis created
service/service-redis unchanged
[root@master ospp]#
  • 查看创建资源如下:
sql 复制代码
**➜** **~** kubectl get service,deploy,pod -n ospp -o wide

NAME                    TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)                         AGE   SELECTOR

service/service-redis   NodePort   10.96.215.124   <none>        6379:30120/TCP,9121:30826/TCP   21h   app=redis

  


NAME                    READY   UP-TO-DATE   AVAILABLE   AGE    CONTAINERS             IMAGES                                         SELECTOR

deployment.apps/redis   1/1     1            1           106m   redis,redis-exporter   redis:5.0.7,oliver006/redis_exporter:v1.50.0   app=redis

  


NAME                        READY   STATUS    RESTARTS   AGE    IP           NODE                      NOMINATED NODE   READINESS GATES

pod/redis-85f6df986-cxx4s   2/2     Running   0          106m   10.244.4.7   my-k8s-cluster-3-worker   <none>           <none>

**➜** **~**

部署成功图如下:

  • Deployment(工作负载
  • Service(服务)**

部署redis_exporter

创建 redis-exporter deploy 应用

yaml 复制代码
---
apiVersion: apps/v1
kind: Deployment
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
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://service-redis.ospp:6379", ## 这里填写账号密码
              "-redis.password",
              "test",
            ]
          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

创建 redis-exporter service

yaml 复制代码
---
apiVersion: v1
kind: Service
metadata:
  name: redis-exporter
  namespace: monitoring
  #labels定义很重要,后面的ServiceMonitor资源需要使用到
  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:
    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

创建 redis-exporter servicemonitor

yaml 复制代码
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
  selector: #选择器,这个地方要配置对应Service的labels信息
    matchLabels:
      app.kubernetes.io/component: exporter
      app.kubernetes.io/name: redis-exporter
      app.kubernetes.io/part-of: kube-prometheus
  • 查看 prometheus target 添加效果:
  • kuboard

说明:

  • prometheus operator是通过ServiceMonitor发现监控目标,并对其进行监控。serviceMonitor 是对service 获取数据的一种方式。**

  • promethus-operator可以通过serviceMonitor 自动识别带有某些 label 的service ,并从这些service 获取数据。

  • serviceMonitor 也是由promethus-operator 自动发现的。

  • prometheus监控过程如下:

  • 部署成功图如下

  • serviceMonitor部署

3、Prometheus告警规则配置

  • prometheus rule规则配置:
yaml 复制代码
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
 labels:
   prometheus: k8s
   role: alert-rules
 name: redis-exporter-rules
 namespace: monitoring
spec:
 groups:
   - name: redis-exporter
     rules:
       - alert: RedisDown
         expr: redis_up == 0
         for: 0m
         labels:
           severity: critical
         annotations:
           summary: Redis down (instance {{ $labels.instance }})
           description: "Redis instance is down\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
       - alert: RedisClusterFlapping
         expr: changes(redis_connected_slaves[1m]) > 1
         for: 2m
         labels:
           severity: critical
         annotations:
           summary: Redis cluster flapping (instance {{ $labels.instance }})
           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).\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
       - alert: RedisReplicationBroken
         expr: delta(redis_connected_slaves[1m]) < 0
         for: 0m
         labels:
           severity: critical
         annotations:
           summary: Redis replication broken (instance {{ $labels.instance }})
           description: "Redis instance lost a slave\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
       - alert: RedisDisconnectedSlaves
         expr: count without (instance, job) (redis_connected_slaves) - sum without (instance, job) (redis_connected_slaves) - 1 > 1
         for: 0m
         labels:
           severity: critical
         annotations:
           summary: Redis disconnected slaves (instance {{ $labels.instance }})
           description: "Redis not replicating for all slaves. Consider reviewing the redis replication status.\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
       - alert: RedisRejectedConnections
         expr: increase(redis_rejected_connections_total[1m]) > 0
         for: 0m
         labels:
           severity: critical
         annotations:
           summary: Redis rejected connections (instance {{ $labels.instance }})
           description: "Some connections to Redis has been rejected\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"

Grafana部署图

单节点监控,推荐模板ID为:11835;

集群方式监控,推荐模板ID为:14615

  • dashboard效果图如下**

单节点监控:

相关推荐
XINGTECODE40 分钟前
海盗王集成网关和商城服务端功能golang版
开发语言·后端·golang
程序猿进阶1 小时前
堆外内存泄露排查经历
java·jvm·后端·面试·性能优化·oom·内存泄露
FIN技术铺1 小时前
Spring Boot框架Starter组件整理
java·spring boot·后端
凡人的AI工具箱1 小时前
15分钟学 Go 第 60 天 :综合项目展示 - 构建微服务电商平台(完整示例25000字)
开发语言·后端·微服务·架构·golang
先天牛马圣体1 小时前
如何提升大型AI模型的智能水平
后端
java亮小白19971 小时前
Spring循环依赖如何解决的?
java·后端·spring
2301_811274312 小时前
大数据基于Spring Boot的化妆品推荐系统的设计与实现
大数据·spring boot·后端
草莓base2 小时前
【手写一个spring】spring源码的简单实现--容器启动
java·后端·spring
Ljw...3 小时前
表的增删改查(MySQL)
数据库·后端·mysql·表的增删查改
编程重生之路3 小时前
Springboot启动异常 错误: 找不到或无法加载主类 xxx.Application异常
java·spring boot·后端