Junhc

岂止于博客

Redis高可用集群搭建

1. 下载redis安装包放在/usr/local目录下
wget http://download.redis.io/releases/redis-5.0.2.tar.gz

tar xzf redis-5.0.2.tar.gz

cd redis-5.0.2
2. 编译与安装
make & make install
3. 启动
src/redis-server redis.conf
4. 关闭
pkill redis-server

src/redis-cli shutdown
开始集群搭建
1. 修改redis.conf配置文件
1)daemonize yes(是否要用守护线程的方式启动)
2)port 8001(分别对每个机器的端口号进行设置)
3dir /usr/local/redis-cluster/8001/(指定数据文件存放位置,必须要指定不同的目录位置,不然会丢失数据)
4)cluster-enabled yes(启动集群模式)
5)cluster-config-file nodes-8001.conf(集群节点信息文件,这里800x最好和port对应上)
6)cluster-node-timeout 5000
7) bind 127.0.0.1(去掉bind绑定访问ip信息)
8) protected-mode  no   (关闭保护模式)
9)appendonly yes
如果要设置密码需要增加如下配置:
10)requirepass xxx     (设置redis访问密码)
11)masterauth  xxx     (设置集群节点间访问密码,跟上面一致)
2. 启动redis实例
/usr/local/redis/redis-5.0.2/src/redis-server /usr/local/redis-cluster/700*/redis.conf
3. 使用redis-cli创建redis集群
/usr/local/redis-5.0.4/src/redis-cli --cluster create --cluster-replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7006 to 127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 4626e1b0cd356df4fe365181e309cda72c371499 127.0.0.1:7001
   slots:[0-5460] (5461 slots) master
M: 7655270c80c35a0c1d7e2dce7cec52b12664cfc6 127.0.0.1:7002
   slots:[5461-10922] (5462 slots) master
M: 27720a39099c3b219e57173527205bdea431d531 127.0.0.1:7003
   slots:[10923-16383] (5461 slots) master
S: 420ca6b67bbe4db60630bc20fd2f2a0c12955fe0 127.0.0.1:7004
   replicates 7655270c80c35a0c1d7e2dce7cec52b12664cfc6
S: d5f4a2122fc632f9b10c4074b662c4b6653eabf2 127.0.0.1:7005
   replicates 27720a39099c3b219e57173527205bdea431d531
S: 58f7091ebdbed34d829cd9547da43dcc85e96d32 127.0.0.1:7006
   replicates 4626e1b0cd356df4fe365181e309cda72c371499
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 127.0.0.1:7001)
M: 4626e1b0cd356df4fe365181e309cda72c371499 127.0.0.1:7001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 420ca6b67bbe4db60630bc20fd2f2a0c12955fe0 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 7655270c80c35a0c1d7e2dce7cec52b12664cfc6
S: 58f7091ebdbed34d829cd9547da43dcc85e96d32 127.0.0.1:7006
   slots: (0 slots) slave
   replicates 4626e1b0cd356df4fe365181e309cda72c371499
M: 7655270c80c35a0c1d7e2dce7cec52b12664cfc6 127.0.0.1:7002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: d5f4a2122fc632f9b10c4074b662c4b6653eabf2 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 27720a39099c3b219e57173527205bdea431d531
M: 27720a39099c3b219e57173527205bdea431d531 127.0.0.1:7003
   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.
开始水平扩展
1. 新增节点70077008
$ sudo /usr/local/redis-5.0.4/src/redis-cli --cluster add-node 127.0.0.1:7007 127.0.0.1:7001
>>> Adding node 127.0.0.1:7007 to cluster 127.0.0.1:7001
Could not connect to Redis at 127.0.0.1:7006: Connection refused
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: 4626e1b0cd356df4fe365181e309cda72c371499 127.0.0.1:7001
   slots:[0-5460] (5461 slots) master
S: d5f4a2122fc632f9b10c4074b662c4b6653eabf2 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 27720a39099c3b219e57173527205bdea431d531
M: 27720a39099c3b219e57173527205bdea431d531 127.0.0.1:7003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 7655270c80c35a0c1d7e2dce7cec52b12664cfc6 127.0.0.1:7002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 420ca6b67bbe4db60630bc20fd2f2a0c12955fe0 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 7655270c80c35a0c1d7e2dce7cec52b12664cfc6
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 127.0.0.1:7007 to make it join the cluster.
[OK] New node added correctly.
2. 重新分配slots
127.0.0.1:7001> cluster nodes
fdd2a3760a0b8ae540c0473a1cb8fad9b67d6256 127.0.0.1:7007@17007 master - 0 1566540984588 0 connected
d5f4a2122fc632f9b10c4074b662c4b6653eabf2 127.0.0.1:7005@17005 slave 27720a39099c3b219e57173527205bdea431d531 0 1566540984590 5 connected
27720a39099c3b219e57173527205bdea431d531 127.0.0.1:7003@17003 master - 0 1566540986619 3 connected 10923-16383
7655270c80c35a0c1d7e2dce7cec52b12664cfc6 127.0.0.1:7002@17002 master - 0 1566540985602 2 connected 5461-10922
420ca6b67bbe4db60630bc20fd2f2a0c12955fe0 127.0.0.1:7004@17004 slave 7655270c80c35a0c1d7e2dce7cec52b12664cfc6 0 1566540985000 4 connected
58f7091ebdbed34d829cd9547da43dcc85e96d32 127.0.0.1:7006@17006 master,fail - 1566538225567 1566538224552 7 disconnected
4626e1b0cd356df4fe365181e309cda72c371499 127.0.0.1:7001@17001 myself,master - 0 1566540986000 8 connected 0-5460
$ /usr/local/redis-5.0.4/src/redis-cli --cluster reshard 127.0.0.1:7001
Could not connect to Redis at 127.0.0.1:7006: Connection refused
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: 4626e1b0cd356df4fe365181e309cda72c371499 127.0.0.1:7001
   slots:[0-5460] (5461 slots) master
M: fdd2a3760a0b8ae540c0473a1cb8fad9b67d6256 127.0.0.1:7007
   slots: (0 slots) master
S: d5f4a2122fc632f9b10c4074b662c4b6653eabf2 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 27720a39099c3b219e57173527205bdea431d531
M: 27720a39099c3b219e57173527205bdea431d531 127.0.0.1:7003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 7655270c80c35a0c1d7e2dce7cec52b12664cfc6 127.0.0.1:7002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 420ca6b67bbe4db60630bc20fd2f2a0c12955fe0 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 7655270c80c35a0c1d7e2dce7cec52b12664cfc6
[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)? 1000
What is the receiving node ID? fdd2a3760a0b8ae540c0473a1cb8fad9b67d6256
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: 4626e1b0cd356df4fe365181e309cda72c371499
Source node #2: done
3. 使用replicate命令将7008指定为7007的从节点
$ sudo /usr/local/redis-5.0.4/src/redis-cli -c -h 127.0.0.1 -p 7008
127.0.0.1:7008> cluster nodes
27720a39099c3b219e57173527205bdea431d531 127.0.0.1:7003@17003 master - 0 1566541756000 3 connected 10923-16383
797bb0661cee8d19bd1e11f092b2cee1c3821cef 127.0.0.1:7006@17006 handshake - 1566541753290 0 0 disconnected
fdd2a3760a0b8ae540c0473a1cb8fad9b67d6256 127.0.0.1:7007@17007 master - 0 1566541754510 9 connected 0-999
4626e1b0cd356df4fe365181e309cda72c371499 127.0.0.1:7001@17001 master - 0 1566541755526 8 connected 1000-5460
420ca6b67bbe4db60630bc20fd2f2a0c12955fe0 127.0.0.1:7004@17004 slave 7655270c80c35a0c1d7e2dce7cec52b12664cfc6 0 1566541755832 2 connected
d5f4a2122fc632f9b10c4074b662c4b6653eabf2 127.0.0.1:7005@17005 slave 27720a39099c3b219e57173527205bdea431d531 0 1566541754511 3 connected
7655270c80c35a0c1d7e2dce7cec52b12664cfc6 127.0.0.1:7002@17002 master - 0 1566541755000 2 connected 5461-10922
f09a64f6b8ed9eb0877476d55017301a1dc556ec 127.0.0.1:7008@17008 myself,master - 0 1566541755000 0 connected
127.0.0.1:7008> CLUSTER REPLICATE fdd2a3760a0b8ae540c0473a1cb8fad9b67d6256
OK
127.0.0.1:7008> cluster nodes
27720a39099c3b219e57173527205bdea431d531 127.0.0.1:7003@17003 master - 0 1566541791409 3 connected 10923-16383
fdd2a3760a0b8ae540c0473a1cb8fad9b67d6256 127.0.0.1:7007@17007 master - 0 1566541790000 9 connected 0-999
4626e1b0cd356df4fe365181e309cda72c371499 127.0.0.1:7001@17001 master - 0 1566541791511 8 connected 1000-5460
420ca6b67bbe4db60630bc20fd2f2a0c12955fe0 127.0.0.1:7004@17004 slave 7655270c80c35a0c1d7e2dce7cec52b12664cfc6 0 1566541791512 2 connected
d5f4a2122fc632f9b10c4074b662c4b6653eabf2 127.0.0.1:7005@17005 slave 27720a39099c3b219e57173527205bdea431d531 0 1566541791000 3 connected
7655270c80c35a0c1d7e2dce7cec52b12664cfc6 127.0.0.1:7002@17002 master - 0 1566541790594 2 connected 5461-10922
b5e8daf75bcbc718987259ab62ffe1e18addb3fe 127.0.0.1:7006@17006 handshake - 1566541787944 0 0 disconnected
f09a64f6b8ed9eb0877476d55017301a1dc556ec 127.0.0.1:7008@17008 myself,slave fdd2a3760a0b8ae540c0473a1cb8fad9b67d6256 0 1566541790000 0 connected
常见问题
  • [ERR] Node 127.0.0.1:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
删除每个节点下aof、rdb、nodes.conf本地备份文件
  • (error) MOVED 12706 127.0.0.1:7003
/usr/local/redis-5.0.4/src/redis-cli -c -h 127.0.0.1 -p 7003
`-c 表示启用集群模式`
参考资料