搭建集群master节点至少要3个,slave节点也是3个,因为一个redis集群如果要对外提供可用的服务,那么集群中必须要有过半的master节点正常工作。基于这个特性,如果想搭建一个能够允许 n 个master节点挂掉的集群,那么就要搭建2n+1个master节点的集群
2个master节点,挂掉1个,则1不过半,则集群down掉,无法使用,容错率为0
3个master节点,挂掉1个,2>1,还可以正常运行,容错率为1
4个master节点,挂掉1个,3>1,还可以正常运行,但是当挂掉2个时,2=2,不过半,容错率依然为1
有3台服务器,每台服务器启用两个端口6379、6380,现在开始搭建
一、上传redis5.0.6到三台服务器
cd /cib/software/redis
tar -zxvf redis-5.0.5.tar.gz
二、每台服务器安装redis5.0.6
cd /cib/software/redis/redis-5.0.5
make && make install (如果这一步报错则执行以下命令)
1)make MALLOC=libc
2)cd src
3)make install
接下来进行下一步,创建端口文件夹
cd /cib/software/redis
mkdir redis-cluster
cd redis-cluster
mkdir 6379
mkdir 6380
将/cib/software/redis/redis-5.0.5/redis.conf文件复制到6379、6380文件夹下面
cd /cib/software/redis/redis-5.0.5
cp redis.conf /cib/software/redis/redis-cluster/6379
cp redis.conf /cib/software/redis/redis-cluster/6380
三、修改6379文件夹下面的redis.conf文件
bind 1号服务器ip
port 6379
daemonize yes
pidfile /var/run/redis-6379.pid
dbfilename dump6379.rdb
logfile /cib/software/redis/redis-cluster/6379/redis.log
dir /cib/software/redis/redis-cluster/6379
appendonly yes
appendfilename “appendonly6379.aof”
appendfsync always
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
注:logfile 和dir 对应相应的存放路径,自行修改
若需要设置密码放开requirepass,密码需要设置一样
四、修改6380文件夹下面的redis.conf文件
bind 1号服务器ip
port 6380
daemonize yes
pidfile /var/run/redis-6380.pid
dbfilename dump6380.rdb
logfile /cib/software/redis/redis-cluster/6380/redis.log
dir /cib/software/redis/redis-cluster/6380
appendonly yes
appendfilename “appendonly6380.aof”
appendfsync always
cluster-enabled yes
cluster-config-file nodes-6380.conf
cluster-node-timeout 15000
注:若需要设置密码放开requirepass,密码需要设置一样
每台服务器都进行如上配置,注意:唯一的区别是6379、6380下面的redis.conf中的bind 各自配置自己的ip
五、启动redis
启动每台服务器的redis
cd cd /cib/software/redis/redis-5.0.5/src
./redis-server /cib/software/redis/redis-cluster/6379/redis.con
./redis-server /cib/software/redis/redis-cluster/6380/redis.conf
需要开放防火墙ip和端口号否则执行下面命令会报错
六、创建集群
未设置密码用如下命令:
–redis-cli --cluster create 1号服务器:6379 1号服务器:6380 2号服务器:6379 2号服务器:6380 3号服务器:6379 3号服务器:6380 --cluster-replicas 1
设置了密码用如下命令:
–redis-cli --cluster create 1号服务器:6379 1号服务器:6380 2号服务器:6379 2号服务器:6380 3号服务器:6379 3号服务器:6380 --cluster-replicas 1 -a redis密码
否则会报错
[ERR] Node ip:6380 NOAUTH Authentication required.
输入创建集群的命令后会出现以下提示,注意Can I set the above configuration? (type ‘yes’ to accept): yes,该处请输入yes,不然好像分配不了哈希槽
[root@wzy-cloud redis_cluster]# redis-cli --cluster create 114.116.35.252:7000 114.116.35.252:7001 114.116.35.252:7002 114.116.35.252:7003 114.116.35.252:7004 114.116.35.252:7005 --cluster-replicas 1 -a wzy123
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 114.116.35.252:7003 to 114.116.35.252:7000
Adding replica 114.116.35.252:7004 to 114.116.35.252:7001
Adding replica 114.116.35.252:7005 to 114.116.35.252:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 4e571c020d1f2cca020132a9adfdea2a367da21d 114.116.35.252:7000
slots:[0-5460] (5461 slots) master
M: 838382153a78260e274c1d2d11a105dd3986a223 114.116.35.252:7001
slots:[5461-10922] (5462 slots) master
M: e86d01e92214015304461a104a9f14e3cedc7829 114.116.35.252:7002
slots:[10923-16383] (5461 slots) master
S: 22b1f3d83f068973c6e8a5d0b9e87c0c1b950594 114.116.35.252:7003
replicates 838382153a78260e274c1d2d11a105dd3986a223
S: 5248474e122d745b7e929a2705da210d3d150b4c 114.116.35.252:7004
replicates e86d01e92214015304461a104a9f14e3cedc7829
S: b3d20a419df22b4c9f4fe14c1fda22c2920c5c11 114.116.35.252:7005
replicates 4e571c020d1f2cca020132a9adfdea2a367da21d
Can I set the above configuration? (type 'yes' to accept): yes
输完yes后,会出现如下提示,[OK] All 16384 slots covered.说明成功啦
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 0.0.0.0:7000)
M: 4e571c020d1f2cca020132a9adfdea2a367da21d 114.116.35.252:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: b3d20a419df22b4c9f4fe14c1fda22c2920c5c11 114.116.35.252:7005
slots: (0 slots) slave
replicates 4e571c020d1f2cca020132a9adfdea2a367da21d
M: e86d01e92214015304461a104a9f14e3cedc7829 114.116.35.252:7002
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 22b1f3d83f068973c6e8a5d0b9e87c0c1b950594 114.116.35.252:7003
slots: (0 slots) slave
replicates 838382153a78260e274c1d2d11a105dd3986a223
M: 838382153a78260e274c1d2d11a105dd3986a223 114.116.35.252:7001
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 5248474e122d745b7e929a2705da210d3d150b4c 114.116.35.252:7004
slots: (0 slots) slave
replicates e86d01e92214015304461a104a9f14e3cedc7829
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
七、验证集群
[root@wzy-cloud redis_cluster]# redis-cli -c -p 7000
127.0.0.1:7000> auth wzy123
OK
127.0.0.1:7000> cluster nodes
b3d20a419df22b4c9f4fe14c1fda22c2920c5c11 114.116.35.252:7005@17005 slave 4e571c020d1f2cca020132a9adfdea2a367da21d 0 1563378113000 6 connected
e86d01e92214015304461a104a9f14e3cedc7829 114.116.35.252:7002@17002 master - 0 1563378115000 3 connected 10923-16383
22b1f3d83f068973c6e8a5d0b9e87c0c1b950594 114.116.35.252:7003@17003 slave 838382153a78260e274c1d2d11a105dd3986a223 0 1563378116490 4 connected
838382153a78260e274c1d2d11a105dd3986a223 114.116.35.252:7001@17001 master - 0 1563378114486 2 connected 5461-10922
5248474e122d745b7e929a2705da210d3d150b4c 114.116.35.252:7004@17004 slave e86d01e92214015304461a104a9f14e3cedc7829 0 1563378115488 5 connected
4e571c020d1f2cca020132a9adfdea2a367da21d 114.116.35.252:7000@17000 myself,master - 0 1563378114000 1 connected 0-5460
114.116.35.252:7000> set qwe 111
OK
114.116.35.252:7000>exit
[root@wzy-cloud 7000]# redis-cli -c -p 7003
114.116.35.252:7003> auth wzy123
OK
114.116.35.252:7003> get qwe
-> Redirected to slot [757] located at 127.0.0.1:7000
(error) NOAUTH Authentication required.
114.116.35.252:7000> auth wzy123
OK
127.0.0.1:7000> get qwe
"111"
127.0.0.1:7000>
查看集群信息还可以用如下命令
cluster info
注:连接服务器的redis
redis-cli -c -h ip地址 -p 6380