Redis 集群环境搭建

创建账号:

useradd -d /home/rcluster -g ai rcluster
echo "123" | passwd --stdin rcluster

编译与安装:

tar -zxvf redis-5.0.14.tar.gz
cd redis-5.0.14
make
make install PREFIX=/home/rcluster
cd ~
mkdir -p 6371/data/
mkdir -p 6372/data/
mkdir -p 6373/data/
mkdir -p 6374/data/
mkdir -p 6375/data/
mkdir -p 6376/data/

cp redis-5.0.14/redis.conf 6371/
cp redis-5.0.14/redis.conf 6372/
cp redis-5.0.14/redis.conf 6373/
cp redis-5.0.14/redis.conf 6374/
cp redis-5.0.14/redis.conf 6375/
cp redis-5.0.14/redis.conf 6376/

首先拉起独立的实例:

[rcluster@c7-base ~]$ ./bin/redis-server 6371/redis.conf 
18850:C 16 Nov 2022 17:38:53.846 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18850:C 16 Nov 2022 17:38:53.846 # Redis version=5.0.14, bits=64, commit=00000000, modified=0, pid=18850, just started
18850:C 16 Nov 2022 17:38:53.846 # Configuration loaded
[rcluster@c7-base ~]$ ./bin/redis-server 6372/redis.conf 
18855:C 16 Nov 2022 17:38:57.452 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18855:C 16 Nov 2022 17:38:57.452 # Redis version=5.0.14, bits=64, commit=00000000, modified=0, pid=18855, just started
18855:C 16 Nov 2022 17:38:57.452 # Configuration loaded
[rcluster@c7-base ~]$ ./bin/redis-server 6373/redis.conf 
18860:C 16 Nov 2022 17:39:00.607 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18860:C 16 Nov 2022 17:39:00.607 # Redis version=5.0.14, bits=64, commit=00000000, modified=0, pid=18860, just started
18860:C 16 Nov 2022 17:39:00.607 # Configuration loaded
[rcluster@c7-base ~]$ ./bin/redis-server 6374/redis.conf 
18865:C 16 Nov 2022 17:39:03.072 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18865:C 16 Nov 2022 17:39:03.072 # Redis version=5.0.14, bits=64, commit=00000000, modified=0, pid=18865, just started
18865:C 16 Nov 2022 17:39:03.072 # Configuration loaded
[rcluster@c7-base ~]$ ./bin/redis-server 6375/redis.conf 
18870:C 16 Nov 2022 17:39:05.302 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18870:C 16 Nov 2022 17:39:05.302 # Redis version=5.0.14, bits=64, commit=00000000, modified=0, pid=18870, just started
18870:C 16 Nov 2022 17:39:05.302 # Configuration loaded
[rcluster@c7-base ~]$ ./bin/redis-server 6376/redis.conf 
18875:C 16 Nov 2022 17:39:07.512 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18875:C 16 Nov 2022 17:39:07.512 # Redis version=5.0.14, bits=64, commit=00000000, modified=0, pid=18875, just started
18875:C 16 Nov 2022 17:39:07.512 # Configuration loaded

查看独立实例是否已启动:

[rcluster@c7-base ~]$ ps -ef | grep redis
rcluster 18851     1  0 17:38 ?        00:00:00 ./bin/redis-server 0.0.0.0:6371 [cluster]
rcluster 18856     1  0 17:38 ?        00:00:00 ./bin/redis-server 0.0.0.0:6372 [cluster]
rcluster 18861     1  0 17:39 ?        00:00:00 ./bin/redis-server 0.0.0.0:6373 [cluster]
rcluster 18866     1  0 17:39 ?        00:00:00 ./bin/redis-server 0.0.0.0:6374 [cluster]
rcluster 18871     1  0 17:39 ?        00:00:00 ./bin/redis-server 0.0.0.0:6375 [cluster]
rcluster 18876     1  0 17:39 ?        00:00:00 ./bin/redis-server 0.0.0.0:6376 [cluster]

创建集群:

[rcluster@c7-base ~]$ bin/redis-cli --cluster create  192.168.181.128:6371 192.168.181.128:6372 192.168.181.128:6373 192.168.181.128:6374 192.168.181.128:6375 192.168.181.128:6376 --cluster-replicas 1 -a 123456
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.181.128:6375 to 192.168.181.128:6371
Adding replica 192.168.181.128:6376 to 192.168.181.128:6372
Adding replica 192.168.181.128:6374 to 192.168.181.128:6373
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 2329b89bc29249271ac7d3930bedeff0dc1479e3 192.168.181.128:6371
   slots:[0-5460] (5461 slots) master
M: e9823f0ef9ff2144d5be0a71806abf3f9b633132 192.168.181.128:6372
   slots:[5461-10922] (5462 slots) master
M: f6f9dd4e4a69f694446ca9a35e3eaa770e10a213 192.168.181.128:6373
   slots:[10923-16383] (5461 slots) master
S: 13422b6aab62dbdd84bf0edc34357bc21f2a5e09 192.168.181.128:6374
   replicates e9823f0ef9ff2144d5be0a71806abf3f9b633132
S: 0e7682bad96a1f494596844577becc5451f8d7fa 192.168.181.128:6375
   replicates f6f9dd4e4a69f694446ca9a35e3eaa770e10a213
S: b7460c27f649ccb548177c129b8cb5cc8d370e75 192.168.181.128:6376
   replicates 2329b89bc29249271ac7d3930bedeff0dc1479e3
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.181.128:6371)
M: 2329b89bc29249271ac7d3930bedeff0dc1479e3 192.168.181.128:6371
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 13422b6aab62dbdd84bf0edc34357bc21f2a5e09 192.168.181.128:6374
   slots: (0 slots) slave
   replicates e9823f0ef9ff2144d5be0a71806abf3f9b633132
S: b7460c27f649ccb548177c129b8cb5cc8d370e75 192.168.181.128:6376
   slots: (0 slots) slave
   replicates 2329b89bc29249271ac7d3930bedeff0dc1479e3
S: 0e7682bad96a1f494596844577becc5451f8d7fa 192.168.181.128:6375
   slots: (0 slots) slave
   replicates f6f9dd4e4a69f694446ca9a35e3eaa770e10a213
M: e9823f0ef9ff2144d5be0a71806abf3f9b633132 192.168.181.128:6372
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: f6f9dd4e4a69f694446ca9a35e3eaa770e10a213 192.168.181.128:6373
   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.

查看集群创建是否成功:

[rcluster@c7-base ~]$ bin/redis-cli -c -p 6371 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6371> cluster nodes
13422b6aab62dbdd84bf0edc34357bc21f2a5e09 192.168.181.128:6374@16374 slave e9823f0ef9ff2144d5be0a71806abf3f9b633132 0 1668591757000 4 connected
b7460c27f649ccb548177c129b8cb5cc8d370e75 192.168.181.128:6376@16376 slave 2329b89bc29249271ac7d3930bedeff0dc1479e3 0 1668591758726 6 connected
0e7682bad96a1f494596844577becc5451f8d7fa 192.168.181.128:6375@16375 slave f6f9dd4e4a69f694446ca9a35e3eaa770e10a213 0 1668591756000 5 connected
e9823f0ef9ff2144d5be0a71806abf3f9b633132 192.168.181.128:6372@16372 master - 0 1668591757720 2 connected 5461-10922
f6f9dd4e4a69f694446ca9a35e3eaa770e10a213 192.168.181.128:6373@16373 master - 0 1668591757000 3 connected 10923-16383
2329b89bc29249271ac7d3930bedeff0dc1479e3 192.168.181.128:6371@16371 myself,master - 0 1668591754000 1 connected 0-5460

集群启动脚本:

[rcluster@c7-base bin]$ more start-cluster.sh 
#!/bin/sh

./redis-server ../6371/redis.conf
./redis-server ../6372/redis.conf
./redis-server ../6373/redis.conf
./redis-server ../6374/redis.conf
./redis-server ../6375/redis.conf
./redis-server ../6376/redis.conf

集群停止脚本:

[rcluster@c7-base bin]$ more stop-cluster.sh 
#!/bin/sh

ps -ef | grep `whoami` | grep redis-server | grep -v grep | awk '{print $2}' | xargs kill

 

常见错误:

1. redis cluster 在 master 节点 fail 后未自动切换主备。检查一下 redis.conf 中的 requirepass 和 masterauth 是否已配置,从节点的 masterauth 与主节点的 requirepass 一致。

posted @ 2022-11-16 17:44  Steven.Chow  阅读(77)  评论(0编辑  收藏  举报