Redis集群模式搭建

1.集群模式选型

redis集群模式的选型

https://dl.lenses.stream/d/?id=550f16d9-14ee-458e-97d8-8ecfcbb695e9

https://dl.lenses.stream/d/?id=ac2a62f3-1fa5-41ac-95ec-fcbb33eb37fd

2.文献参考

搭建redis5.0集群

https://www.jianshu.com/p/0b94c3a496e3

docker方式搭建redis集群(redis5)

https://www.jianshu.com/p/0172fad7d8d2

3.集群搭建开始

3.1构建容器持久化的文件路径和redis.conf配置文件

两台虚拟机部署6个redis
1.创建redis相目录
node1上:

mkdir -p /root/fct/redis-cluster/redis{7001,7002,7003}
 mkdir -p /root/fct/redis-cluster/redis{7001,7002,7003}/data
 touch /root/fct/redis-cluster/redis{7001,7002,7003}/redis.conf

其中redis.conf配置文件

bind 172.xx.xxx.xxx
protected-mode yes
port 7001
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
loglevel notice
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
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
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
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
requirepass root
masterauth  root
cluster-enabled  yes
cluster-config-file  nodes.conf
cluster-node-timeout  15000

注意:只用配置文件只要修改对应ip和端口即可

3.2 执行如下命令执行将镜像生产容器

docker run -d  \
--network=host \
-v /root/fct/redis-cluster/redis7001/data/:/data:z \
-v /root/fct/redis-cluster/redis7001/redis.conf:/usr/local/etc/redis/redis.conf:z \
--name redis7001 redis \
redis-server /usr/local/etc/redis/redis.conf \



docker run -d  \
--network=host \
-v /root/fct/redis-cluster/redis7002/data/:/data:z \
-v /root/fct/redis-cluster/redis7002/redis.conf:/usr/local/etc/redis/redis.conf:z \
--name redis7002 redis \
redis-server /usr/local/etc/redis/redis.conf 



docker run -d  \
--network=host \
-v /root/fct/redis-cluster/redis7003/data/:/data:z \
-v /root/fct/redis-cluster/redis7003/redis.conf:/usr/local/etc/redis/redis.conf:z \
--name redis7003 redis \
redis-server /usr/local/etc/redis/redis.conf 



docker run -d  \
--network=host \
-v /root/fct/redis-cluster/redis7004/data/:/data:z \
-v /root/fct/redis-cluster/redis7004/redis.conf:/usr/local/etc/redis/redis.conf:z \
--name redis7004 redis \
redis-server /usr/local/etc/redis/redis.conf 





docker run -d  \
--network=host \
-v /root/fct/redis-cluster/redis7005/data/:/data:z \
-v /root/fct/redis-cluster/redis7005/redis.conf:/usr/local/etc/redis/redis.conf:z \
--name redis7005 redis \
redis-server /usr/local/etc/redis/redis.conf 



docker run -d  \
--network=host \
-v /root/fct/redis-cluster/redis7006/data/:/data:z \
-v /root/fct/redis-cluster/redis7006/redis.conf:/usr/local/etc/redis/redis.conf:z \
--name redis7006 redis \
redis-server /usr/local/etc/redis/redis.conf

3.3 将独立的各个redis的node节点串联在一起组成redis集群

./redis-cli -a root --cluster create 172.19.32.142:7001 172.19.32.142:7002 172.19.32.142:7003 172.19.32.146:7004 172.19.32.146:7005 172.19.32.146:7006 --cluster-replicas 1

3.4 测试集群客户端连接情况

使用docker进入redis容器内部,执行客户端命令:

##其中-c相当于--cluster  表示进入集群模式
./redis-cli -c  -h 172.19.32.142 -p 7002

然后使用CLUSTER INFO 和CLUSTER NODES命令检查集群状态

Redis Cluster 集群模式搭建_redis集群

CLUSTER INFO命令

Redis Cluster 集群模式搭建_docker_02

之后可以分别使用java代码连接redis集群测试,同时开始集群高可用性测试,主备切换的功能!

0071