目录

使用 Kubernetes 部署 Redis 主从及 Sentinel 高可用架构(未做共享存储版)

文章目录

  • [使用 Kubernetes 部署 Redis 主从及 Sentinel 高可用架构](#使用 Kubernetes 部署 Redis 主从及 Sentinel 高可用架构)
  • [Redis 主从架构部署 (`1.yaml`)](#Redis 主从架构部署 (1.yaml))
  • [Redis Sentinel 部署 (`2.yaml`)](#Redis Sentinel 部署 (2.yaml))
  • [Sentinel 服务暴露 (`3.yaml`)](#Sentinel 服务暴露 (3.yaml))
  • 部署步骤
  • 总结

使用 Kubernetes 部署 Redis 主从及 Sentinel 高可用架构

本文将详细介绍如何在 Kubernetes 中部署 Redis 主从及 Sentinel 高可用架构,提供完整的 YAML 配置文件,并逐步解析其关键配置。命名空间已调整为 test

我这是单点,未做共享存储版


Redis 主从架构部署 (1.yaml)

首先,使用 StatefulSet 部署 Redis 主从结构,每个实例通过主机名确定角色:

yaml 复制代码
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
  namespace: test
spec:
  serviceName: redis
  replicas: 3
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      initContainers:
      - name: init-redis
        image: docker.wctmd.us.kg/busybox:latest
        command: ["/bin/sh", "-c"]
        args: ["mkdir -p /data/redis/$(hostname)"]
        volumeMounts:
        - name: redis-data
          mountPath: /data
      containers:
      - name: redis
        image: docker.wctmd.us.kg/redis:5.0.14
        ports:
        - containerPort: 6379
        command: ["/bin/sh"]
        args: 
        - "-c"
        - |
          if [ $(hostname) = "redis-0" ]; then
            redis-server --port 6379 --protected-mode no --replica-announce-ip $(hostname).redis
          else
            redis-server --port 6379 --protected-mode no --replicaof redis-0.redis 6379 --replica-announce-ip $(hostname).redis
          fi
        volumeMounts:
        - name: redis-data
          mountPath: /data
      volumes:
      - name: redis-data
        hostPath:
          path: /data/redis
          type: DirectoryOrCreate

关键点解析

  • 主从自动识别redis-0 为主节点,其它 Pod 配置为从节点并通过 --replicaof 指定主节点。
  • 数据持久化 :使用 hostPath 存储 Redis 数据,映射到宿主机的 /data/redis
  • 独立目录管理 :通过 initContainer 动态创建 /data/redis/$(hostname),避免目录冲突。

Redis Sentinel 部署 (2.yaml)

接下来,部署 Sentinel 来监控 Redis 主从并实现自动故障转移:

yaml 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-sentinel
  namespace: test
spec:
  replicas: 3
  selector:
    matchLabels:
      app: redis-sentinel
  template:
    metadata:
      labels:
        app: redis-sentinel
    spec:
      containers:
      - name: redis-sentinel
        image: docker.wctmd.us.kg/redis:5.0.14
        ports:
        - containerPort: 26379
        command: [ "redis-sentinel" ]
        args: ["/data/sentinel.conf"]
        volumeMounts:
        - name: sentinel-conf
          mountPath: /data
      initContainers:
      - name: init-sentinel
        image: docker.wctmd.us.kg/busybox:latest
        command: ["/bin/sh", "-c"]
        args:
        - cp /etc/redis/sentinel.conf /data/sentinel.conf && chmod 600 /data/sentinel.conf
        volumeMounts:
        - name: sentinel-conf
          mountPath: /data
        - name: config-volume
          mountPath: /etc/redis
      volumes:
      - name: sentinel-conf
        emptyDir: {}
      - name: config-volume
        configMap:
          name: sentinel-config
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: sentinel-config
  namespace: test
data:
  sentinel.conf: |
    sentinel monitor mymaster redis-0.redis 6379 2
    sentinel down-after-milliseconds mymaster 5000
    sentinel failover-timeout mymaster 60000
    sentinel parallel-syncs mymaster 1

关键点解析

  • 动态配置文件管理 :通过 initContainer 将只读的 ConfigMap 文件复制到可写路径,并调整权限。
  • 多实例部署:使用 Deployment 创建 3 个 Sentinel 实例以实现高可用。
  • 监控配置ConfigMap 定义了 Sentinel 的监控规则,包括主节点、超时、同步参数等。

Sentinel 服务暴露 (3.yaml)

最后,通过 Service 将 Sentinel 对外暴露,便于外部访问:

yaml 复制代码
apiVersion: v1
kind: Service
metadata:
  name: redis-sentinel
  namespace: test
spec:
  type: NodePort
  ports:
  - nodePort: 30154
    port: 26379
    targetPort: 26379
    protocol: TCP
  selector:
    app: redis-sentinel

关键点解析

  • 外部访问支持 :使用 NodePort 暴露 Sentinel 服务,监听宿主机的 30154 端口。
  • 服务选择器 :通过标签 app: redis-sentinel 将流量路由到 Sentinel Pod。

部署步骤

  1. 创建命名空间 test

    bash 复制代码
    kubectl create namespace test
  2. 应用 1.yaml 部署 Redis:

    bash 复制代码
    kubectl apply -f 1.yaml
  3. 应用 2.yaml 部署 Sentinel:

    bash 复制代码
    kubectl apply -f 2.yaml
  4. 应用 3.yaml 创建服务:

    bash 复制代码
    kubectl apply -f 3.yaml
  5. 验证部署状态:

    bash 复制代码
    kubectl -n test get pods
    kubectl -n test get svc

总结

本文展示了如何使用 Kubernetes 部署 Redis 主从架构和 Sentinel 高可用集群。通过灵活的 StatefulSetDeployment 配置,我们实现了高可靠性和动态配置管理,同时确保数据持久化和集群安全性。

建议 :在生产环境中,可以进一步优化存储方案(如使用 PersistentVolume)并增强安全配置(如启用 Redis/Sentinel 认证)。

本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
Kale又菜又爱玩9 分钟前
Sentinel全面解析与实战教程
java·spring·微服务·sentinel·springboot·springcloud
花心蝴蝶.2 小时前
初识Redis
数据库·redis·缓存
我是Superman丶3 小时前
【工具】Redis管理工具推荐
运维·数据库·redis
xiaolingting3 小时前
Redis 与 Java HashMap 扩容负载因子差异解析
数据库·redis·hashmap·负载因子·内存碎片·内存敏感·渐进式扩容
三次握手四次挥手6 小时前
Redis解读指南
redis
Connie14517 小时前
在 Kubernetes (k8s) 中,apiserver 的 IIP和 VIP的区别
云原生·容器·kubernetes
〆、风神8 小时前
Spring Boot 自定义 Redis Starter 开发指南(附动态 TTL 实现)
spring boot·redis·后端
一行•坚书9 小时前
Redis客户端命令到服务器底层对象机制的完整流程?什么是Redis对象机制?为什么要有Redis对象机制?
服务器·数据库·redis
Supersist12 小时前
【我要找工作_01】当我在学习Redis时,我到底在学习个啥?
redis·后端
玄明Hanko12 小时前
Redis到底能不能做主数据库?
数据库·redis·后端