原文链接:https://blog.csdn.net/zzz3112362088/article/details/89228943
原文链接:https://blog.csdn.net/baidu_38558076/article/details/90707045
集群搭建官方指导
https://redis.io/topics/cluster-tutorial
下载软件包
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
$ wget http://download.redis.io/releases/redis-5.0.5.tar.gz
$ tar xzf redis-5.0.5.tar.gz
$ cd redis-5.0.5
$ make
要想外部服务器访问到我们搭建的集群,我们需要对脚本修改一下:
start部分增加如下配置
--bind 指定我们要绑定的ip(服务器ip) --requirepass 指定我们的密码 --protected-mode no 关闭安全模式(外部应用可以连接到redis)
create 部分 修改如下:
stop 部分 修改如下:
修改后保存
然后在utils/create-cluster目录下执行
./create-cluster start
执行 ps -ef | grep redis 确定redis实例创建成功
./redis-cli --cluster create 192.168.1.228:30001 192.168.1.228:30002 192.168.1.228:30003 192.168.1.228:30004 192.168.1.228:30005 192.168.1.228:30006 --cluster-replicas 1
可以看到已经为我们创建成功,并自动分配了3个主节点,3个从节点,然后提示我们是否确定创建,输入yes
可以看到集群已经创建成功,并且为3个主节点分配了hash槽
可以看到已经为我们创建成功,并自动分配了3个主节点,3个从节点,然后提示我们是否确定创建,输入yes
当遇到分片分配错误时可以通过命令修复redis-cli --cluster fix ip:6371 -a redispassword
切换主从:将slave切换为master,将master切换为slave。登录slave的redis服务,执行cluster failover命令
//搭建记录:
[root@hanbing src]# cd ../utils/create-cluster/
[root@hanbing create-cluster]# vi create-cluster
[root@hanbing create-cluster]# ps -ef | grep redis
root 8522 1 0 04:19 ? 00:00:13 ./redis-server *:6379
root 8582 7992 0 04:34 pts/3 00:00:00 vi redis.conf
root 8687 1 0 05:07 ? 00:00:05 ../../src/redis-server *:30001 [cluster]
root 8689 1 0 05:07 ? 00:00:05 ../../src/redis-server *:30002 [cluster]
root 8691 1 0 05:07 ? 00:00:05 ../../src/redis-server *:30003 [cluster]
root 8693 1 0 05:07 ? 00:00:05 ../../src/redis-server *:30004 [cluster]
root 8701 1 0 05:07 ? 00:00:05 ../../src/redis-server *:30005 [cluster]
root 8709 1 0 05:07 ? 00:00:06 ../../src/redis-server *:30006 [cluster]
root 8805 3621 0 05:47 pts/0 00:00:00 grep redis
[root@hanbing create-cluster]# kill -9 8687
[root@hanbing create-cluster]# kill -9 8689
[root@hanbing create-cluster]# kill -9 8691
[root@hanbing create-cluster]# kill -9 8693
[root@hanbing create-cluster]# kill -9 8701
[root@hanbing create-cluster]# kill -9 8709
[root@hanbing create-cluster]# ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
[root@hanbing create-cluster]# cd ../../src/
[root@hanbing src]# ./redis-cli -c
127.0.0.1:6379>
[root@hanbing src]# ./redis-cli -p 30001 -c
127.0.0.1:30001> set dd d
(error) CLUSTERDOWN Hash slot not served
127.0.0.1:30001>
[root@hanbing src]# ./redis-cli --cluster create 192.168.1.228:30001 192.168.1.228:30002 192.168.1.228:30003 192.168.1.228:30004 192.168.1.228:30005 ip:7006 --clus[root@hanbing src]# ./redis-cli --cluster create 192.168.1.228:30001 192.168.1.228:30002 192.168.1.228:30003 192.168.1.228:30004 192.168.1.228:30005 192.168.1.228:30006 --cluster-replicas 1
[ERR] Node 192.168.1.228:30001 DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
[root@hanbing src]# ps -ef | grep redis
root 8522 1 0 04:19 ? 00:00:13 ./redis-server *:6379
root 8582 7992 0 04:34 pts/3 00:00:00 vi redis.conf
root 8810 1 0 05:48 ? 00:00:00 ../../src/redis-server *:30001 [cluster]
root 8812 1 0 05:48 ? 00:00:00 ../../src/redis-server *:30002 [cluster]
root 8820 1 0 05:48 ? 00:00:00 ../../src/redis-server *:30003 [cluster]
root 8825 1 0 05:48 ? 00:00:00 ../../src/redis-server *:30004 [cluster]
root 8830 1 0 05:48 ? 00:00:00 ../../src/redis-server *:30005 [cluster]
root 8832 1 0 05:48 ? 00:00:00 ../../src/redis-server *:30006 [cluster]
root 8852 3621 0 05:52 pts/0 00:00:00 grep redis
[root@hanbing src]# kill -9 8810
[root@hanbing src]# kill -9 8812
[root@hanbing src]# kill -9 8820
[root@hanbing src]# kill -9 8825
[root@hanbing src]# kill -9 8830
[root@hanbing src]# kill -9 8832
[root@hanbing src]# cd ../utils/create-cluster/
[root@hanbing create-cluster]# vi create-cluster
[root@hanbing create-cluster]# vi create-cluster
[root@hanbing create-cluster]# ./redis-cli --cluster create 192.168.1.228:30001 192.168.1.228:30002 192.168.1.228:30003 192.168.1.228:30004 192.168.1.228:30005 192.168.1.228:30006 --cluster-replicas 1
-bash: ./redis-cli: 没有那个文件或目录
[root@hanbing create-cluster]# cd ../../src
[root@hanbing src]# ./redis-cli --cluster create 192.168.1.228:30001 192.168.1.228:30002 192.168.1.228:30003 192.168.1.228:30004 192.168.1.228:30005 192.168.1.228:30006 --cluster-replicas 1
Could not connect to Redis at 192.168.1.228:30001: Connection refused
[root@hanbing src]# cd ../utils/create-cluster/
[root@hanbing create-cluster]# ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
[root@hanbing create-cluster]# cd ../../src
[root@hanbing src]# ./redis-cli --cluster create 192.168.1.228:30001 192.168.1.228:30002 192.168.1.228:30003 192.168.1.228:30004 192.168.1.228:30005 192.168.1.228:30006 --cluster-replicas 1
>>> 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.1.228:30005 to 192.168.1.228:30001
Adding replica 192.168.1.228:30006 to 192.168.1.228:30002
Adding replica 192.168.1.228:30004 to 192.168.1.228:30003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: e1ff789aadfbf4f40d7b5a1545c8fa67a5a8c8d8 192.168.1.228:30001
slots:[0-5460] (5461 slots) master
M: 3894da8df489128dfb0dca76fe3910e1214ca746 192.168.1.228:30002
slots:[5461-10922] (5462 slots) master
M: 0924a19a43bd47934aaec2febc071d40d6a887c8 192.168.1.228:30003
slots:[10923-16383] (5461 slots) master
S: 61dd9b5dcfddec848ecd8ed9769ddce7fda0c73e 192.168.1.228:30004
replicates 0924a19a43bd47934aaec2febc071d40d6a887c8
S: 91b6ec3cb02eb797705fe41709262b91107ec693 192.168.1.228:30005
replicates e1ff789aadfbf4f40d7b5a1545c8fa67a5a8c8d8
S: 9843ededc77f4fca920255b899d286f056bdd557 192.168.1.228:30006
replicates 3894da8df489128dfb0dca76fe3910e1214ca746
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.1.228:30001)
M: e1ff789aadfbf4f40d7b5a1545c8fa67a5a8c8d8 192.168.1.228:30001
slots:[0-5460] (5461 slots) master
13830755786541760513 additional replica(s)
S: 91b6ec3cb02eb797705fe41709262b91107ec693 192.168.1.228:30005
slots: (0 slots) slave
replicates e1ff789aadfbf4f40d7b5a1545c8fa67a5a8c8d8
S: 9843ededc77f4fca920255b899d286f056bdd557 192.168.1.228:30006
slots: (0 slots) slave
replicates 3894da8df489128dfb0dca76fe3910e1214ca746
M: 3894da8df489128dfb0dca76fe3910e1214ca746 192.168.1.228:30002
slots:[5461-10922] (5462 slots) master
633457549596688385 additional replica(s)
M: 0924a19a43bd47934aaec2febc071d40d6a887c8 192.168.1.228:30003
slots:[10923-16383] (5461 slots) master
633461054290001921 additional replica(s)
S: 61dd9b5dcfddec848ecd8ed9769ddce7fda0c73e 192.168.1.228:30004
slots: (0 slots) slave
replicates 0924a19a43bd47934aaec2febc071d40d6a887c8
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@hanbing src]#