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
...
相关推荐
1.14(java)10 小时前
MySQL数据库操作全攻略
java·数据库·mysql
jmxwzy10 小时前
MySQL
数据库·mysql
自己的九又四分之三站台11 小时前
PostgreSQL:万物皆可PostgreSQL的力量
数据库·postgresql
一条大祥脚11 小时前
25.12.30
数据库·redis·缓存
雨中飘荡的记忆11 小时前
MyBatis SQL执行模块详解
数据库·sql·mybatis
飞Link12 小时前
【MySQL】Linux(CentOS7)下安装MySQL8教程
linux·数据库·mysql
程可爱12 小时前
详解Redis消息队列的三种实现方案
redis
数据库生产实战12 小时前
Oracle的_segment_count和3个event对高并发事务与索引性能的影响分析
数据库·oracle
程序员侠客行13 小时前
Mybatis二级缓存实现详解
java·数据库·后端·架构·mybatis
Tipriest_13 小时前
linux中的文本分接流tee命令介绍
linux·服务器·数据库