1. 程式人生 > 實用技巧 >Redis 5.0.9配置cluster叢集模式

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.

 檢視叢集狀態

  redis叢集安裝完成