一、部署三主三从的Redis集群
分别为6个节点建立挂载目录,每个目录下建立数据、配置、日志文件夹。
docker-compose内容如下:
version: '3'
services:
redis1:
image: redis:6.2.3
restart: always
ports:
- "6379:6379"
- "16379:16379"
volumes:
- "./6379/data:/data/redis"
- "./6379/logs:/opt/redis/logs"
- "./6379/etc/redis.conf:/etc/redis/redis.conf"
command: redis-server /etc/redis/redis.conf
redis2:
image: redis:6.2.3
restart: always
ports:
- "6380:6380"
- "16380:16380"
volumes:
- "./6380/data:/data/redis"
- "./6380/logs:/opt/redis/logs"
- "/opt/redis/cluster/6380/etc/redis.conf:/etc/redis/redis.conf"
command: redis-server /etc/redis/redis.conf
redis3:
image: redis:6.2.3
restart: always
ports:
- "6381:6381"
- "16381:16381"
volumes:
- "./6381/data:/data/redis"
- "./6381/logs:/opt/redis/logs"
- "./6381/etc/redis.conf:/etc/redis/redis.conf"
command: redis-server /etc/redis/redis.conf
redis4:
image: redis:6.2.3
restart: always
ports:
- "7379:7379"
- "17379:17379"
volumes:
- "./7379/data:/data/redis"
- "./7379/logs:/opt/redis/logs"
- "./7379/etc/redis.conf:/etc/redis/redis.conf"
command: redis-server /etc/redis/redis.conf
redis5:
image: redis:6.2.3
restart: always
ports:
- "7380:7380"
- "17380:17380"
volumes:
- "./7380/data:/data/redis"
- "./7380/logs:/opt/redis/logs"
- "./7380/etc/redis.conf:/etc/redis/redis.conf"
command: redis-server /etc/redis/redis.conf
redis6:
image: redis:6.2.3
restart: always
ports:
- "7381:7381"
- "17381:17381"
volumes:
- "./7381/data:/data/redis"
- "./7381/logs:/opt/redis/logs"
- "./7381/etc/redis.conf:/etc/redis/redis.conf"
command: redis-server /etc/redis/redis.conf
redis.conf大同小异,以redis1的配置为例,大部分是默认配置值,主要是修改集群配置部分,和端口部分
bind 0.0.0.0
protected-mode no
# 每个实例有各自的端口,6380,6381等等
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
pidfile /var/run/redis.pid
loglevel verbose
logfile "/opt/redis/logs/redis.log"
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir /data/redis/
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
requirepass 123456
maxmemory 10737418240
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
# 集群关键配置
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
cluster-announce-ip 172.19.23.214
#每个实例改为各自对应的端口
cluster-announce-port 6379
cluster-announce-bus-port 16379
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events Ex
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
使用docker-compose up -d 启动集群,查看状态是否正常,如果启动失败可以查看6379/logs/redis.log查看日志。
进入redis的bin目录下执行,(也可以进入任意一个容器内执行)
./redis-cli --cluster create -a 123456 172.16.19.234:6379 172.16.19.234:7379 172.16.19.234:6380 172.16.19.234:7380 172.16.19.234:6381 172.16.19.234:7381 --cluster-replicas 1
命令会输出一段提示,输入yes同意集群配置,稍等片刻显示成功,这个过程不需要太久(不到一分钟),如果等了很久一直显示......,那就是有问题,但是控制台不会报错,要检查配置、日志看是否有问题。
配置成功后,连接集群
./redis-cli -c -h 172.16.19.234 -p 6379 -a 123456
查看集群信息
查看集群节点
测试命令,根据键值所在槽位,会自动切换到对应节点的连接。
集群模式不支持select 选数据库,只有0号
二、数据迁移
安装redis-dump
npm install redis-dump -g
也有的教程是用
apt-get install ruby ruby-dev gcc
sudo gem install redis-dump
安装的,不知道有什么区别,我两种方式都安装了。
导出:
# -d 表示几号数据库
redis-dump -u 172.16.19.234:6379 -a 123456 -d 0 > data0.json
导入:
cat data0.json | bin/redis-cli -c -h 172.16.19.234 -p 6379 -a 123456
最后使用dbsize命令检查迁移数据是否完整。集群要把每个节点的总和加起来。