Docker 安装 Redis
大约 8 分钟约 2422 字
Docker 安装 Redis
简介
Redis(Remote Dictionary Server)是一个开源的内存数据结构存储系统,可以用作数据库、缓存和消息代理。它支持多种数据结构(字符串、哈希、列表、集合、有序集合等),并提供丰富的功能特性(发布订阅、Lua 脚本、事务、持久化等),是目前最流行的 NoSQL 内存数据库之一。
使用 Docker 部署 Redis 可以快速搭建开发和测试环境,通过合理的配置文件挂载和数据持久化设置,也能满足中小型生产环境的需求。本文将详细介绍使用 Docker 部署 Redis 的完整流程,包括配置文件管理、数据持久化、内存管理、安全认证以及集群模式的部署。
环境准备
系统要求
| 项目 | 最低要求 | 推荐配置 |
|---|---|---|
| 操作系统 | CentOS 7+ / Ubuntu 18.04+ | CentOS 7.9 |
| 内存 | 256MB | 1GB+ |
| 磁盘 | 1GB | 10GB+ SSD |
| Docker | 19.03+ | 最新稳定版 |
端口规划
| 端口 | 说明 |
|---|---|
| 6379 | Redis 默认服务端口 |
| 16379 | Redis Sentinel 端口(高可用模式) |
| 26379 | Redis Cluster 总线端口(集群模式) |
第一步:准备配置文件
Redis Docker 镜像默认无配置文件启动,但在生产环境中,通过配置文件管理 Redis 参数是最佳实践。
创建配置文件
# 创建配置文件目录
mkdir -p /etc/redis/conf
mkdir -p /etc/redis/data
# 创建 Redis 配置文件
cat > /etc/redis/conf/redis.conf <<'EOF'
# ========== 基础配置 ==========
# 绑定地址(允许外部访问)
bind 0.0.0.0
# 监听端口
port 6379
# 密码认证(生产环境必须设置)
requirepass your_strong_password
# 非后台模式(Docker 容器必须为 no,否则容器会因无前台进程而退出)
daemonize no
# ========== 内存管理 ==========
# 最大内存限制(根据服务器内存设置)
maxmemory 1gb
# 内存淘汰策略(LRU 最近最少使用)
maxmemory-policy allkeys-lru
# ========== 持久化配置 ==========
# RDB 持久化(快照)
save 900 1
save 300 10
save 60 10000
# RDB 文件名
dbfilename dump.rdb
# RDB 文件目录
dir /data
# 开启 AOF 持久化
appendonly yes
# AOF 文件名
appendfilename "appendonly.aof"
# AOF 同步策略(everysec 每秒同步,平衡性能和数据安全)
appendfsync everysec
# AOF 重写触发条件
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
# ========== 日志配置 ==========
# 日志级别
loglevel notice
# 日志输出到标准输出(Docker 推荐)
logfile ""
# ========== 客户端配置 ==========
# 最大客户端连接数
maxclients 10000
# 客户端空闲超时(秒),0 表示禁用
timeout 300
# TCP keepalive
tcp-keepalive 300
# ========== 安全配置 ==========
# 禁用危险命令
rename-command FLUSHDB ""
rename-command FLUSHALL ""
rename-command CONFIG ""
rename-command DEBUG ""
EOFdaemonize 必须为 no
在 Docker 容器中,daemonize 必须设置为 no。如果设置为 yes,Redis 会以守护进程方式在后台运行,导致容器主进程退出,Docker 认为容器已停止而将其关闭。
配置文件关键参数说明
| 参数 | 说明 | 推荐值 |
|---|---|---|
daemonize | 是否以守护进程运行 | no(Docker 必须为 no) |
bind | 绑定地址 | 0.0.0.0 或 127.0.0.1 |
port | 监听端口 | 6379 |
requirepass | 访问密码 | 生产环境必须设置 |
maxmemory | 最大内存限制 | 根据服务器内存设定 |
maxmemory-policy | 内存淘汰策略 | allkeys-lru |
appendonly | 开启 AOF 持久化 | yes |
appendfsync | AOF 同步策略 | everysec |
save | RDB 快照触发条件 | 按需配置 |
第二步:启动 Redis 容器
基础启动命令
docker run \
-p 6379:6379 \
--restart=always \
--name redis \
-v /etc/redis/conf/redis.conf:/etc/redis/redis.conf \
-v /etc/redis/data:/data \
-d redis:7.0 \
redis-server /etc/redis/redis.conf \
--requirepass 'your_strong_password' \
--appendonly yes参数详解
| 参数 | 说明 |
|---|---|
-p 6379:6379 | 端口映射,宿主机 6379 -> 容器 6379 |
--restart=always | Docker 服务重启后自动启动 |
--name redis | 容器名称 |
-v /etc/redis/conf/redis.conf:/etc/redis/redis.conf | 配置文件挂载 |
-v /etc/redis/data:/data | 数据目录挂载 |
-d redis:7.0 | 后台运行 Redis 7.0 |
redis-server /etc/redis/redis.conf | 使用配置文件启动 |
--requirepass 'your_strong_password' | 命令行指定密码(会覆盖配置文件) |
--appendonly yes | 开启 AOF 持久化 |
命令行参数与配置文件
命令行参数(如 --requirepass、--appendonly)的优先级高于配置文件。如果同时在配置文件和命令行中设置了相同的参数,命令行参数会覆盖配置文件的值。
第三步:容器管理
开机自启动配置
# Docker 容器的 --restart 参数说明
# no - 容器退出时,不重启容器
# on-failure - 只有在非 0 状态退出时才重新启动容器
# always - 无论退出状态如何,都重启容器
# unless-stopped - 类似 always,但手动停止后不会自动重启如果启动时未指定 restart 参数
# 通过 update 命令修改重启策略
docker update --restart=always redis
# 查看所有容器
docker ps -a
# 通过容器 ID 或名称操作容器
docker start redis # 启动容器
docker stop redis # 停止容器
docker restart redis # 重启容器
docker rm redis # 删除容器(需先停止)
docker rm -f redis # 强制删除运行中的容器第四步:防火墙配置
# 查看防火墙当前的放行端口列表
firewall-cmd --list-ports
# 添加 Redis 端口
firewall-cmd --add-port=6379/tcp --permanent
# 重新加载防火墙
firewall-cmd --reload
# 验证端口
firewall-cmd --list-ports第五步:连接与验证
使用 redis-cli 连接
# 进入 Redis 容器
docker exec -it redis bash
# 使用 redis-cli 连接
redis-cli
# 如果设置了密码
redis-cli -a your_strong_password
# 从宿主机直接连接
redis-cli -h 127.0.0.1 -p 6379 -a your_strong_password基本操作验证
# 测试连接
127.0.0.1:6379> PING
PONG
# 设置键值
127.0.0.1:6379> SET name "Redis"
OK
# 获取键值
127.0.0.1:6379> GET name
"Redis"
# 查看服务器信息
127.0.0.1:6379> INFO server
# 查看内存使用情况
127.0.0.1:6379> INFO memory
# 查看客户端连接
127.0.0.1:6379> CLIENT LIST
# 查看数据库大小
127.0.0.1:6379> DBSIZE持久化机制详解
RDB vs AOF
| 特性 | RDB(快照) | AOF(追加日志) |
|---|---|---|
| 原理 | 定时将内存数据快照到磁盘 | 记录每次写操作到日志文件 |
| 恢复速度 | 快(直接加载快照) | 慢(需要重放所有写操作) |
| 文件大小 | 小(二进制压缩) | 大(文本格式) |
| 数据安全性 | 可能丢失最后一次快照后的数据 | 最多丢失 1 秒的数据 |
| 性能影响 | fork 子进程时有短暂阻塞 | 写入日志有轻微性能开销 |
| 适用场景 | 备份、灾备恢复 | 数据安全要求高的场景 |
RDB 持久化配置
# save <seconds> <changes>
# 900 秒内有 1 次修改则触发快照
save 900 1
# 300 秒内有 10 次修改则触发快照
save 300 10
# 60 秒内有 10000 次修改则触发快照
save 60 10000
# 禁用 RDB 持久化
save ""
# RDB 文件压缩
rdbcompression yes
# RDB 文件校验
rdbchecksum yesAOF 持久化配置
# 开启 AOF
appendonly yes
# AOF 同步策略
# always - 每次写操作都同步(最安全,最慢)
# everysec - 每秒同步一次(推荐,平衡安全和性能)
# no - 由操作系统决定何时同步(最快,最不安全)
appendfsync everysec
# AOF 文件重写
auto-aof-rewrite-percentage 100 # AOF 文件大小增长 100% 时触发重写
auto-aof-rewrite-min-size 64mb # AOF 文件最小 64MB 才触发重写
# 混合持久化(Redis 4.0+)
aof-use-rdb-preamble yes内存管理与淘汰策略
内存淘汰策略
| 策略 | 说明 |
|---|---|
noeviction | 不淘汰(默认),内存满时写入操作返回错误 |
allkeys-lru | 从所有键中淘汰最近最少使用的键 |
volatile-lru | 从设置了过期时间的键中淘汰最近最少使用的 |
allkeys-random | 从所有键中随机淘汰 |
volatile-random | 从设置了过期时间的键中随机淘汰 |
volatile-ttl | 淘汰 TTL 最短的键 |
allkeys-lfu | 从所有键中淘汰使用频率最低的(Redis 4.0+) |
volatile-lfu | 从设置了过期时间的键中淘汰使用频率最低的 |
# 动态修改淘汰策略
127.0.0.1:6379> CONFIG SET maxmemory-policy allkeys-lru
OK
# 查看当前淘汰策略
127.0.0.1:6379> CONFIG GET maxmemory-policyDocker Compose 部署
单机模式
# docker-compose-redis.yml
version: '3.8'
services:
redis:
image: redis:7.0
container_name: redis
restart: always
ports:
- "6379:6379"
volumes:
- ./redis/conf/redis.conf:/etc/redis/redis.conf
- ./redis/data:/data
command: redis-server /etc/redis/redis.conf
networks:
- app-net
redis-commander:
image: rediscommander/redis-commander:latest
container_name: redis-commander
restart: always
ports:
- "8081:8081"
environment:
REDIS_HOSTS: local:redis:6379:0:your_strong_password
depends_on:
- redis
networks:
- app-net
networks:
app-net:
driver: bridgeSentinel 高可用模式
# docker-compose-redis-sentinel.yml
version: '3.8'
services:
redis-master:
image: redis:7.0
container_name: redis-master
restart: always
ports:
- "6379:6379"
volumes:
- ./master/conf/redis.conf:/etc/redis/redis.conf
- ./master/data:/data
command: redis-server /etc/redis/redis.conf
networks:
- redis-net
redis-slave1:
image: redis:7.0
container_name: redis-slave1
restart: always
ports:
- "6380:6379"
volumes:
- ./slave1/data:/data
command: redis-server --slaveof redis-master 6379 --masterauth your_strong_password --requirepass your_strong_password
depends_on:
- redis-master
networks:
- redis-net
redis-sentinel1:
image: redis:7.0
container_name: redis-sentinel1
restart: always
ports:
- "26379:26379"
command: redis-sentinel /etc/redis/sentinel.conf
volumes:
- ./sentinel1/conf/sentinel.conf:/etc/redis/sentinel.conf
depends_on:
- redis-master
networks:
- redis-net
networks:
redis-net:
driver: bridge常见问题排查
容器启动失败
# 查看容器日志
docker logs redis
# 常见原因1:配置文件语法错误
# 解决:检查 redis.conf 中的参数格式
# 常见原因2:daemonize 设置为 yes
# 解决:将 daemonize 改为 no
# 常见原因3:端口被占用
ss -tlnp | grep 6379
# 常见原因4:目录权限问题
ls -la /etc/redis/data
chmod -R 755 /etc/redis/data连接被拒绝
# 检查容器状态
docker ps | grep redis
# 检查 Redis 是否在监听
docker exec redis redis-cli -a your_strong_password PING
# 检查 bind 配置
# 如果 bind 为 127.0.0.1,外部无法连接
# 改为 bind 0.0.0.0
# 检查防火墙
firewall-cmd --list-ports内存使用过高
# 查看 Redis 内存使用
docker exec redis redis-cli -a your_strong_password INFO memory
# 查看占用内存最多的键
docker exec redis redis-cli -a your_strong_password --bigkeys
# 查看所有键的数量
docker exec redis redis-cli -a your_strong_password DBSIZE
# 清理缓存(谨慎使用)
docker exec redis redis-cli -a your_strong_password FLUSHDB性能优化建议
- 关闭持久化:纯缓存场景可以关闭 RDB 和 AOF,大幅提升性能
- 合理设置 maxmemory:建议为物理内存的 50%-70%
- 使用 Pipeline:批量执行命令减少网络往返
- 禁用 THP:
echo never > /sys/kernel/mm/transparent_hugepage/enabled - 使用连接池:应用端使用 Redis 连接池
