8000 🚀 Docker 部署 Redis 常见问题排查与解决方案记录 · Issue #43 · zhangwt-cn/notes · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

🚀 Docker 部署 Redis 常见问题排查与解决方案记录 #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
zhangwt-cn opened this issue Nov 29, 2024 · 0 comments
Open

Comments

@zhangwt-cn
Copy link
Owner
zhangwt-cn commented Nov 29, 2024

📝 前言

记录 Docker 部署 Redis 过程,留作参考。

使用版本

  • CentOS 7.9
  • Docker 20.10.9
  • Redis 6.2.16

1. ⚙️ 基础部署配置

1.1 📂 目录结构

首先,我们需要创建必要的目录结构:

mkdir -p /docker/redis/{conf,data}

1.2 📄 基础配置文件

下载指定版本 redis.conf1,解压后将 redis.conf 放到 /docker/redis/conf/ 目录下,并在 /docker/redis/conf/redis.conf 中配置以下基本项:

bind 0.0.0.0
port 6379
requirepass your_password
protected-mode yes
daemonize no
appendonly yes
配置项 示例值 作用 含义 注意事项
bind 0.0.0.0 指定 Redis 监听的网络接口 允许从任何 IP 地址访问 Redis 服务 生产环境建议指定具体 IP,以增加安全性
port 6379 指定 Redis 服务器监听的端口号 6379 是 Redis 的默认端口 客户端通过此端口与 Redis 建立连接
requirepass your_password 设置 Redis 访问密码 客户端连接需要提供密码验证 增加基本的访问安全保护
protected-mode yes 保护模式配置 启用保护模式,增加安全性 当 bind 配置为 0.0.0.0 时,必须设置密码才能从外部访问
daemonize no 是否以守护进程方式运行 设为 no 表示在前台运行 常用于容器化部署,便于容器管理
appendonly yes 持久化策略配置 启用 AOF 持久化机制 • 实时记录写操作
• 数据更安全
• 可以实现数据恢复
• 降低数据丢失风险

这些配置组合在一起,形成了一个基本的、安全的 Redis 服务配置,适合容器化部署环境

1.3 🔨 初始运行命令

docker run -d \
  --name redis \
  --restart always \
  -p 10130:6379 \
  -v /docker/redis/conf/redis.conf:/etc/redis/redis.conf \
  -v /docker/redis/data:/data \
  redis:latest \
  redis-server /etc/redis/redis.conf

2. 🔍 问题排查与解决

2.1 🌐 TCP backlog 问题

问题现象

WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128

💡 解决方案

# 临时解决
sysctl -w net.core.somaxconn=1024

# 永久解决
echo "net.core.somaxconn=1024" >> /etc/sysctl.conf
sysctl -p

# Docker 运行命令添加参数
--sysctl net.core.somaxconn=1024

2.2 💾 内存过度使用警告

问题现象

WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition.

💡 解决方案

# 临时解决
sysctl vm.overcommit_memory=1

# 永久解决
echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
sysctl -p

# Docker 运行命令添加参数
--sysctl vm.overcommit_memory=1

2.3 🔑 后台任务权限问题

问题现象

Fatal: Can't initialize Background Jobs. Error message: Operation not permitted

💡 解决方案

  1. 在 Docker 运行命令中添加 --privileged=true 参数,给予容器必要的系统权限。2
  2. 升级 Docker 版本到 20.10.10 3

2.4 📝 PID 文件写入权限问题

问题现象

Failed to write PID file: Permission denied

💡 解决方案

chmod -R 777 /docker/redis/data
chmod -R 777 /docker/redis/conf

3. ✅ 最终可用配置

3.1 🛠️ 完整的 Docker 运行命令

docker run -d \
  --name redis \
  --restart always \
  -p 10130:6379 \
  -v /docker/redis/conf/redis.conf:/etc/redis/redis.conf \
  -v /docker/redis/data:/data \
  --privileged=true \
  redis:latest \
  redis-server /etc/redis/redis.conf

3.2 ✔️ 验证部署

# 检查容器状态
docker ps | grep redis

# 查看容器日志
docker logs redis

# 测试连接
docker exec -it redis redis-cli -a your_password ping

4. 📚 最佳实践建议

  1. 🏷️ 版本选择

    • 生产环境建议使用指定版本而不是 latest
    • 可以选择较为稳定的版本,如 Redis 6.2.16
  2. 🔐 安全性考虑

    • 修改默认端口
    • 设置强密码
    • 限制容器资源使用
  3. 📊 监控与维护

    • 定期备份数据
    • 监控容器运行状态
    • 定期检查日志

📌 总结

通过逐步排查和解决各种问题,最终实现了 Redis 的稳定运行。关键在于正确处理权限问题和系统参数配置。在实际部署中,建议根据具体环境和需求适当调整配置参数。

Footnotes

  1. Redis Releases

  2. Redis GitHub Issues

  3. GitHub Wiki

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant
0