概述
一、说明
1、redis版本
redis-5.0.5
2、redis集群简介
(1)redis3.0版本之前只支持单例模式,在3.0版本及以后才支持集群,我这里用的是redis5.05版本;
(2)redis集群采用P2P模式,是完全去中心化的,不存在中心节点或者代理节点;
(3)Redis 集群会将用户数据分散保存至各个节点中,突破单机 Redis 内存最大存储容量。集群引入了 哈希槽slot的概念,其搭建完成后会生16384个哈希槽slot,同时会根据节点的数量大致均等的将16384个哈希槽映射到不同的节点上。当用户存储key-value时,集群会先对key进行CRC16校验然后对16384取模来决定key-value放置哪个槽,从而实现自动分割数据到不同的节点上。故每个Redis集群理论上最多可以有16384个节点。
(4)Redis 集群支持主从复制和故障恢复。集群使用了主从复制模型,每个主节点master
应至少有一个从节点slave
。假设某个主节点故障,其所有子节点会广播一个数据包给集群里的其他主节点来请求选票,一旦某个从节点收到了大多数主节点的回应,那么它就赢得了选举,被推选为主节点,负责处理之前旧的主节点负责的哈希槽。
3、集群规划
Redis集群至少需要3个节点,因为投票容错机制要求超过半数节点认为某个节点挂了该节点才是挂了,所以2个节点无法构成集群。 要保证集群的高可用,需要每个节点都有从节点,也就是备份节点,所以Redis集群至少需要6台服务器。这里再一台服务器上进行模拟六个节点,并将集群规划如下:
节点编号 | IP地址 | 端口号 | 节点类型 | 从节点 | 启动配置位置 |
A | 192.168.xxx.21 | 7001 | 主节点 | a | /opt/softWare/redis-cluster/redis01/redis.conf |
B | 192.168.xxx.21 | 7002 | 主节点 | b | /opt/softWare/redis-cluster/redis02/redis.conf |
C | 192.168.xxx.21 | 7003 | 主节点 | c | /opt/softWare/redis-cluster/redis03/redis.conf |
a | 192.168.xxx.21 | 8001 | 从节点 | / | /opt/softWare/redis-cluster/redis04/redis.conf |
b | 192.168.xxx.21 | 8002 | 从节点 | / | /opt/softWare/redis-cluster/redis05/redis.conf |
c | 192.168.xxx.21 | 8003 | 从节点 | / | /opt/softWare/redis-cluster/redis06/redis.conf |
二、手动搭建集群
1、创建放置配置文件的文件夹
[root@master softWare]# cd redis-cluster/
[root@master redis-cluster]# mkdir -p redis01 redis02 redis03 redis04 redis05 redis06
[root@master redis-cluster]# pwd
/opt/softWare/redis-cluster
[root@master redis-cluster]# ls
redis01
redis02
redis03
redis04
redis05
redis06
2、将配置文件拷入redis01并修改配置文件
[root@master redis-5.0.5]# cp -r redis.conf /opt/softWare/redis-cluster/redis01/
#端口号
port 7001
#开启后台运行
daemonize yes
#设置 Redis 实例 pid 文件
pidfile /var/run/redis_7001.pid
#绑定本机IP
bind 192.168.xxx.21
#设置密码
requirepass 123456
#节点内部互相访问的密码,如果节点设置了密码,这项一定要配,否则不会主从复制和宕机选举
masterauth 123456
#启用集群莫斯
cluster-enabled yes
#设置当前节点集群配置文件路径
cluster-config-file nodes-7001.conf
#设置当前节点连接超时毫秒数
cluster-node-timeout 15000
3、将此配置文件分别 复制到另外五个,并相应配置
[root@master redis01]# cp -r redis.conf ../redis02/
[root@master redis01]# cp -r redis.conf ../redis03/
[root@master redis01]# cp -r redis.conf ../redis04/
[root@master redis01]# cp -r redis.conf ../redis05/
[root@master redis01]# cp -r redis.conf ../redis06/
......
4、分别启动这六个节点
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis01/redis.conf
7511:C 31 Mar 2020 09:17:36.938 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7511:C 31 Mar 2020 09:17:36.938 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=7511, just started
7511:C 31 Mar 2020 09:17:36.938 # Configuration loaded
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis02/redis.conf
7516:C 31 Mar 2020 09:17:44.791 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7516:C 31 Mar 2020 09:17:44.791 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=7516, just started
7516:C 31 Mar 2020 09:17:44.791 # Configuration loaded
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis03/redis.conf
7521:C 31 Mar 2020 09:17:48.813 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7521:C 31 Mar 2020 09:17:48.813 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=7521, just started
7521:C 31 Mar 2020 09:17:48.813 # Configuration loaded
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis04/redis.conf
7526:C 31 Mar 2020 09:17:52.887 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7526:C 31 Mar 2020 09:17:52.887 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=7526, just started
7526:C 31 Mar 2020 09:17:52.887 # Configuration loaded
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis05/redis.conf
7531:C 31 Mar 2020 09:17:57.509 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7531:C 31 Mar 2020 09:17:57.509 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=7531, just started
7531:C 31 Mar 2020 09:17:57.509 # Configuration loaded
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis06/redis.conf
7536:C 31 Mar 2020 09:18:02.772 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7536:C 31 Mar 2020 09:18:02.772 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=7536, just started
7536:C 31 Mar 2020 09:18:02.772 # Configuration loaded
[root@master bin]# ps -ef | grep redis
root
7512
1
0 09:17 ?
00:00:00 ./redis-server 192.168.xxx.21:7001 [cluster]
root
7517
1
0 09:17 ?
00:00:00 ./redis-server 192.168.xxx.21:7002 [cluster]
root
7522
1
0 09:17 ?
00:00:00 ./redis-server 192.168.xxx.21:7003 [cluster]
root
7527
1
0 09:17 ?
00:00:00 ./redis-server 192.168.xxx.21:8001 [cluster]
root
7532
1
0 09:17 ?
00:00:00 ./redis-server 192.168.xxx.21:8002 [cluster]
root
7537
1
0 09:18 ?
00:00:00 ./redis-server 192.168.xxx.21:8003 [cluster]
root
7552
2683
0 09:18 pts/1
00:00:00 grep --color=auto redis
5、节点握手
虽然上面 6 个节点都启用了群集支持,但默认情况下它们是不相互信任或者说没有联系的。节点握手就是在各个节点之间创建链接(每个节点与其他节点相连),形成一个完整的网格,即集群。
[root@master bin]# ./redis-cli -h 192.168.xxx.21 -a 123456 -p 7001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.xxx.21:7001> DBSIZE
(integer) 0
192.168.xxx.21:7001> cluster meet 192.168.xxx.21 7002
OK
192.168.xxx.21:7001> cluster meet 192.168.xxx.21 7003
OK
192.168.xxx.21:7001> cluster meet 192.168.xxx.21 7004
OK
192.168.xxx.21:7001> cluster meet 192.168.xxx.21 8001
OK
192.168.xxx.21:7001> cluster meet 192.168.xxx.21 8002
OK
192.168.xxx.21:7001> cluster meet 192.168.xxx.21 8003
OK
192.168.xxx.21:7001> cluster nodes
e03d146c1c5a1e9a50eef9e08eb082765a7e61be 192.168.xxx.21:7001@17001 myself,master - 0 1585617741000 0 connected
00ae067dc79d43ccaf3f0d22ace47367b81b597f 192.168.xxx.21:7002@17002 master - 0 1585617741665 1 connected
bd9ae2ff064e0dcfbedb771727f5bd6751cd475a 192.168.xxx.21:8001@18001 master - 0 1585617740000 3 connected
34783fc59c05a667fb64a05a06e051e376861f65 192.168.xxx.21:7003@17003 master - 0 1585617742686 2 connected
52abf430be59ac223fa03c252989f4a6b70a0d49 192.168.xxx.21:8003@18003 master - 0 1585617741000 5 connected
058fda7786377c92ea485b91e75633fd7e3729c7 192.168.xxx.21:8002@18002 master - 0 1585617740643 4 connected
192.168.xxx.21:7002> cluster nodes
34783fc59c05a667fb64a05a06e051e376861f65 192.168.xxx.21:7003@17003 master - 0 1585617697522 2 connected
058fda7786377c92ea485b91e75633fd7e3729c7 192.168.xxx.21:8002@18002 master - 0 1585617696501 4 connected
e03d146c1c5a1e9a50eef9e08eb082765a7e61be 192.168.xxx.21:7001@17001 master - 0 1585617696000 0 connected
bd9ae2ff064e0dcfbedb771727f5bd6751cd475a 192.168.xxx.21:8001@18001 master - 0 1585617695000 3 connected
52abf430be59ac223fa03c252989f4a6b70a0d49 192.168.xxx.21:8003@18003 master - 0 1585617696000 5 connected
00ae067dc79d43ccaf3f0d22ace47367b81b597f 192.168.xxx.21:7002@17002 myself,master - 0 1585617697000 1 connected
6、分配槽位
(1)查看集群状态
192.168.xxx.21:7001> cluster info
cluster_state:fail
......
由此可知集群状态时没有上线的;
(2)分配槽位
[root@master bin]# ./redis-cli -h 192.168.xxx.21 -p 7001 -a 123456 cluster addslots {0..5461}
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
[root@master bin]# ./redis-cli -h 192.168.xxx.21 -p 7002 -a 123456 cluster addslots {5462..10922}
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
[root@master bin]# ./redis-cli -h 192.168.xxx.21 -p 7003 -a 123456 cluster addslots {10923..16383}
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
(3)槽位分配之后再查看集群状态
192.168.230.21:7001> cluster info
cluster_state:ok
......
7、主从复制配置
Redis 集群成功上线之后,要给主节点指定从节点,这样实现集群的高可用。
使用 cluster replicate 命令手动给从节点配置主节点。
集群中各个节点的node-id
可以用cluster nodes
命令查看。
192.168.xxx.21:7002> cluster nodes
34783fc59c05a667fb64a05a06e051e376861f65 192.168.xxx.21:7003@17003 master - 0 1585617697522 2 connected
058fda7786377c92ea485b91e75633fd7e3729c7 192.168.xxx.21:8002@18002 master - 0 1585617696501 4 connected
e03d146c1c5a1e9a50eef9e08eb082765a7e61be 192.168.xxx.21:7001@17001 master - 0 1585617696000 0 connected
bd9ae2ff064e0dcfbedb771727f5bd6751cd475a 192.168.xxx.21:8001@18001 master - 0 1585617695000 3 connected
52abf430be59ac223fa03c252989f4a6b70a0d49 192.168.xxx.21:8003@18003 master - 0 1585617696000 5 connected
00ae067dc79d43ccaf3f0d22ace47367b81b597f 192.168.xxx.21:7002@17002 myself,master - 0 1585617697000 1 connected
[root@master bin]# ./redis-cli -h 192.168.xxx.21 -p 8001 -a 123456 cluster replicate e03d146c1c5a1e9a50eef9e08eb082765a7e61be
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
[root@master bin]# ./redis-cli -h 192.168.xxx.21 -p 8002 -a 123456 cluster replicate 00ae067dc79d43ccaf3f0d22ace47367b81b597f
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
[root@master bin]# ./redis-cli -h 192.168.xxx.21 -p 8003 -a 123456 cluster replicate 34783fc59c05a667fb64a05a06e051e376861f65
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
执行完上面,再查看集群节点信息:
192.168.xxx.21:7002> cluster nodes
34783fc59c05a667fb64a05a06e051e376861f65 192.168.xxx.21:7003@17003 master - 0 1585618262189 2 connected 10923-16383
058fda7786377c92ea485b91e75633fd7e3729c7 192.168.xxx.21:8002@18002 slave 00ae067dc79d43ccaf3f0d22ace47367b81b597f 0 1585618261000 4 connected
e03d146c1c5a1e9a50eef9e08eb082765a7e61be 192.168.xxx.21:7001@17001 master - 0 1585618261171 0 connected 0-5461
bd9ae2ff064e0dcfbedb771727f5bd6751cd475a 192.168.xxx.21:8001@18001 slave e03d146c1c5a1e9a50eef9e08eb082765a7e61be 0 1585618260000 3 connected
52abf430be59ac223fa03c252989f4a6b70a0d49 192.168.xxx.21:8003@18003 slave 34783fc59c05a667fb64a05a06e051e376861f65 0 1585618260141 5 connected
00ae067dc79d43ccaf3f0d22ace47367b81b597f 192.168.xxx.21:7002@17002 myself,master - 0 1585618258000 1 connected 5462-10922
8、连接集群测试
注意:连接命令加-c,不然报错:(error) MOVED 6928 192.168.xxx.21:7002
[root@master bin]# ./redis-cli -p 7001 -a 123456 -h 192.168.xxx.21 -a 123456 -c
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.xxx.21:7001> set aa AA
OK
192.168.xxx.21:7001> set str cc
-> Redirected to slot [6928] located at 192.168.xxx.21:7002
OK
192.168.xxx.21:7002> get str
"cc"
192.168.xxx.21:7002>
最后
以上就是糊涂哈密瓜为你收集整理的手动搭建Redis集群【超详细】一、说明二、手动搭建集群的全部内容,希望文章能够帮你解决手动搭建Redis集群【超详细】一、说明二、手动搭建集群所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复