Redis 5.0.9配置cluster集群模式
1、准备工作
准备两台以上已经安装Redis的服务器,这里以两台安装了Redis5.0.9的Centos 7 为例子
Redis安装:https://www.cnblogs.com/chenppp/p/13437212.html
主节点:192.168.199.50:7001|7002|7003
从节点:192.168.199.51:7004|7005|7006
当数据量达到很大时,单台的Redis服务器是无法满足需求的,这时候,就需要对Redis进行集群,由多台机器组成分布式的Redis集群,这样新增节点非常方便
1. 多个redis节点之间数据共享
2. 所有的节点都是一主一从(也可以是一主多从),从节点不提供服务
3. 不支持同时处理多个key(如MSET/MGET),因为redis需要把key均匀分布在各个节点上, 并发量很高的情况下同时创建key-value会降低性能并导致不可预测的行为
4. 支持在线增加、删除节点
5. 客户端可以连任何一个主节点进行读写
2、配置cluster集群
1、在主节点上创建redis_7001_node.conf、redis_7002_node.conf、redis_7003_node.conf,配置文件
配置文件参考,注意修改标红色字段
bind 192.168.199.50 protected-mode yes port 7001 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile /usr/local/redis/logs/redis_7001.pid loglevel notice logfile "/usr/local/redis/logs/redis_7001.log" 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 /usr/local/redis/data/7001 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 cluster-enabled yes cluster-config-file redis_7001_node.conf cluster-node-timeout 10100 requirepass password
2、创建各节点data目录
[root@swarm-node1 redis]# mkdir -p data/{7001,7002,7003} [root@swarm-node1 redis]# [root@swarm-node1 redis]# ls bin conf data logs [root@swarm-node1 redis]# ls data/ 7001 7002 7003 [root@swarm-node1 redis]#
3、启动服务
[root@swarm-node1 bin]# ./redis-server ../conf/redis_7001_node.conf
7131:C 15 Aug 2020 20:48:31.618 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7131:C 15 Aug 2020 20:48:31.618 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=7131, just started
7131:C 15 Aug 2020 20:48:31.618 # Configuration loaded
[root@swarm-node1 bin]# ./redis-server ../conf/redis_7002_node.conf
7136:C 15 Aug 2020 20:48:34.361 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7136:C 15 Aug 2020 20:48:34.361 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=7136, just started
7136:C 15 Aug 2020 20:48:34.361 # Configuration loaded
[root@swarm-node1 bin]# ./redis-server ../conf/redis_7003_node.conf
7141:C 15 Aug 2020 20:48:37.468 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7141:C 15 Aug 2020 20:48:37.468 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=7141, just started
7141:C 15 Aug 2020 20:48:37.468 # Configuration loaded
[root@swarm-node1 bin]#
[root@swarm-node1 bin]# ps -ef |grep redis
root 7132 1 0 20:48 ? 00:00:00 ./redis-server 192.168.199.50:7001 [cluster]
root 7137 1 0 20:48 ? 00:00:00 ./redis-server 192.168.199.50:7002 [cluster]
root 7142 1 0 20:48 ? 00:00:00 ./redis-server 192.168.199.50:7003 [cluster]
root 7147 6880 0 20:48 pts/0 00:00:00 grep --color=auto redis
[root@swarm-node1 bin]#
4、配置从节点,并启动服务
从节点配置文件参数上面案例
启动服务
[root@swarm-node2 bin]# ./redis-server ../conf/redis_7004_node.conf 6995:C 15 Aug 2020 08:53:26.228 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 6995:C 15 Aug 2020 08:53:26.228 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=6995, just started 6995:C 15 Aug 2020 08:53:26.228 # Configuration loaded [root@swarm-node2 bin]# ./redis-server ../conf/redis_7005_node.conf 7000:C 15 Aug 2020 08:53:29.253 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 7000:C 15 Aug 2020 08:53:29.253 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=7000, just started 7000:C 15 Aug 2020 08:53:29.253 # Configuration loaded [root@swarm-node2 bin]# ./redis-server ../conf/redis_7006_node.conf 7005:C 15 Aug 2020 08:53:32.500 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 7005:C 15 Aug 2020 08:53:32.500 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=7005, just started 7005:C 15 Aug 2020 08:53:32.500 # Configuration loaded [root@swarm-node2 bin]# [root@swarm-node2 bin]# ps -ef |grep redis root 6996 1 0 08:53 ? 00:00:00 ./redis-server 192.168.199.51:7004 [cluster] root 7001 1 0 08:53 ? 00:00:00 ./redis-server 192.168.199.51:7005 [cluster] root 7006 1 0 08:53 ? 00:00:00 ./redis-server 192.168.199.51:7006 [cluster] root 7011 6902 0 08:53 pts/0 00:00:00 grep --color=auto redis [root@swarm-node2 bin]#
5、配置集群
Redis Cluster 在5.0之后取消了ruby脚本 redis-trib.rb的支持,所以创建集群要用到 redis-cli
在主节点上面执行:
等待执行完出现ok表示执行完成
[root@swarm-node1 bin]# ./redis-cli --cluster create 192.168.199.50:7001 192.168.199.50:7002 192.168.199.50:7003 192.168.199.51:7004 192.168.199.51:7005 192.168.199.51:7006 --cluster-replicas 1 -a password
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.199.51:7006 to 192.168.199.50:7001
Adding replica 192.168.199.50:7003 to 192.168.199.51:7004
Adding replica 192.168.199.51:7005 to 192.168.199.50:7002
M: 2ba1ef9b5053c3793fb0ba58baa57e1cbc12c618 192.168.199.50:7001
slots:[0-5460] (5461 slots) master
M: 674b3703f19c01082f41f91cdeaa2c1c8f9339f7 192.168.199.50:7002
slots:[10923-16383] (5461 slots) master
S: 93a65c394f1e188fc041b238b5a718460c7213a1 192.168.199.50:7003
replicates 52a1a3739af715826aa597421824de6187086808
M: 52a1a3739af715826aa597421824de6187086808 192.168.199.51:7004
slots:[5461-10922] (5462 slots) master
S: a33c1a5b606afb178b9c8f607d6831bc5fee505e 192.168.199.51:7005
replicates 674b3703f19c01082f41f91cdeaa2c1c8f9339f7
S: f39d1bc607d5ba8f69bf8d7caf7fc0cd306f7825 192.168.199.51:7006
replicates 2ba1ef9b5053c3793fb0ba58baa57e1cbc12c618
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
....
>>> Performing Cluster Check (using node 192.168.199.50:7001)
M: 2ba1ef9b5053c3793fb0ba58baa57e1cbc12c618 192.168.199.50:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 674b3703f19c01082f41f91cdeaa2c1c8f9339f7 192.168.199.50:7002
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 93a65c394f1e188fc041b238b5a718460c7213a1 192.168.199.50:7003
slots: (0 slots) slave
replicates 52a1a3739af715826aa597421824de6187086808
M: 52a1a3739af715826aa597421824de6187086808 192.168.199.51:7004
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: a33c1a5b606afb178b9c8f607d6831bc5fee505e 192.168.199.51:7005
slots: (0 slots) slave
replicates 674b3703f19c01082f41f91cdeaa2c1c8f9339f7
S: f39d1bc607d5ba8f69bf8d7caf7fc0cd306f7825 192.168.199.51:7006
slots: (0 slots) slave
replicates 2ba1ef9b5053c3793fb0ba58baa57e1cbc12c618
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.