1.部署运行环境
[root@host51 ~]# cat /var/lib/redis/6379/nodes-6351.conf
07df1de074025884c615cc2b0e1331742ca77419 :0@0 myself,master - 0 0 0 connected
vars currentEpoch 0 lastVoteEpoch 0
[root@host51 ~]# yum -y install ruby rubygems
[root@host51 ~]# cd redis-cluster/
[root@host51 redis-cluster]# ls
redis-3.2.1.gem ruby-devel-2.0.0.648-30.el7.x86_64.rpm
[root@host51 redis-cluster]# rpm -ivh ruby-devel-2.0.0.648-30.el7.x86_64.rpm
[root@host51 ~]# cd redis-cluster/
[root@host51 redis-cluster]# ls
redis-3.2.1.gem ruby-devel-2.0.0.648-30.el7.x86_64.rpm
[root@host51 redis-cluster]# gem install redis-3.2.1.gem
Successfully installed redis-3.2.1
Parsing documentation for redis-3.2.1
Installing ri documentation for redis-3.2.1
1 gem installed
2.创建ruby脚本:
[root@host51 ~]# cd redis-4.0.8/
[root@host51 redis-4.0.8]# ls
00-RELEASENOTES COPYING Makefile redis.conf runtest-sentinel tests
BUGS deps MANIFESTO runtest sentinel.conf utils
CONTRIBUTING INSTALL README.md runtest-cluster src
[root@host51 redis-4.0.8]# cd src/
[root@host51 src]# ls
[root@host51 src]# cp redis-trib.rb /root/bin
[root@host51 src]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@host51 src]# ls -ld /root/bin
-rwxr-xr-x. 1 root root 65991 7月 30 09:41 /root/bin
创建集群:
redis-trib.rb create --replicas 1 192.168.4.51:6351 192.168.4..52:6352 192.168.4.53:6353 192.168.4.54:6354 192.168.4.55:6354 192.168.4.56:6356
[root@host51 src]# ./redis-trib.rb create --replicas 1 192.168.4.51:6351 192.168.4.52:6352 192.168.4.53:6353 192.168.4.54:6354 192.168.4.55:6355 192.168.4.56:6356
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.4.51:6351
192.168.4.52:6352
192.168.4.53:6353
Adding replica 192.168.4.55:6355 to 192.168.4.51:6351
Adding replica 192.168.4.56:6356 to 192.168.4.52:6352
Adding replica 192.168.4.54:6354 to 192.168.4.53:6353
M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351
slots:0-5460 (5461 slots) master
M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
M: 29023faf64c53fedbe7d8861ec548d3b12c9e821 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
S: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354
replicates 29023faf64c53fedbe7d8861ec548d3b12c9e821
S: bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355
replicates 07df1de074025884c615cc2b0e1331742ca77419
S: a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356
replicates c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4
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.4.51:6351)
M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356
slots: (0 slots) slave
replicates c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4
S: bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355
slots: (0 slots) slave
replicates 07df1de074025884c615cc2b0e1331742ca77419
M: 29023faf64c53fedbe7d8861ec548d3b12c9e821 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354
slots: (0 slots) slave
replicates 29023faf64c53fedbe7d8861ec548d3b12c9e821
M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> cluster nodes
a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356@16356 slave c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 0 1532918336592 6 connected
bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355@16355 slave 07df1de074025884c615cc2b0e1331742ca77419 0 1532918337596 5 connected
07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351@16351 myself,master - 0 1532918334000 1 connected 0-5460
29023faf64c53fedbe7d8861ec548d3b12c9e821 192.168.4.53:6353@16353 master - 0 1532918335590 3 connected 10923-16383
cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354@16354 slave 29023faf64c53fedbe7d8861ec548d3b12c9e821 0 1532918336000 4 connected
c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352@16352 master - 0 1532918336592 2 connected 5461-10922
[root@host50 ~]# redis-cli -c -h 192.168.4.51 -p 6351
192.168.4.51:6351> keys *
(empty list or set)
192.168.4.51:6351> set name bob
-> Redirected to slot [
5798] located at
192.168.4.52:6352
OK
在客户端接连集群存储数据(连接master库的ip地址)
存储数据必须连接集群中的master主机(连接任意一个master主机都可以)
[root@host50 ~]# redis-cli -c -h 192.168.4.51 -p 6351
192.168.4.51:6351> set nme krry
OK
192.168.4.51:6351> set age 109
OK
192.168.4.51:6351> keys *
1) "nme"
2) "age"
到存储库里查看数据:
192.168.4.51:6351> get name
-> Redirected to slot [5798] located at 192.168.4.52:6352
"bob"
192.168.4.52:6352> get age
-> Redirected to slot [741] located at 192.168.4.51:6351
"109"
192.168.4.52:6352> keys *
1) "name"
192.168.4.52:6352> get name
"bob"
192.168.4.51:6351> set name bob
-> Redirected to slot [5798] located at 192.168.4.52:6352
OK
192.168.4.52:6352>
存储数据的原理
读取数据的原理;
测试高可用集群:
[root@host51 src]# ./redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 29023faf64c53fedbe7d8861ec548d3b12c9e821 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355
slots: (0 slots) slave
replicates 07df1de074025884c615cc2b0e1331742ca77419
M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356
slots: (0 slots) slave
replicates c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4
S: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354
slots: (0 slots) slave
replicates 29023faf64c53fedbe7d8861ec548d3b12c9e821
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@host53 ~]# /etc/init.d/redis_6379 stop
Stopping ...
[root@host51 src]# ./redis-trib.rb check 192.168.4.51:6351
*** WARNING: 192.168.4.54:6354 claims to be slave of unknown node ID 29023faf64c53fedbe7d8861ec548d3b12c9e821.
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355
slots: (0 slots) slave
replicates 07df1de074025884c615cc2b0e1331742ca77419
M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356
slots: (0 slots) slave
replicates c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4
S: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354
slots: (0 slots) slave
replicates 29023faf64c53fedbe7d8861ec548d3b12c9e821
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[ERR] Not all 16384 slots are covered by nodes.
[root@host53 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host53 ~]# ss -ntulp | grep redis
tcp LISTEN 0 128 192.168.4.53:6353 *:* users:(("redis-server",pid=1765,fd=6))
tcp LISTEN 0 128 192.168.4.53:16353 *:* users:(("redis-server",pid=1765,fd=8))
[root@host51 src]# ./redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 07df1de074025884c615cc2b0e1331742ca77419 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 29023faf64c53fedbe7d8861ec548d3b12c9e821 192.168.4.53:6353
slots: (0 slots) slave
replicates cd3542ec299850a4a8f2c8cf42874ac06d3f19b6
S: bdee4e018b155fb55d25be5956bcef0de69c73d1 192.168.4.55:6355
slots: (0 slots) slave
replicates 07df1de074025884c615cc2b0e1331742ca77419
M: c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: a9cc8b066e53b38391172a200b48f96b1cde40fc 192.168.4.56:6356
slots: (0 slots) slave
replicates c4ff83ca9d9b2d91e74806a0f6b9fc2060ccd4f4
M: cd3542ec299850a4a8f2c8cf42874ac06d3f19b6 192.168.4.54:6354
slots:10923-16383 (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.