1
2
3
|
10.10.2.70:6300 10.10.2.70:6301 主從 10.10.2.71:6300 10.10.2.71:6301 主從 10.10.2.85:6300 10.10.2.85:6301 主從 |
1
|
Redis version 3.0.5 |
1
2
3
4
5
6
7
8
|
wget http: //download .redis.io /releases/redis-3 .0.5. tar .gz tar -zxvf redis-3.0.5. tar .gz cd redis-3.0.5 make cp redis-3.0.5 /src/redis-trib .rb /bin/ cp redis-3.0.5 /src/redis-server /bin/ cp redis-3.0.5 /src/redis-cli /bin/ |
1
2
|
yum -y install ruby rubygems gem install redis --version 3.0.5 |
1
2
3
|
echo never > /sys/kernel/mm/transparent_hugepage/enabled echo "vm.overcommit_memory = 1" >> /etc/sysctl .conf sysctl -p |
1
2
|
mkdir /data/redis/6300 -p mkdir /data/redis/6301 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
vim /etc/redis_6300 .conf daemonize yes port 6300 tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice maxmemory 10gb databases 16 dir /data/redis/6300 slave-serve-stale-data yes loglevel notice logfile "/data/redis/6300/redis_6300.log" #slave只讀 slave- read -only yes #not use default repl-disable-tcp-nodelay yes slave-priority 100 #打開aof持久化 appendonly yes #每秒一次aof寫 appendfsync everysec #關閉在aof rewrite的時候對新的寫操作進行fsync no-appendfsync-on-rewrite yes auto-aof-rewrite-min-size 64mb lua- time -limit 5000 #打開redis集羣 cluster-enabled yes cluster-config- file /data/redis/6300/nodes-6300 .conf #節點互連超時的閥值(單位毫秒) cluster-node-timeout 15000 #一個主節點在擁有多少個好的從節點的時候就要割讓一個從節點出來給其他沒有從節點或者從節點掛掉的主節點 cluster-migration-barrier 1 #如果某一些key space沒有被集羣中任何節點覆蓋,最常見的就是一個node掛掉,集羣將停止接受寫入 cluster-require-full-coverage no #部署在同一機器的redis實例,把auto-aof-rewrite搓開,防止瞬間fork所有redis進程做rewrite,佔用大量內存 auto-aof-rewrite-percentage 80-100 slowlog-log-slower-than 10000 slowlog-max-len 128 notify-keyspace-events "" hash -max-ziplist-entries 512 hash -max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set -max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 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 |
1
2
3
4
|
redis-server /etc/redis_6300 .conf redis-server /etc/redis_6301 .conf echo "redis-server /etc/redis_6300.conf" >> /etc/rc . local echo "redis-server /etc/redis_6301.conf" >> /etc/rc . local |
1
2
|
#節點角色由順序決定,先master之後是slave,本文中6300是master,6301是slave redis-trib.rb create --replicas 1 10.10.2.70:6300 10.10.2.71:6300 10.10.2.85:6300 10.10.2.70:6301 10.10.2.71:6301 10.10.2.85:6301 |
1
|
redis-trib.rb check 10.10.2.70:6300 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
[root@yw_0_0 ~] # redis-trib.rb add-node 10.10.2.85:6302 10.10.2.70:6300 >>> Adding node 10.10.2.85:6302 to cluster 10.10.2.70:6300 Connecting to node 10.10.2.70:6300: OK Connecting to node 10.10.2.85:6300: OK Connecting to node 10.10.2.85:6301: OK Connecting to node 10.10.2.71:6300: OK Connecting to node 10.10.2.70:6301: OK Connecting to node 10.10.2.71:6301: OK >>> Performing Cluster Check (using node 10.10.2.70:6300) S: cd1f2c1f348bb4359337e7462c1e21dc82f1551b 10.10.2.70:6300 slots: (0 slots) slave replicates 85412cf3d8e69354115fc0991f470b32b9213cd7 M: 6bea6afa2ee8dfb0cc3c96f804eb3fa77ce98013 10.10.2.85:6300 slots:0-5460 (5461 slots) master 1 additional replica(s) S: a74642c0fbc98f921be477eabcdd22eccd89891f 10.10.2.85:6301 slots: (0 slots) slave replicates 2568dbd91fffa16ff93ea8db19275fd7ec8af41a M: 2568dbd91fffa16ff93ea8db19275fd7ec8af41a 10.10.2.71:6300 slots:5461-10922 (5462 slots) master 1 additional replica(s) M: 85412cf3d8e69354115fc0991f470b32b9213cd7 10.10.2.70:6301 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: 22d2dec483824b84571a60e8c037fff957615552 10.10.2.71:6301 slots: (0 slots) slave replicates 6bea6afa2ee8dfb0cc3c96f804eb3fa77ce98013 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. Connecting to node 10.10.2.85:6302: OK >>> Send CLUSTER MEET to node 10.10.2.85:6302 to make it join the cluster. [OK] New node added correctly. |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
[root@yw_0_0 ~] # redis-trib.rb add-node --slave --master-id 5ef18f95f75756891aa948ea1f200044f1d3947c 10.10.2.85:6303 10.10.2.70:6300 >>> Adding node 10.10.2.85:6303 to cluster 10.10.2.70:6300 Connecting to node 10.10.2.70:6300: OK Connecting to node 10.10.2.85:6300: OK Connecting to node 10.10.2.85:6302: OK Connecting to node 10.10.2.85:6301: OK Connecting to node 10.10.2.71:6300: OK Connecting to node 10.10.2.70:6301: OK Connecting to node 10.10.2.71:6301: OK >>> Performing Cluster Check (using node 10.10.2.70:6300) S: cd1f2c1f348bb4359337e7462c1e21dc82f1551b 10.10.2.70:6300 slots: (0 slots) slave replicates 85412cf3d8e69354115fc0991f470b32b9213cd7 M: 6bea6afa2ee8dfb0cc3c96f804eb3fa77ce98013 10.10.2.85:6300 slots:0-5460 (5461 slots) master 1 additional replica(s) M: 5ef18f95f75756891aa948ea1f200044f1d3947c 10.10.2.85:6302 slots: (0 slots) master 0 additional replica(s) S: a74642c0fbc98f921be477eabcdd22eccd89891f 10.10.2.85:6301 slots: (0 slots) slave replicates 2568dbd91fffa16ff93ea8db19275fd7ec8af41a M: 2568dbd91fffa16ff93ea8db19275fd7ec8af41a 10.10.2.71:6300 slots:5461-10922 (5462 slots) master 1 additional replica(s) M: 85412cf3d8e69354115fc0991f470b32b9213cd7 10.10.2.70:6301 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: 22d2dec483824b84571a60e8c037fff957615552 10.10.2.71:6301 slots: (0 slots) slave replicates 6bea6afa2ee8dfb0cc3c96f804eb3fa77ce98013 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. Connecting to node 10.10.2.85:6303: OK >>> Send CLUSTER MEET to node 10.10.2.85:6303 to make it join the cluster. Waiting for the cluster to join . >>> Configure node as replica of 10.10.2.85:6302. [OK] New node added correctly. |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
[root@yw_0_0 ~] # redis-trib.rb reshard 10.10.2.70:6300 Connecting to node 10.10.2.70:6300: OK Connecting to node 10.10.2.85:6300: OK Connecting to node 10.10.2.85:6303: OK Connecting to node 10.10.2.85:6302: OK Connecting to node 10.10.2.85:6301: OK Connecting to node 10.10.2.71:6300: OK Connecting to node 10.10.2.70:6301: OK Connecting to node 10.10.2.71:6301: OK >>> Performing Cluster Check (using node 10.10.2.70:6300) S: cd1f2c1f348bb4359337e7462c1e21dc82f1551b 10.10.2.70:6300 slots: (0 slots) slave replicates 85412cf3d8e69354115fc0991f470b32b9213cd7 M: 6bea6afa2ee8dfb0cc3c96f804eb3fa77ce98013 10.10.2.85:6300 slots:0-5460 (5461 slots) master 1 additional replica(s) S: fc90d090fae909fd4f962752941c039d081d3854 10.10.2.85:6303 slots: (0 slots) slave replicates 5ef18f95f75756891aa948ea1f200044f1d3947c M: 5ef18f95f75756891aa948ea1f200044f1d3947c 10.10.2.85:6302 slots: (0 slots) master 1 additional replica(s) S: a74642c0fbc98f921be477eabcdd22eccd89891f 10.10.2.85:6301 slots: (0 slots) slave replicates 2568dbd91fffa16ff93ea8db19275fd7ec8af41a M: 2568dbd91fffa16ff93ea8db19275fd7ec8af41a 10.10.2.71:6300 slots:5461-10922 (5462 slots) master 1 additional replica(s) M: 85412cf3d8e69354115fc0991f470b32b9213cd7 10.10.2.70:6301 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: 22d2dec483824b84571a60e8c037fff957615552 10.10.2.71:6301 slots: (0 slots) slave replicates 6bea6afa2ee8dfb0cc3c96f804eb3fa77ce98013 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. How many slots do you want to move (from 1 to 16384)? 3000 #設置需要把3000個slot做移動 What is the receiving node ID? 5ef18f95f75756891aa948ea1f200044f1d3947c #設置接收這3000個slot的節點ID,也就是剛纔新加的10.10.2.85:6302的ID Please enter all the source node IDs. Type 'all' to use all the nodes as source nodes for the hash slots. Type 'done' once you entered all the source nodes IDs. Source node #1:85412cf3d8e69354115fc0991f470b32b9213cd7 #設置這3000slot的來源ID,這裏我從集羣之前的3個節點分別去取一部分slot Source node #2:6bea6afa2ee8dfb0cc3c96f804eb3fa77ce98013 #設置這3000slot的來源ID,這裏我從集羣之前的3個節點分別去取一部分slot Source node #3:2568dbd91fffa16ff93ea8db19275fd7ec8af41a #設置這3000slot的來源ID,這裏我從集羣之前的3個節點分別去取一部分slot Source node #4:done #輸入done開始做一些初始化操作 此處省略 Do you want to proceed with the proposed reshard plan ( yes /no )? yes 輸入 yes 確認開始遷移slot |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
[root@yw_0_0 ~] # redis-trib.rb reshard 10.10.2.70:6300 Connecting to node 10.10.2.70:6300: OK Connecting to node 10.10.2.85:6300: OK Connecting to node 10.10.2.85:6303: OK Connecting to node 10.10.2.85:6302: OK Connecting to node 10.10.2.85:6301: OK Connecting to node 10.10.2.71:6300: OK Connecting to node 10.10.2.70:6301: OK Connecting to node 10.10.2.71:6301: OK >>> Performing Cluster Check (using node 10.10.2.70:6300) S: cd1f2c1f348bb4359337e7462c1e21dc82f1551b 10.10.2.70:6300 slots: (0 slots) slave replicates 85412cf3d8e69354115fc0991f470b32b9213cd7 M: 6bea6afa2ee8dfb0cc3c96f804eb3fa77ce98013 10.10.2.85:6300 slots:999-5460 (4462 slots) master 1 additional replica(s) S: fc90d090fae909fd4f962752941c039d081d3854 10.10.2.85:6303 slots: (0 slots) slave replicates 5ef18f95f75756891aa948ea1f200044f1d3947c M: 5ef18f95f75756891aa948ea1f200044f1d3947c 10.10.2.85:6302 slots:0-998,5461-6461,10923-11921 (2999 slots) master 1 additional replica(s) S: a74642c0fbc98f921be477eabcdd22eccd89891f 10.10.2.85:6301 slots: (0 slots) slave replicates 2568dbd91fffa16ff93ea8db19275fd7ec8af41a M: 2568dbd91fffa16ff93ea8db19275fd7ec8af41a 10.10.2.71:6300 slots:6462-10922 (4461 slots) master 1 additional replica(s) M: 85412cf3d8e69354115fc0991f470b32b9213cd7 10.10.2.70:6301 slots:11922-16383 (4462 slots) master 1 additional replica(s) S: 22d2dec483824b84571a60e8c037fff957615552 10.10.2.71:6301 slots: (0 slots) slave replicates 6bea6afa2ee8dfb0cc3c96f804eb3fa77ce98013 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. How many slots do you want to move (from 1 to 16384)? 3000 #上文給這個節點遷入了3000個slot,所以這裏還選擇遷出3000個slot What is the receiving node ID? 85412cf3d8e69354115fc0991f470b32b9213cd7 #接收這3000slot節點的主ID Please enter all the source node IDs. Type 'all' to use all the nodes as source nodes for the hash slots. Type 'done' once you entered all the source nodes IDs. Source node #1:5ef18f95f75756891aa948ea1f200044f1d3947c #要下線節點的主ID Source node #4:done 此處省略 Do you want to proceed with the proposed reshard plan ( yes /no )? yes |
1
2
3
4
5
6
7
8
9
|
10.10.2.71:6300> cluster nodes 85412cf3d8e69354115fc0991f470b32b9213cd7 10.10.2.70:6301 master - 0 1445853133399 12 connected 0-999 6462-7460 10923-16383 22d2dec483824b84571a60e8c037fff957615552 10.10.2.71:6301 slave 6bea6afa2ee8dfb0cc3c96f804eb3fa77ce98013 0 1445853132898 10 connected 6bea6afa2ee8dfb0cc3c96f804eb3fa77ce98013 10.10.2.85:6300 master - 0 1445853134400 10 connected 1000-5461 2568dbd91fffa16ff93ea8db19275fd7ec8af41a 10.10.2.71:6300 myself,master - 0 0 11 connected 5462-6461 7461-10922 cd1f2c1f348bb4359337e7462c1e21dc82f1551b 10.10.2.70:6300 slave 85412cf3d8e69354115fc0991f470b32b9213cd7 0 1445853131395 12 connected fc90d090fae909fd4f962752941c039d081d3854 10.10.2.85:6303 slave 5ef18f95f75756891aa948ea1f200044f1d3947c 0 1445853133899 8 connected a74642c0fbc98f921be477eabcdd22eccd89891f 10.10.2.85:6301 slave 2568dbd91fffa16ff93ea8db19275fd7ec8af41a 0 1445853129394 11 connected 5ef18f95f75756891aa948ea1f200044f1d3947c 10.10.2.85:6302 master - 0 1445853132397 8 connected |
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@yw_0_0 ~] # redis-trib.rb del-node 10.10.2.85:6303 fc90d090fae909fd4f962752941c039d081d3854 >>> Removing node fc90d090fae909fd4f962752941c039d081d3854 from cluster 10.10.2.85:6303 Connecting to node 10.10.2.85:6303: OK Connecting to node 10.10.2.85:6301: OK Connecting to node 10.10.2.85:6302: OK Connecting to node 10.10.2.85:6300: OK Connecting to node 10.10.2.70:6300: OK Connecting to node 10.10.2.71:6301: OK Connecting to node 10.10.2.70:6301: OK Connecting to node 10.10.2.71:6300: OK >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node. |
1
2
3
4
5
6
7
8
9
10
11
|
redis-trib.rb del-node 10.10.2.70:6301 5ef18f95f75756891aa948ea1f200044f1d3947c >>> Removing node 5ef18f95f75756891aa948ea1f200044f1d3947c from cluster 10.10.2.70:6301 Connecting to node 10.10.2.70:6301: OK Connecting to node 10.10.2.71:6300: OK Connecting to node 10.10.2.85:6301: OK Connecting to node 10.10.2.71:6301: OK Connecting to node 10.10.2.85:6302: OK Connecting to node 10.10.2.70:6300: OK Connecting to node 10.10.2.85:6300: OK >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node. |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
[root@yw_0_0 ~] # redis-trib.rb add-node --slave --master-id cd1f2c1f348bb4359337e7462c1e21dc82f1551b 10.10.2.85:6302 10.10.2.70:6300 >>> Adding node 10.10.2.85:6302 to cluster 10.10.2.70:6300 Connecting to node 10.10.2.70:6300: OK Connecting to node 10.10.2.85:6300: OK Connecting to node 10.10.2.71:6300: OK Connecting to node 10.10.2.70:6301: OK Connecting to node 10.10.2.85:6301: OK Connecting to node 10.10.2.71:6301: OK >>> Performing Cluster Check (using node 10.10.2.70:6300) M: cd1f2c1f348bb4359337e7462c1e21dc82f1551b 10.10.2.70:6300 slots:3000-5461,6462-7460,10923-16383 (8922 slots) master 1 additional replica(s) M: e36cdef7a26ed59e8d9db2cf1dbc1997bfc9dfde 10.10.2.85:6300 slots:0-2999 (3000 slots) master 1 additional replica(s) M: 2568dbd91fffa16ff93ea8db19275fd7ec8af41a 10.10.2.71:6300 slots:5462-6461,7461-10922 (4462 slots) master 1 additional replica(s) S: 85412cf3d8e69354115fc0991f470b32b9213cd7 10.10.2.70:6301 slots: (0 slots) slave replicates cd1f2c1f348bb4359337e7462c1e21dc82f1551b S: 89fcc4994a99ed2fe9bbb908c58dfda2cf31e7d2 10.10.2.85:6301 slots: (0 slots) slave replicates e36cdef7a26ed59e8d9db2cf1dbc1997bfc9dfde S: 1f3ea36eacbe005a4b9ac52aeef6d83337dac051 10.10.2.71:6301 slots: (0 slots) slave replicates 2568dbd91fffa16ff93ea8db19275fd7ec8af41a [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. Connecting to node 10.10.2.85:6302: OK >>> Send CLUSTER MEET to node 10.10.2.85:6302 to make it join the cluster. Waiting for the cluster to join . >>> Configure node as replica of 10.10.2.70:6300. [OK] New node added correctly. |
1
|
redis-cli -h 10.10.2.71 -p 6301 shutdown |
1
2
3
4
5
6
7
8
|
10.10.2.71:6300> CLUSTER nodes 85412cf3d8e69354115fc0991f470b32b9213cd7 10.10.2.70:6301 slave cd1f2c1f348bb4359337e7462c1e21dc82f1551b 0 1445911596844 17 connected 89fcc4994a99ed2fe9bbb908c58dfda2cf31e7d2 10.10.2.85:6301 slave e36cdef7a26ed59e8d9db2cf1dbc1997bfc9dfde 0 1445911594841 20 connected 2568dbd91fffa16ff93ea8db19275fd7ec8af41a 10.10.2.71:6300 myself,master - 0 0 11 connected 5462-6461 7461-10922 cd1f2c1f348bb4359337e7462c1e21dc82f1551b 10.10.2.70:6300 master - 0 1445911593839 17 connected 3000-5461 6462-7460 10923-16383 2b34532cd6937063d1da26cd4652881b73d97a06 10.10.2.85:6302 slave 2568dbd91fffa16ff93ea8db19275fd7ec8af41a 0 1445911592838 17 connected #已成功掛到了10.10.2.71:6300下 1f3ea36eacbe005a4b9ac52aeef6d83337dac051 10.10.2.71:6301 slave,fail 2568dbd91fffa16ff93ea8db19275fd7ec8af41a 1445911561982 1445911559778 11 disconnected e36cdef7a26ed59e8d9db2cf1dbc1997bfc9dfde 10.10.2.85:6300 master - 0 1445911595843 20 connected 0-2999 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
集羣 CLUSTER INFO 打印集羣的信息 CLUSTER NODES 列出集羣當前已知的所有節點(node),以及這些節點的相關信息。 節點 CLUSTER MEET <ip> <port> 將 ip 和 port 所指定的節點添加到集羣當中,讓它成爲集羣的一份子。 CLUSTER FORGET <node_id> 從集羣中移除 node_id 指定的節點。 CLUSTER REPLICATE <node_id> 將當前節點設置爲 node_id 指定的節點的從節點。 CLUSTER SAVECONFIG 將節點的配置文件保存到硬盤裏面。 槽(slot) CLUSTER ADDSLOTS <slot> [slot ...] 將一個或多個槽(slot)指派(assign)給當前節點。 CLUSTER DELSLOTS <slot> [slot ...] 移除一個或多個槽對當前節點的指派。 CLUSTER FLUSHSLOTS 移除指派給當前節點的所有槽,讓當前節點變成一個沒有指派任何槽的節點。 CLUSTER SETSLOT <slot> NODE <node_id> 將槽 slot 指派給 node_id 指定的節點,如果槽已經指派給另一個節點,那麼先讓另一個節點刪除該槽>,然後再進行指派。 CLUSTER SETSLOT <slot> MIGRATING <node_id> 將本節點的槽 slot 遷移到 node_id 指定的節點中。 CLUSTER SETSLOT <slot> IMPORTING <node_id> 從 node_id 指定的節點中導入槽 slot 到本節點。 CLUSTER SETSLOT <slot> STABLE 取消對槽 slot 的導入( import )或者遷移(migrate)。 鍵 CLUSTER KEYSLOT <key> 計算鍵 key 應該被放置在哪個槽上。 CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的鍵值對數量。 CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 個 slot 槽中的鍵。 |