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
...
相关推荐
unicrom_深圳市由你创科技9 小时前
MySQL 乐观锁的实际落地:避免并发更新冲突的 3 种实现方式
数据库·mysql
zhujian8263710 小时前
二十八、【鸿蒙 NEXT】orm框架
数据库·华为·sqlite·harmonyos·orm框架
数据安全科普王10 小时前
HTTP缓存机制详解:强缓存 vs 协商缓存
网络协议·http·缓存
小码吃趴菜10 小时前
MySQL远程连接
数据库·mysql
被星1砸昏头10 小时前
高级爬虫技巧:处理JavaScript渲染(Selenium)
jvm·数据库·python
王文搏10 小时前
MySQL 常用函数用法速查(含解释与示例)
数据库·mysql·adb
信创天地10 小时前
国产关系型数据库部署与权限管理实战:人大金仓、达梦、南大通用、华为GaussDB
数据库·华为·gaussdb
l1t10 小时前
psql 中的流水线操作(PostgreSQL 18)
数据库·人工智能·postgresql
enfpZZ小狗10 小时前
使用Flask快速搭建轻量级Web应用
jvm·数据库·python
Geek攻城猫10 小时前
深入理解 SQL 多字段排序:从基础到高级技巧
数据库·sql