Redis redis-trib集群配置


 

redis文檔:http://doc.redisfans.com/

參考:https://www.cnblogs.com/wuxl360/p/5920330.html

          http://www.cnblogs.com/carryping/p/7447823.html

         https://www.jianshu.com/p/2639549bedc8

 

1. 下載並解壓

cd /root/software
wget http: //download .redis.io /releases/redis-3 .2.3. tar .gz
tar -zxvf redis-3.2.4.tar.gz 

2. 編譯安裝

cd redis-3.2.3 make PREFIX=/usr/local/redis-3.2.3 install
ln -sv /usr/local/redis-3.2.3 /usr/local/redis

3. 將 redis-trib.rb 復制到 /usr/local/bin 目錄下

cd src
cp redis-trib.rb /usr/local/bin/  

4. 創建 Redis 節點

首先在機器上/usr/local/redis/ 目錄下創建 cluster-test 目錄;

mkdir redis_cluster  

在cluster-test 目錄下,創建名為7001、7002、7003,7004、7005、7006的目錄,並將 redis.conf 拷貝到這劉個目錄中

mkdir 7001 7002 7003 7004 7005 7006
cp redis.conf cluster-test/7001 cp redis.conf cluster-test/7002 cp redis.conf cluster-test/7003
cp redis.conf cluster-test/7004 cp redis.conf cluster-test/7005 cp redis.conf cluster-test/7006    

分別修改這6個配置文件,修改如下內容

bind 0.0.0.0
port 7001
dir /usr/local/redis/cluster-test/7001
pidfile /var/run/redis_7001.pid
cluster-enabled  yes                           
cluster-config-file  nodes_7001.conf
cluster-node-timeout  15000 
appendonly yes
appendfilename "appendonly.aof"
daemonize yes

protected-mode yes
tcp-backlog 511
timeout 0
tcp-keepalive 300
supervised no
loglevel verbose
logfile ""
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated 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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
7001 redis.conf
bind 0.0.0.0
port 7002
dir /usr/local/redis/cluster-test/7002
pidfile /var/run/redis_7002.pid
cluster-enabled  yes                           
cluster-config-file  nodes_7002.conf
cluster-node-timeout  15000 
appendonly yes
appendfilename "appendonly.aof"
daemonize yes

protected-mode yes
tcp-backlog 511
timeout 0
tcp-keepalive 300
supervised no
loglevel verbose
logfile ""
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated 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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
7002 redis.conf
bind 0.0.0.0
port 7003
dir /usr/local/redis/cluster-test/7003
pidfile /var/run/redis_7003.pid
cluster-enabled  yes                           
cluster-config-file  nodes_7003.conf
cluster-node-timeout  15000 
appendonly yes
appendfilename "appendonly.aof"
daemonize yes

protected-mode yes
tcp-backlog 511
timeout 0
tcp-keepalive 300
supervised no
loglevel verbose
logfile ""
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated 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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
7003 redis.conf
bind 0.0.0.0
port 7004
dir /usr/local/redis/cluster-test/7004
pidfile /var/run/redis_7004.pid
cluster-enabled  yes                           
cluster-config-file  nodes_7004.conf
cluster-node-timeout  15000 
appendonly yes
appendfilename "appendonly.aof"
daemonize yes

protected-mode yes
tcp-backlog 511
timeout 0
tcp-keepalive 300
supervised no
loglevel verbose
logfile ""
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated 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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
7004 redis.conf
bind 0.0.0.0
port 7005
dir /usr/local/redis/cluster-test/7005
pidfile /var/run/redis_7005.pid
cluster-enabled  yes                           
cluster-config-file  nodes_7005.conf
cluster-node-timeout  15000 
appendonly yes
appendfilename "appendonly.aof"
daemonize yes

protected-mode yes
tcp-backlog 511
timeout 0
tcp-keepalive 300
supervised no
loglevel verbose
logfile ""
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated 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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
7005 redis.conf
pidfile /var/run/redis_7006.pid
cluster-enabled  yes                           
cluster-config-file  nodes_7006.conf
cluster-node-timeout  15000 
appendonly yes
appendfilename "appendonly.aof"
daemonize yes

protected-mode yes
tcp-backlog 511
timeout 0
tcp-keepalive 300
supervised no
loglevel verbose
logfile ""
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated 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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
7006 redis.conf
復制代碼
port  7001                                        //端口7001,7002,7001 bind 本機ip //默認ip為127.0.0.1 需要改為其他節點機器可訪問的ip 否則創建集群時無法訪問對應的端口,無法創建集群 daemonize yes //redis后台運行 pidfile /var/run/redis_7001.pid //pidfile文件對應7001,7002,7003 cluster-enabled yes //開啟集群 把注釋#去掉 cluster-config-file nodes_7001.conf //集群的配置 配置文件首次啟動自動生成 7001,7002,7003 cluster-node-timeout 15000 //請求超時 默認15秒,可自行設置 appendonly yes //aof日志開啟 有需要就開啟,它會每次寫操作都記錄一條日志 
復制代碼

5.啟動並查看

/usr/local/redis/bin/redis-server /usr/local/redis/cluster-test/7001/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/cluster-test/7002/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/cluster-test/7003/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/cluster-test/7004/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/cluster-test/7005/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/cluster-test/7006/redis.conf

查看顯示下面信息
[root@localhost 7001]# netstat -ntlp |grep redis
tcp        0      0 0.0.0.0:7005                0.0.0.0:*                   LISTEN      19225/redis-server  
tcp        0      0 0.0.0.0:7006                0.0.0.0:*                   LISTEN      19229/redis-server  
tcp        0      0 0.0.0.0:17001               0.0.0.0:*                   LISTEN      19209/redis-server  
tcp        0      0 0.0.0.0:17002               0.0.0.0:*                   LISTEN      19213/redis-server  
tcp        0      0 0.0.0.0:17003               0.0.0.0:*                   LISTEN      19215/redis-server  
tcp        0      0 0.0.0.0:17004               0.0.0.0:*                   LISTEN      19221/redis-server  
tcp        0      0 0.0.0.0:17005               0.0.0.0:*                   LISTEN      19225/redis-server  
tcp        0      0 0.0.0.0:17006               0.0.0.0:*                   LISTEN      19229/redis-server  
tcp        0      0 0.0.0.0:7001                0.0.0.0:*                   LISTEN      19209/redis-server  
tcp        0      0 0.0.0.0:7002                0.0.0.0:*                   LISTEN      19213/redis-server  
tcp        0      0 0.0.0.0:7003                0.0.0.0:*                   LISTEN      19215/redis-server  
tcp        0      0 0.0.0.0:7004                0.0.0.0:*                   LISTEN      19221/redis-server  


6.創建集群

redis-trib.rb  create  --replicas  1 192.168.8.102:7001  192.168.8.102:7002 192.168.8.102:7003  192.168.8.102:7004  192.168.8.102:7005 192.168.8.102:7006
gem install redis 報錯
    ERROR:  Error installing redis:
     redis requires Ruby version >= 2.2.2.

解決辦法是 先安裝rvm,再把ruby版本提升至2.4.5

1.安裝curl
sudo yum install curl

2. 安裝RVM
curl -L get.rvm.io | bash -s stable

再次報錯:
[root@localhost yum.repos.d]# curl -L get.rvm.io | bash -s stable
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 24173  100 24173    0     0  21587      0  0:00:01  0:00:01 --:--:--  128k
Downloading https://github.com/rvm/rvm/archive/1.29.7.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.7/1.29.7.tar.gz.asc
gpg: Signature made Fri 04 Jan 2019 06:01:48 AM CST using RSA key ID 39499BDB
gpg: Can't check signature: No public key
GPG signature verification failed for '/usr/local/rvm/archives/rvm-1.29.7.tgz' - 'https://github.com/rvm/rvm/releases/download/1.29.7/1.29.7.tar.gz.asc'! Try to install GPG v2 and then fetch the public key:

    gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

or if it fails:

    command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
    command curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -

In case of further problems with validation please refer to https://rvm.io/rvm/security

那就按照上面的執行上面語句就OK啦


3.
source /usr/local/rvm/scripts/rvm

4. 查看rvm庫中已知的ruby版本

rvm list known

5. 安裝一個ruby版本

rvm install 2.4.5

6. 使用一個ruby版本

rvm use 2.4.5

7. 卸載一個已知版本

rvm remove 2.0.0

8. 查看版本

ruby --version

9. 再安裝redis就可以了
gem install redis

再執行 輸入 yes 即可,然后出現如下內容,說明安裝成功:
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.8.102:7001
192.168.8.102:7002
192.168.8.102:7003
Adding replica 192.168.8.102:7004 to 192.168.8.102:7001
Adding replica 192.168.8.102:7005 to 192.168.8.102:7002
Adding replica 192.168.8.102:7006 to 192.168.8.102:7003
M: 229393055278b1cded847e554739255905b33fb3 192.168.8.102:7001
   slots:0-5460 (5461 slots) master
M: ef175d84db52e084b5d74cf9f1c414011bf6cce9 192.168.8.102:7002
   slots:5461-10922 (5462 slots) master
M: 1bca1b7b96f3fe936ad44f254d17da26da9fd186 192.168.8.102:7003
   slots:10923-16383 (5461 slots) master
S: 837eea90f07c9cdebfa7e1924d2e2788cf5573eb 192.168.8.102:7004
   replicates 229393055278b1cded847e554739255905b33fb3
S: 0f3d5f7e78dc857efc5b58ab674faee9fba876af 192.168.8.102:7005
   replicates ef175d84db52e084b5d74cf9f1c414011bf6cce9
S: b41ae432b4bd8d7ca44bf318c7b9382f8dbd7a79 192.168.8.102:7006
   replicates 1bca1b7b96f3fe936ad44f254d17da26da9fd186
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.8.102:7001)
M: 229393055278b1cded847e554739255905b33fb3 192.168.8.102:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 1bca1b7b96f3fe936ad44f254d17da26da9fd186 192.168.8.102:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: ef175d84db52e084b5d74cf9f1c414011bf6cce9 192.168.8.102:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 0f3d5f7e78dc857efc5b58ab674faee9fba876af 192.168.8.102:7005
   slots: (0 slots) slave
   replicates ef175d84db52e084b5d74cf9f1c414011bf6cce9
S: 837eea90f07c9cdebfa7e1924d2e2788cf5573eb 192.168.8.102:7004
   slots: (0 slots) slave
   replicates 229393055278b1cded847e554739255905b33fb3
S: b41ae432b4bd8d7ca44bf318c7b9382f8dbd7a79 192.168.8.102:7006
   slots: (0 slots) slave
   replicates 1bca1b7b96f3fe936ad44f254d17da26da9fd186
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.


 

7.測試

[root@localhost 7001]# ../../bin/redis-cli -c -h 192.168.8.102 -p 7001
192.168.8.102:7001>
192.168.8.102:7001>
192.168.8.102:7001> set name zhangsan
-> Redirected to slot [5798] located at 192.168.8.102:7002
OK

[root@localhost 7001]# ../../../redis/bin/redis-cli -c -p 7006
127.0.0.1:7006>
127.0.0.1:7006>
127.0.0.1:7006>
127.0.0.1:7006> get name
-> Redirected to slot [5798] located at 192.168.8.102:7002
"zhangsan"

驗證數據一致性
[root@localhost 7003]# md5sum dump.rdb
2604704e38811948117ddc473d62dc55  dump.rdb

[root@localhost 7001]# md5sum dump.rdb
2604704e38811948117ddc473d62dc55  dump.rdb

說明集群運作正常。

 

8.測試故障

讓192.168.8.102:7003當掉

[root@localhost 7001]# cat nodes_7001.conf 1bca1b7b96f3fe936ad44f254d17da26da9fd186 192.168.8.102:7003 master - 0 1551780190387 3 connected 10923-16383 ef175d84db52e084b5d74cf9f1c414011bf6cce9 192.168.8.102:7002 master - 0 1551780191393 2 connected 5461-10922 0f3d5f7e78dc857efc5b58ab674faee9fba876af 192.168.8.102:7005 slave ef175d84db52e084b5d74cf9f1c414011bf6cce9 0 1551780188372 5 connected 229393055278b1cded847e554739255905b33fb3 192.168.8.102:7001 myself,master - 0 0 1 connected 0-5460 837eea90f07c9cdebfa7e1924d2e2788cf5573eb 192.168.8.102:7004 slave 229393055278b1cded847e554739255905b33fb3 0 1551780187366 4 connected b41ae432b4bd8d7ca44bf318c7b9382f8dbd7a79 192.168.8.102:7006 slave 1bca1b7b96f3fe936ad44f254d17da26da9fd186 0 1551780189379 6 connected

[root@localhost 7001]# netstat -ntlp |grep 7003
tcp        0      0 0.0.0.0:17003               0.0.0.0:*                   LISTEN      19215/redis-server  
tcp        0      0 0.0.0.0:7003                0.0.0.0:*                   LISTEN      19215/redis-server  
[root@localhost 7001]#
[root@localhost 7001]#
[root@localhost 7001]#
[root@localhost 7001]#
[root@localhost 7001]# kill -9 19215
[root@localhost 7001]#
[root@localhost 7001]# cat nodes_7001.conf
1bca1b7b96f3fe936ad44f254d17da26da9fd186 192.168.8.102:7003 master,fail - 1551784667064 1551784663541 3 disconnected
ef175d84db52e084b5d74cf9f1c414011bf6cce9 192.168.8.102:7002 master - 0 1551784682702 2 connected 5461-10922
0f3d5f7e78dc857efc5b58ab674faee9fba876af 192.168.8.102:7005 slave ef175d84db52e084b5d74cf9f1c414011bf6cce9 0 1551784681691 5 connected
229393055278b1cded847e554739255905b33fb3 192.168.8.102:7001 myself,master - 0 0 1 connected 0-5460
837eea90f07c9cdebfa7e1924d2e2788cf5573eb 192.168.8.102:7004 slave 229393055278b1cded847e554739255905b33fb3 0 1551784679675 4 connected
b41ae432b4bd8d7ca44bf318c7b9382f8dbd7a79 192.168.8.102:7006 master - 0 1551784680684 7 connected 10923-16383
vars currentEpoch 7 lastVoteEpoch 7
[root@localhost 7001]# redis-cli -c -p 7001
127.0.0.1:7001>
127.0.0.1:7001>
127.0.0.1:7001>
127.0.0.1:7001> get name
-> Redirected to slot [5798] located at 192.168.8.102:7002
"zhangsan"
可以訪問


再當掉192.168.8.102:7006

[root@localhost 7001]# redis-cli -c -p 7001
127.0.0.1:7001>
127.0.0.1:7001>
127.0.0.1:7001>
127.0.0.1:7001> get name
(error) CLUSTERDOWN The cluster is down
127.0.0.1:7001>
[root@localhost 7001]# !cat
cat nodes_7001.conf
1bca1b7b96f3fe936ad44f254d17da26da9fd186 192.168.8.102:7003 slave,fail b41ae432b4bd8d7ca44bf318c7b9382f8dbd7a79 1551785439155 1551785434725 7 disconnected
ef175d84db52e084b5d74cf9f1c414011bf6cce9 192.168.8.102:7002 master - 0 1551785536517 2 connected 5461-10922
0f3d5f7e78dc857efc5b58ab674faee9fba876af 192.168.8.102:7005 slave ef175d84db52e084b5d74cf9f1c414011bf6cce9 0 1551785537528 5 connected
229393055278b1cded847e554739255905b33fb3 192.168.8.102:7001 myself,master - 0 0 1 connected 0-5460
837eea90f07c9cdebfa7e1924d2e2788cf5573eb 192.168.8.102:7004 slave 229393055278b1cded847e554739255905b33fb3 0 1551785539547 4 connected
b41ae432b4bd8d7ca44bf318c7b9382f8dbd7a79 :0 master,fail,noaddr - 1551785518472 1551785516358 7 disconnected 10923-16383
vars currentEpoch 7 lastVoteEpoch 7




開啟主節點:7006端口
[root@localhost 7001]# cat ../start_cluster.sh
/usr/local/redis/bin/redis-server /usr/local/redis/cluster-test/7001/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/cluster-test/7002/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/cluster-test/7003/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/cluster-test/7004/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/cluster-test/7005/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/cluster-test/7006/redis.conf
[root@localhost 7001]#
[root@localhost 7001]#
[root@localhost 7001]# /usr/local/redis/bin/redis-server /usr/local/redis/cluster-test/7006/redis.conf
[root@localhost 7001]#
[root@localhost 7001]# cat nodes_7001.conf
1bca1b7b96f3fe936ad44f254d17da26da9fd186 192.168.8.102:7003 slave,fail b41ae432b4bd8d7ca44bf318c7b9382f8dbd7a79 0 1551785119857 7 connected
ef175d84db52e084b5d74cf9f1c414011bf6cce9 192.168.8.102:7002 master - 0 1551785118344 2 connected 5461-10922
0f3d5f7e78dc857efc5b58ab674faee9fba876af 192.168.8.102:7005 slave ef175d84db52e084b5d74cf9f1c414011bf6cce9 0 1551785119352 5 connected
229393055278b1cded847e554739255905b33fb3 192.168.8.102:7001 myself,master - 0 0 1 connected 0-5460
837eea90f07c9cdebfa7e1924d2e2788cf5573eb 192.168.8.102:7004 slave 229393055278b1cded847e554739255905b33fb3 0 1551785117336 4 connected
b41ae432b4bd8d7ca44bf318c7b9382f8dbd7a79 192.168.8.102:7006 master - 0 1551785786843 7 connected 10923-16383

再檢測又可以訪問了
[root@localhost 7001]# redis-cli -c -p 7001
127.0.0.1:7001>
127.0.0.1:7001>
127.0.0.1:7001> get name
-> Redirected to slot [5798] located at 192.168.8.102:7002
"zhangsan"

注意:恢復時,先恢復主節點 再恢復從節點

 

簡單說一下原理

redis cluster在設計的時候,就考慮到了去中心化,去中間件,也就是說,集群中的每個節點都是平等的關系,都是對等的,每個節點都保存各自的數據和整個集群的狀態。每個節點都和其他所有節點連接,而且這些連接保持活躍,這樣就保證了我們只需要連接集群中的任意一個節點,就可以獲取到其他節點的數據。

Redis 集群沒有並使用傳統的一致性哈希來分配數據,而是采用另外一種叫做哈希槽 (hash slot)的方式來分配的。redis cluster 默認分配了 16384 個slot,當我們set一個key 時,會用CRC16算法來取模得到所屬的slot,然后將這個key 分到哈希槽區間的節點上,具體算法就是:CRC16(key) % 16384。所以我們在測試的時候看到set 和 get 的時候,直接跳轉到了7000端口的節點。

Redis 集群會把數據存在一個 master 節點,然后在這個 master 和其對應的salve 之間進行數據同步。當讀取數據時,也根據一致性哈希算法到對應的 master 節點獲取數據。只有當一個master 掛掉之后,才會啟動一個對應的 salve 節點,充當 master 。

需要注意的是:必須要3個或以上的主節點,否則在創建集群時會失敗,並且當存活的主節點數小於總節點數的一半時,整個集群就無法提供服務了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM