2.2 KiB
2.2 KiB
Redis 安装指南
本目录包含在 Kubernetes 集群中安装 Redis 的配置文件。
文件说明
install-redis.sh- Redis 安装脚本redis-deployment.yaml- Redis Deployment 配置redis-service.yaml- Redis Service 配置redis-configmap.yaml- Redis 配置文件
安装步骤
- 确保 kubectl 已安装并配置好集群连接
- 运行安装脚本:
chmod +x install-redis.sh ./install-redis.sh
配置说明
Redis 配置
- 端口: 6379
- 密码: redis123
- 内存限制: 512Mi
- 持久化: 启用 RDB 和 AOF
- 数据目录: /data
服务访问
- 集群内访问:
redis.default.svc.cluster.local:6379 - 密码: redis123
使用示例
连接 Redis
# 获取 Redis Pod 名称
kubectl get pods -l app=redis
# 连接到 Redis
kubectl exec -it <redis-pod-name> -n default -- redis-cli -a redis123
基本操作
# 设置键值
SET mykey "Hello Redis"
# 获取值
GET mykey
# 查看所有键
KEYS *
# 查看 Redis 信息
INFO
从应用连接
# 在应用的配置中使用
redis:
host: redis.default.svc.cluster.local
port: 6379
password: redis123
监控和维护
查看 Redis 状态
kubectl get pods -l app=redis
kubectl logs <redis-pod-name>
查看 Redis 信息
kubectl exec -it <redis-pod-name> -- redis-cli -a redis123 INFO
备份数据
# 创建快照
kubectl exec -it <redis-pod-name> -- redis-cli -a redis123 BGSAVE
# 查看备份文件
kubectl exec -it <redis-pod-name> -- ls -la /data/
故障排除
常见问题
-
Pod 启动失败
- 检查资源限制
- 查看 Pod 日志:
kubectl logs <pod-name>
-
连接被拒绝
- 检查 Service 配置
- 验证网络策略
-
内存不足
- 调整 maxmemory 配置
- 增加 Pod 内存限制
日志查看
kubectl logs -f <redis-pod-name>
生产环境建议
- 持久化存储: 使用 PersistentVolume 替代 emptyDir
- 高可用: 考虑 Redis Sentinel 或 Redis Cluster
- 监控: 集成 Prometheus 监控
- 备份: 定期备份 RDB 和 AOF 文件
- 安全: 使用强密码和网络策略