Redis单节点分片集群实现

(/≧▽≦)/~┴┴ 嗨~我叫小奥 ✨✨✨

👀👀👀 个人博客:小奥的博客

👍👍👍:个人CSDN

⭐️⭐️⭐️:Github传送门

🍹 本人24应届生一枚,技术和水平有限,如果文章中有不正确的内容,欢迎多多指正!

📜 欢迎点赞收藏关注哟! ❤️

文章目录

因为工作中需要使用到单节点redis分片集群,所以在网上参考了别人的一些实现之后,借助ai工具生成的使用方案,均已验证可行。

参考开源项目:

GitHub - diorman/redis-single-node-cluster: Redis cluster with all slots assigned to a single node for development, testing and CI/CD

dockerfile部署

  1. 准备redis.conf配置文件
plain 复制代码
port 6379
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly no
save ""
  1. 准备初始化脚本init.sh
bash 复制代码
#!/bin/sh
set -e
echo "Waiting for Redis to start..."
until redis-cli -p 6379 ping; do sleep 1; done
echo "Assigning all 16384 slots to this node..."
SLOTS=$(seq 0 16383 | tr '\n' ' ')
redis-cli -p 6379 cluster addslots $SLOTS
echo "Cluster setup complete."
redis-cli -p 6379 cluster info
  1. Dockerfile 编写
shell 复制代码
# 使用官方 Redis 镜像作为基础镜像
FROM redis:7.2-alpine

# 创建目录存放配置文件和初始化脚本
WORKDIR /etc/redis

# 将本地的 redis.conf 和 init.sh 拷贝到镜像中
COPY redis.conf /etc/redis/redis.conf
COPY init.sh /etc/redis/init.sh

# 给予 init.sh 可执行权限
RUN chmod +x /etc/redis/init.sh

# 设置容器启动命令
CMD ["sh", "-c", "redis-server /etc/redis/redis.conf --daemonize no && /etc/redis/init.sh"]
  1. 构建镜像和运行容器
shell 复制代码
docker build -t redis-single-node-cluster .
docker run -d --name redis-single-node-cluster -p 6379:6379 redis-single-node-cluster

kubernetes部署

  1. 准备redis-single-node-cluster.yaml如下:
yaml 复制代码
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-cluster-config
data:
  redis.conf: |
    port 6379
    cluster-enabled yes
    cluster-config-file nodes.conf
    cluster-node-timeout 5000
    appendonly no
    save ""
  init.sh: |
    #!/bin/sh
    set -e
    echo "Waiting for Redis to start..."
    until redis-cli -p 6379 ping; do sleep 1; done
    echo "Assigning all 16384 slots to this node..."
    SLOTS=$(seq 0 16383 | tr '\n' ' ')
    redis-cli -p 6379 cluster addslots $SLOTS
    echo "✅ Cluster setup complete."
    redis-cli -p 6379 cluster info
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-single-node-cluster
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis-single-node-cluster
  template:
    metadata:
      labels:
        app: redis-single-node-cluster
    spec:
      containers:
        - name: redis
          image: redis:7.2-alpine
          ports:
            - containerPort: 6379
          command:
            - /bin/sh
            - -c
            - |
              redis-server /etc/redis/redis.conf &
              sleep 3
              /etc/redis/init.sh
              wait
          volumeMounts:
            - name: config
              mountPath: /etc/redis
          resources:
            requests:
              memory: "64Mi"
              cpu: "10m"
            limits:
              memory: "128Mi"
              cpu: "100m"
      volumes:
        - name: config
          configMap:
            name: redis-cluster-config
            defaultMode: 0755
---
apiVersion: v1
kind: Service
metadata:
  name: redis-single-node-cluster
spec:
  selector:
    app: redis-single-node-cluster
  ports:
    - port: 6379
      targetPort: 6379
  type: ClusterIP
  1. 部署
shell 复制代码
# redis-single 为命名空间 
kubectl apply -n redis-single -f redis-single-node-cluster.yaml
  1. 检查服务状态
shell 复制代码
# 查看 Pod 是否创建
kubectl get pod -n redis-single

# 如果 Pod 卡在 ContainerCreating 或 Error,查看日志
kubectl describe pod -n redis-single -l app=redis-single-node-cluster

# 如果 Pod Running 但初始化失败,看日志
kubectl logs -n redis-single -l app=redis-single-node-cluster --tail=50

正常情况下日志如下:

plain 复制代码
Waiting for Redis to start...
Assigning all 16384 slots to this node...
✅ Cluster setup complete.
cluster_state:ok
cluster_slots_assigned:16384
...
相关推荐
科技小花2 小时前
数据治理平台架构演进观察:AI原生设计如何重构企业数据管理范式
数据库·重构·架构·数据治理·ai-native·ai原生
一江寒逸2 小时前
零基础从入门到精通MySQL(中篇):进阶篇——吃透多表查询、事务核心与高级特性,搞定复杂业务SQL
数据库·sql·mysql
D4c-lovetrain2 小时前
linux个人心得22 (mysql)
数据库·mysql
阿里小阿希3 小时前
CentOS7 PostgreSQL 9.2 升级到 15 完整教程
数据库·postgresql
荒川之神3 小时前
Oracle 数据仓库雪花模型设计(完整实战方案)
数据库·数据仓库·oracle
做个文艺程序员3 小时前
MySQL安全加固十大硬核操作
数据库·mysql·安全
不吃香菜学java3 小时前
Redis简单应用
数据库·spring boot·tomcat·maven
一个天蝎座 白勺 程序猿3 小时前
Apache IoTDB(15):IoTDB查询写回(INTO子句)深度解析——从语法到实战的ETL全链路指南
数据库·apache·etl·iotdb
不知名的老吴4 小时前
Redis的延迟瓶颈:TCP栈开销无法避免
数据库·redis·缓存
YOU OU4 小时前
三大范式和E-R图
数据库