redis-cluster部署手册(helm版本)
使用 Helm 部署 Redis 集群
前提条件
- Kubernetes 集群已搭建完成。
- Helm 已安装完成。
Redis 集群部署流程
1. 部署 Redis 集群
使用 Helm 部署 Redis 集群,并将其存储配置为使用 NFS Provisioner。
1.1 添加 Helm 仓库
添加 Bitnami 的 Helm 仓库:
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
1.2 准备 Redis 集群的 Values 配置文件
创建一个名为 redis-cluster-values.yaml 的文件,内容如下:
## ========================================
## 1. 基础配置
## ========================================
## 覆盖默认名称
fullnameOverride: "redis-cluster"
## ========================================
## 2. 安全配置(必须配置)
## ========================================
## 启用密码认证
usePassword: true
## 方式一:直接设置密码(开发环境)
password: "psd123" # 生产环境不要用这个
## 方式二:使用Secret(生产环境推荐)
# existingSecret: "redis-auth-secret"
# existingSecretPasswordKey: "redis-password"
## ========================================
## 3. 集群配置
## ========================================
## 集群节点数(3主3从)
cluster:
nodes: 6 # 3个主节点 + 3个从节点
replicas: 1 # 每个主节点1个副本
## 集群初始化
init: true
## ========================================
## 4. 持久化配置
## ========================================
persistence:
enabled: false
size: 8Gi # 每个节点8GB
## 使用指定的存储类
# storageClass: "gp3" # AWS
# storageClass: "premium-rwo" # GKE
# storageClass: "managed-csi-premium" # AKS
accessModes:
- ReadWriteOnce
## ========================================
## 5. Redis 主配置
## ========================================
redis:
## 资源限制
resources:
requests:
memory: "256Mi"
cpu: "100m"
limits:
memory: "1Gi"
cpu: "500m"
##健康检查
livenessProbe:
initialDelaySeconds: 30 # Redis启动需要时间
readinessProbe:
initialDelaySeconds: 30
## Pod调度策略, 避免Pod调度到同一节点
podAntiAffinityPreset: "soft" # 开发用soft,生产用hard
## 节点选择器(可选)
# nodeSelector:
# node-type: redis
## 容忍度(可选)
# tolerations:
# - key: "dedicated"
# operator: "Equal"
# value: "redis"
# effect: "NoSchedule"
## ========================================
## 6. 网络策略(安全)
## ========================================
networkPolicy:
enabled: true
allowExternal: true # 允许外部访问
## ========================================
## 7. 服务配置
## ========================================
service:
type: ClusterIP # 内部访问
## 如果需要外部访问(不推荐直接暴露)
# type: LoadBalancer
# annotations:
# service.beta.kubernetes.io/aws-load-balancer-internal: "true"
## ========================================
## 8. 监控配置(可选)
## ========================================
metrics:
enabled: false # 启用Prometheus指标
1.2 安装 Redis 集群
使用 Helm 安装 Redis 集群,并配置使用 NFS Provisioner 提供的存储:
helm install redis-cluster bitnami/redis-cluster \
--namespace redis-cluster \
--create-namespace \
--version 12.0.1 \
-f redis-cluster-values.yaml
2. 验证部署
2.1 检查 Pod 状态
部署完成后,可以使用以下命令检查 Redis 集群的 Pod 状态:
kubectl get pods -n redis-cluster
2.2 检查 Service 状态
检查 Redis 集群的 Service 状态:
kubectl get svc -n redis-cluster
确保所有 Service 都正常运行。
3. 部署redis-cluster-proxy(Camellia Redis Proxy)
因redis官方提供的redis-cluster-proxy长久未更新,可以使用网易云提供的camellia-redis-proxy来代理redis集群
3.1 官方yaml源
3.2 自定义官方yaml源
以下是一个自定义的 YAML 文件,用于部署 Camellia Redis Proxy:
#创建camellia-redis-cluster-proxy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: camellia-redis-cluster-proxy
namespace: redis-cluster #保持与redis-cluster部署在同一个命名空间中
labels:
app: camellia-redis-cluster-proxy
spec:
replicas: 3
selector:
matchLabels:
app: camellia-redis-cluster-proxy
template:
metadata:
labels:
app: camellia-redis-cluster-proxy
spec:
enableServiceLinks: false
containers:
- name: container-camellia
image: 48n6e/camellia-redis-proxy:1.3.2-jdk-21-0.0.1
command:
- java
- "-XX:+UseG1GC"
- "-XX:+UseContainerSupport"
- "-Dio.netty.tryReflectionSetAccessible=true"
- "--add-opens"
- "java.base/java.lang=ALL-UNNAMED"
- "--add-opens"
- "java.base/java.io=ALL-UNNAMED"
- "--add-opens"
- "java.base/java.math=ALL-UNNAMED"
- "--add-opens"
- "java.base/java.net=ALL-UNNAMED"
- "--add-opens"
- "java.base/java.nio=ALL-UNNAMED"
- "--add-opens"
- "java.base/java.security=ALL-UNNAMED"
- "--add-opens"
- "java.base/java.text=ALL-UNNAMED"
- "--add-opens"
- "java.base/java.time=ALL-UNNAMED"
- "--add-opens"
- "java.base/java.util=ALL-UNNAMED"
- "--add-opens"
- "java.base/jdk.internal.access=ALL-UNNAMED"
- "--add-opens"
- "java.base/jdk.internal.misc=ALL-UNNAMED"
- "--add-opens"
- "java.base/sun.net.util=ALL-UNNAMED"
- "-Xms100m"
- "-Xmx4096m"
- "-server"
- "org.springframework.boot.loader.JarLauncher"
resources:
requests:
memory: "100Mi"
cpu: "1"
limits:
memory: "4Gi"
cpu: "1"
ports:
- containerPort: 6380
name: db
protocol: TCP
volumeMounts:
- name: camellia-redis-cluster-proxy
mountPath: /opt/camellia-redis-proxy/BOOT-INF/classes/application.yml
subPath: application-sharding-singlewrite-multiread.yml
volumes:
- name: camellia-redis-cluster-proxy
configMap:
name: camellia-redis-cluster-proxy
---
apiVersion: v1
kind: ConfigMap
metadata:
name: camellia-redis-cluster-proxy
namespace: redis-cluster
data:
application-sharding-singlewrite-multiread.yml: |
server:
port: 6380
spring:
application:
name: camellia-redis-proxy-server
camellia-redis-proxy:
console-port: 16379
password: psd123 #代理密码,部署时请替换
monitor-enable: true
monitor-interval-seconds: 60
plugins:
- monitorPlugin
- bigKeyPlugin
- hotKeyPlugin
- hotKeyCachePlugin
config:
"check.redis.resource.valid.not.active.threshold.sceonds": 300
transpond:
type: local
local:
type: simple
resource: redis-cluster://psd123@redis-cluster.redis-cluster.svc.cluster.local:6379 ##替换成redis-cluster service域名
redis-conf:
sharding-func: com.netease.nim.camellia.core.client.env.CRC16HashTagShardingFunc
preheat: false
close-idle-connection: true
check-idle-connection-threshold-seconds: 600
close-idle-connection-delay-seconds: 60
---
apiVersion: v1
kind: Service
metadata:
name: camellia-redis-cluster-proxy
namespace: redis-cluster
annotations:
prometheus.io/scrape: 'true'
prometheus.io/port: '16379'
prometheus.io/services: 'false'
prometheus.io/scrape_slow: 'true'
prometheus.io/path: '/metrics'
spec:
ports:
- name: port-6380
protocol: TCP
port: 6380
targetPort: 6380
- name: port-16379
protocol: TCP
port: 16379
targetPort: 16379
type: ClusterIP
selector:
app: camellia-redis-cluster-proxy
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: redis-cluster-proxy-ingress
namespace: redis-cluster
spec:
ingressClassName: nginx
rules:
- host: redis.xxx.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: camellia-redis-cluster-proxy
port:
number: 6380
3.3 部署
将上述 YAML 文件保存为 camellia-redis-cluster-proxy.yaml,然后使用以下命令部署:
kubectl apply -f camellia-redis-cluster-proxy.yaml
3.4 访问方式
- Ingress 访问:https://redis.xxx.com(注意:ingress-nginx必须开启tcp代理,客户端(如:aspnetcore)才可通过域名使用)

浙公网安备 33010602011771号