(/≧▽≦)/~┴┴ 嗨~我叫小奥 ✨✨✨
👀👀👀 个人博客:小奥的博客
👍👍👍:个人CSDN
⭐️⭐️⭐️:Github传送门
🍹 本人24应届生一枚,技术和水平有限,如果文章中有不正确的内容,欢迎多多指正!
📜 欢迎点赞收藏关注哟! ❤️
文章目录
因为工作中需要使用到单节点redis分片集群,所以在网上参考了别人的一些实现之后,借助ai工具生成的使用方案,均已验证可行。
参考开源项目:
dockerfile部署
- 准备
redis.conf配置文件
plain
port 6379
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly no
save ""
- 准备初始化脚本
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
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"]
- 构建镜像和运行容器
shell
docker build -t redis-single-node-cluster .
docker run -d --name redis-single-node-cluster -p 6379:6379 redis-single-node-cluster
kubernetes部署
- 准备
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
- 部署
shell
# redis-single 为命名空间
kubectl apply -n redis-single -f redis-single-node-cluster.yaml
- 检查服务状态
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
...