redis集群搭建和之前写的redis伪集群搭建基本是一样的,区别在于集群由多个服务器上搭建,伪集群则在单个服务器上搭建多个节点。集群搭建也可以参考【Redis伪集群搭建】

准备工作:准备3台服务器(或者3个虚拟机),我准备的3个服务器:192.168.0.24、192.168.0.111、192.168.0.58,相互之前是可以通信的。

当然,如果你服务器(或虚拟机)足够多,你也可以每个服务器只建一个节点,比如找6台服务器(或虚拟机)搭建集群。建议集群主节点个数为单数,保证了只有故障的主节点大于一半时,才会导致集群不能工作;每个主节点至少配置一个从节点,当主节点故障时有从节点切换成主节点。

1、打开24,新建文件夹redis-cluster,并在redis-cluster目录下新建8001、8002两个文件夹

[root@*** ~]# cd /opt
[root@*** opt]# ls
ORCLfmap  rh
[root@*** opt]# mkdir redis-cluster
[root@*** opt]# ls
ORCLfmap  redis-cluster  rh
[root@*** opt]# cd redis-cluster/
[root@*** redis-cluster]# ls
[root@*** redis-cluster]# mkdir 8001
[root@*** redis-cluster]# mkdir 8002
[root@*** redis-cluster]# ls
8001  8002

2、下载编译redis,也可以点击【Redis官网】,按照Installation下命令完成下载和编译。

[root@*** redis-cluster]# wget http://download.redis.io/releases/redis-5.0.4.tar.gz
.
.
.
[root@*** redis-cluster]# ls
8001  8002  redis-5.0.4.tar.gz
[root@*** redis-cluster]# tar xzf redis-5.0.4.tar.gz 
[root@*** redis-cluster]# cd redis-5.0.4/
[root@*** redis-5.0.4]# make
.
.
.

如果make遇到问题,参考下【Linux下安装redis】

3、复制配置文件,修改配置

[root@*** redis-cluster]# cp redis-5.0.4/redis.conf  8001/redis8001.conf
[root@*** redis-cluster]# vim 8001/redis8001.conf

修改成以下对应配置

#绑定ip,也可以在bind配置加上#,注掉bind
bind 192.168.0.24

#关闭保护模式
protected-mode no

#设置端口
port 8001

#后台运行
daemonize yes

#pid文件路径
pidfile /opt/redis-cluster/8001/redis_8001.pid

#日志文件路径
logfile "/opt/redis-cluster/8001/redis8001.log"

#开启集群模式
cluster-enabled yes

#redis集群节点配置文件
cluster-config-file /opt/redis-cluster/8001/nodes-8001.conf

#集群节点访问超时时间
cluster-node-timeout 15000

#集群迁移屏障
cluster-migration-barrier 1

#当负责一个插槽的主库下线且没有相应的从库进行故障恢复时,集群不可用
cluster-require-full-coverage yes

:%s/8001/8002/g将配置中的8001全部改成8002。

[root@*** redis-cluster]# cp 8001/redis8001.conf  8002/redis8002.conf
[root@*** redis-cluster]# vim 8002/redis8002.conf

4、复制节点

使用命令scp将redis文件复制到111和58,复制的文件日志省略

[root@*** redis-cluster]# scp -r /opt/redis-cluster/ root@192.168.0.111:/opt/redis-cluster/
The authenticity of host '192.168.0.111 (192.168.0.111)' can't be established.
RSA key fingerprint is 1a:a7:17:8b:bf:33:ce:4d:0a:6d:80:26:cf:59:40:4c.
Are you sure you want to continue connecting (yes/no)?  yes
Warning: Permanently added '192.168.0.111' (RSA) to the list of known hosts.
root@192.168.0.111's password: 
.
.
.
[root@*** redis-cluster]# scp -r /opt/redis-cluster/ root@192.168.0.58:/opt/redis-cluster/
The authenticity of host '192.168.0.58 (192.168.0.58)' can't be established.
RSA key fingerprint is 1a:a7:17:8b:bf:33:ce:4d:0a:6d:80:26:cf:59:40:4c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.58' (RSA) to the list of known hosts.
root@192.168.0.58's password:

记得修改bind配置,分别改成192.168.0.111和192.168.0.58,查找bind可以使用命令/bind或?bind

[root@*** ~]# cd /opt/redis-cluster/
[root@*** redis-cluster]# vim 8001/redis8001.conf

5、启动redis服务

启动命令:

[root@*** redis-cluster]# ./redis-5.0.4/src/redis-server  8001/redis8001.conf  &
[root@*** redis-cluster]# ./redis-5.0.4/src/redis-server  8002/redis8002.conf  &

1)、24上启动redis服务

[root@*** redis-cluster]# ps -ef|grep redis
root     11852     1  0 21:55 ?        00:00:00 ./redis-5.0.4/src/redis-server 192.168.0.24:8001 [cluster]
root     11865     1  0 21:55 ?        00:00:00 ./redis-5.0.4/src/redis-server 192.168.0.24:8002 [cluster]
root     11875 10722  0 21:55 pts/0    00:00:00 grep --color=auto redis
[2]+  Done                    ./redis-5.0.4/src/redis-server 8002/redis8002.conf

2)、111上启动redis服务

[root@*** redis-cluster]# ps -ef|grep redis
root      6125     1  0 22:38 ?        00:00:00 ./redis-5.0.4/src/redis-server 192.168.0.111:8001 [cluster]
root      6142     1  0 22:40 ?        00:00:00 ./redis-5.0.4/src/redis-server 192.168.0.111:8002 [cluster]
root      6148  5850  0 22:40 pts/0    00:00:00 grep --color=auto redis
[2]+  Done                    ./redis-5.0.4/src/redis-server 8002/redis8002.conf

3)、58上启动redis服务

[root@e*** redis-cluster]# ps -ef|grep redis
root     29014     1  0 22:47 ?        00:00:00 ./redis-5.0.4/src/redis-server 192.168.0.58:8001 [cluster]
root     29024     1  0 22:47 ?        00:00:00 ./redis-5.0.4/src/redis-server 192.168.0.58:8002 [cluster]
root     29139 26199  0 22:49 pts/0    00:00:00 grep --color=auto redis
[2]+  Done                    ./redis-5.0.4/src/redis-server 8002/redis8002.conf

6、构建集群

[root@***redis-cluster]# ./redis-5.0.4/src/redis-cli --cluster-replicas 1 --cluster create 192.168.0.24:8001 192.168.0.111:8001 192.168.0.58:8001 192.168.0.24:8002 192.168.0.111:8002 192.168.0.58:8002
>>> 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.0.111:8002 to 192.168.0.24:8001
Adding replica 192.168.0.58:8002 to 192.168.0.111:8001
Adding replica 192.168.0.24:8002 to 192.168.0.58:8001
M: e80b66392e8c56b46ca9c899056f4d3be15d71da 192.168.0.24:8001
   slots:[0-5460] (5461 slots) master
M: 1dd0742fe874901f6d59a1c55611779977dd5e9f 192.168.0.111:8001
   slots:[5461-10922] (5462 slots) master
M: 533fd78ee6316b8e87231b690a03b722960dbfad 192.168.0.58:8001
   slots:[10923-16383] (5461 slots) master
S: 0765663dff45fef94deb7c471b962ba983cf733a 192.168.0.24:8002
   replicates 533fd78ee6316b8e87231b690a03b722960dbfad
S: 07bfd8acd3e7dc3cf68a617f386c282291c81b6a 192.168.0.111:8002
   replicates e80b66392e8c56b46ca9c899056f4d3be15d71da
S: d4387bb2288d436e12a17dc9a750bc447adb6e55 192.168.0.58:8002
   replicates 1dd0742fe874901f6d59a1c55611779977dd5e9f
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.0.24:8001)
M: e80b66392e8c56b46ca9c899056f4d3be15d71da 192.168.0.24:8001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 1dd0742fe874901f6d59a1c55611779977dd5e9f 192.168.0.111:8001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 533fd78ee6316b8e87231b690a03b722960dbfad 192.168.0.58:8001
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 0765663dff45fef94deb7c471b962ba983cf733a 192.168.0.24:8002
   slots: (0 slots) slave
   replicates 533fd78ee6316b8e87231b690a03b722960dbfad
S: 07bfd8acd3e7dc3cf68a617f386c282291c81b6a 192.168.0.111:8002
   slots: (0 slots) slave
   replicates e80b66392e8c56b46ca9c899056f4d3be15d71da
S: d4387bb2288d436e12a17dc9a750bc447adb6e55 192.168.0.58:8002
   slots: (0 slots) slave
   replicates 1dd0742fe874901f6d59a1c55611779977dd5e9f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

7、查看节点信息

[root@*** redis-cluster]# ./redis-5.0.4/src/redis-cli -h 192.168.0.24 -p 8001
192.168.0.24:8001> cluster nodes
e80b66392e8c56b46ca9c899056f4d3be15d71da 192.168.0.24:8001@18001 myself,master - 0 1564931257000 1 connected 0-5460
1dd0742fe874901f6d59a1c55611779977dd5e9f 192.168.0.111:8001@18001 master - 0 1564931259373 2 connected 5461-10922
533fd78ee6316b8e87231b690a03b722960dbfad 192.168.0.58:8001@18001 master - 0 1564931260429 3 connected 10923-16383
0765663dff45fef94deb7c471b962ba983cf733a 192.168.0.24:8002@18002 slave 533fd78ee6316b8e87231b690a03b722960dbfad 0 1564931259000 4 connected
07bfd8acd3e7dc3cf68a617f386c282291c81b6a 192.168.0.111:8002@18002 slave e80b66392e8c56b46ca9c899056f4d3be15d71da 0 1564931256000 5 connected
d4387bb2288d436e12a17dc9a750bc447adb6e55 192.168.0.58:8002@18002 slave 1dd0742fe874901f6d59a1c55611779977dd5e9f 0 1564931257000 6 connected
192.168.0.24:8001> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.0.111,port=8002,state=online,offset=756,lag=0
master_replid:6618ba6c58f612aad55de8dfaf57bafcee099456
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:756
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:756

8、简单测试下

192.168.0.24:8001> quit
[root@*** redis-cluster]# ./redis-5.0.4/src/redis-cli -c -h 192.168.0.24 -p 8001
192.168.0.24:8001> set test aaa
-> Redirected to slot [6918] located at 192.168.0.111:8001
OK
192.168.0.111:8001>