1 使用docker部署单机的redis
1.拉取Redis镜像
# 拉取最新版本
docker pull redis:latest# 或指定版本
docker pull redis:7.2
2. 创建Redis配置文件
# 创建配置文件目录
mkdir -p /docker/redis/conf
mkdir -p /docker/redis/data# 创建Redis配置文件
nano /docker/redis/conf/redis.conf配置文件内容如下:# 基础配置
port 6379
bind 0.0.0.0
protected-mode no
daemonize no# 持久化配置
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec# 内存配置
maxmemory 1gb
maxmemory-policy allkeys-lru# 日志配置
loglevel notice
logfile "/data/redis.log"# 密码配置(可选)
requirepass your_password
3.启动Redis容器
docker run -d \--name redis \--restart always \-p 6379:6379 \-v /docker/redis/conf/redis.conf:/etc/redis/redis.conf \-v /docker/redis/data:/data \redis:latest \redis-server /etc/redis/redis.conf
4 测试连接
# 进入容器
docker exec -it redis redis-cli# 如果设置了密码,需要认证
AUTH your_password# 测试
SET test "Hello Redis"
GET test
2 Redis集群安装
1 创建Docker网络
docker network create redis-net
2 创建集群配置文件
为每个节点创建配置文件:
# 创建目录结构
mkdir -p /docker/redis-cluster/7001/conf
mkdir -p /docker/redis-cluster/7001/data
mkdir -p /docker/redis-cluster/7002/conf
mkdir -p /docker/redis-cluster/7002/data
mkdir -p /docker/redis-cluster/7003/conf
mkdir -p /docker/redis-cluster/7003/data
mkdir -p /docker/redis-cluster/7004/conf
mkdir -p /docker/redis-cluster/7004/data
mkdir -p /docker/redis-cluster/7005/conf
mkdir -p /docker/redis-cluster/7005/data
mkdir -p /docker/redis-cluster/7006/conf
mkdir -p /docker/redis-cluster/7006/data
3.创建配置文件
nano /docker/redis-cluster/7001/conf/redis.conf
nano /docker/redis-cluster/7002/conf/redis.conf
nano /docker/redis-cluster/7003/conf/redis.conf
nano /docker/redis-cluster/7004/conf/redis.conf
nano /docker/redis-cluster/7005/conf/redis.conf
nano /docker/redis-cluster/7006/conf/redis.conf
配置内容:
port 7001
bind 0.0.0.0
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.20.0.11
cluster-announce-port 7001
cluster-announce-bus-port 17001port 7002
bind 0.0.0.0
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.20.0.12
cluster-announce-port 7002
cluster-announce-bus-port 17002port 7003
bind 0.0.0.0
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.20.0.13
cluster-announce-port 7003
cluster-announce-bus-port 17003port 7004
bind 0.0.0.0
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.20.0.14
cluster-announce-port 7004
cluster-announce-bus-port 17004port 7005
bind 0.0.0.0
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.20.0.15
cluster-announce-port 7005
cluster-announce-bus-port 17005port 7006
bind 0.0.0.0
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.20.0.16
cluster-announce-port 7006
cluster-announce-bus-port 17006
3.创建docker-compose.yml
nano /docker/redis-cluster/docker-compose.yml内容如下version: 'docker-compose 版本号'
services:redis1:image: redis:latestcontainer_name: redis1networks:redis-net:ipv4_address: 172.20.0.11volumes:- ./7001/conf/redis.conf:/etc/redis/redis.conf- ./7001/data:/datacommand: redis-server /etc/redis/redis.confports:- "7001:7001"- "17001:17001"redis2:image: redis:latestcontainer_name: redis2networks:redis-net:ipv4_address: 172.20.0.12volumes:- ./7002/conf/redis.conf:/etc/redis/redis.conf- ./7002/data:/datacommand: redis-server /etc/redis/redis.confports:- "7002:7002"- "17002:17002"# 重复类似配置直到redis6# ...networks:redis-net:driver: bridgeipam:config:- subnet: 172.20.0.0/16
4.启动集群
# 启动所有容器
cd /docker/redis-cluster
docker-compose up -d
# 进入redis1 创建集群
docker exec -it redis1 redis-cli --cluster create \172.20.0.11:7001 \172.20.0.12:7002 \172.20.0.13:7003 \172.20.0.14:7004 \172.20.0.15:7005 \172.20.0.16:7006 \--cluster-replicas 1
3 监控和管理
1 安装Redis Commander(Web管理界面)
docker run -d \--name redis-commander \--restart always \-p 8081:8081 \-e REDIS_HOSTS=local:redis:6379 \rediscommander/redis-commander:latest
2.常用管理命令
# 查看容器状态
docker ps | grep redis# 查看容器日志
docker logs redis# 查看集群信息
docker exec -it redis1 redis-cli cluster info# 查看节点信息
docker exec -it redis1 redis-cli cluster nodes
4 备份和恢复
1 创建备份脚本
#!/bin/bash
BACKUP_DIR="/docker/redis/backups"
DATE=$(date +%Y%m%d_%H%M%S)
mkdir -p $BACKUP_DIR# 创建备份
docker exec redis redis-cli SAVE
docker cp redis:/data/dump.rdb $BACKUP_DIR/dump_$DATE.rdb
2 设置定时备份
# 编辑crontab
crontab -e# 添加定时任务(每天凌晨2点备份)
0 2 * * * /docker/redis/backup.sh
5.性能优化建议
# redis.conf 性能优化配置# 内存优化
maxmemory-samples 10
activedefrag yes
active-defrag-ignore-bytes 100mb
active-defrag-threshold-lower 10
active-defrag-threshold-upper 30# 持久化优化
save 900 1
save 300 10
save 60 10000# 网络优化
tcp-keepalive 300
timeout 0
tcp-backlog 511
6 安全配置
# redis.conf 安全配置# 设置访问密码
requirepass your_strong_password# 禁用危险命令
rename-command FLUSHDB ""
rename-command FLUSHALL ""
rename-command CONFIG ""
rename-command KEYS ""# 设置最大连接数
maxclients 10000