一.简介
- 为什么要使用集群
为了在大流量访问下保证业务的稳定,集群化是存储的必然形态
为了保证在某个服务器崩溃后,仍然能够提供稳定的服务
单个服务器容易因为单机内存,并发和流量等问题
未来的发展趋势肯定是云计算和大数据的紧密结合
只有分布式架构才能满足需求
- redis-cluster描述
redis-cluster采用无中心化结构,每个节点保存数据和整个集群状态,每个节点和其他所有节点连接,连接任意一个节点,即可连接到集群
在redis3.0之后,就支持redis-cluster集群,至少需要3Master+3Slave才能建立集群
是主仆模式和哨兵模式的结合,优于这两个模式
- 特点
所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和宽带
节点的fail是通过集群中超过半数的节点检测失效时才会生效
客户端与redis节点直连,不需要中间proxy层,客户端不需要连接集群中的所有节点,连接其中一个即可
redis-cluster把所有的物理节点映射到[0-1638]slot上,不一定平均分配,有cluster负责维护
redis集群会预先分好16384个哈希槽,当需要在redis集群中放置一个key-value时,redis会对key使用crc16算法算出一个结果,然后把结果对16384求余数,这样key就会对应一个哈希槽,redis会根据节点数量大致均匀的将哈希槽映射到不同的节点中
- redis-cluster的容错
master的不可用
使用投票机制,投票过程是集群中的所有Master参与的,如果有半数以上的Master与检测的Master节点连接超时,就会认为该Master节点不可用,就会让该Master的Slave代替
整个集群的不可用
当集群中任意Master不可用,且当前Master没有可用的Slave,整个集群进入fail状态,这时哈希槽的映射也不完整了
二.redis-cluster集群的搭建
redis3.X和redis4.X的版本还需要下载redis-trib.rb工具,这里安装的redis版本为5.0.7
- 创建文件夹redis-cluster
mkdir redis-cluster
- 在文件夹中创建六个文件夹,用于存放配置文件
cd redis-cluster
mkdir 7000 7001 7002 7003 7004 7005
如果是想搭建在不同服务器上的话,这六个文件夹就应该创建到不同服务器上
- 拷贝redis.conf
将安装redis时,压缩包解压的文件夹(redis-5.0.7)中的redis.conf复制到我们刚刚创建的文件夹中,每个文件夹都要一个配置
cp redis.conf /usr/local/redis-cluster/7000
- 修改redis.conf
需要修改每个文件夹下的redis.conf
#关闭保护模式 用于公网访问
protected-mode no
port 7000
#开启集群模式
#需要删掉#,解开注释
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 5000
#后台启动
daemonize yes
pidfile /var.run/redis_7000.pid
logfile "7000.log"
#需要注释
#dir /redis/data
#将网络绑定注释,外网就可以访问redis了
#bind 127.0.0.1
#用于连接主节点的密码
masterauth 123456
#设置redis密码,各节点密码最好保持一致
requirepass 123456
可以通过/名称来查找属性,修改完7000文件夹下的redis.conf后,复制到其他文件夹中,使用:%s/7000/7001来实现快速修改其他redis.conf
- 复制src/redis-server
要加-r,将压缩包解压的文件(redis-5.0.7)中的src复制到redis-cluster
cp -r src /usr/local/redis-cluster
到这里,redis-cluster集群的配置和启动项就已经配置完了
三.redis-cluster集群的启动
- 启动各个节点
/src/redis-server ./7000/redis.conf
./src/redis-server ./7001/redis.conf
./src/redis-server ./7002/redis.conf
./src/redis-server ./7003/redis.conf
./src/redis-server ./7004/redis.conf
./src/redis-server ./7005/redis.conf
- 查看节点是否启动
ps -ef | grep -i redis
root 75240 1 0 15:34 ? 00:00:00 ./src/redis-server *:7000 [cluster]
root 75253 1 0 15:36 ? 00:00:00 ./src/redis-server *:7001 [cluster]
root 75258 1 0 15:36 ? 00:00:00 ./src/redis-server *:7002 [cluster]
root 75271 1 0 15:36 ? 00:00:00 ./src/redis-server *:7003 [cluster]
root 75276 1 0 15:36 ? 00:00:00 ./src/redis-server *:7004 [cluster]
root 75282 1 0 15:36 ? 00:00:00 ./src/redis-server *:7005 [cluster]
- 创建redis-cluster集群
./src/redis-cli --cluster create -a 123456 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1
如果是不同服务器的话,修改ip地址和端口即可
出现这条语句,填yes即可,这是询问是否接受它的配置
Can I set the above configuration? (type 'yes' to accept): yes
出现以下代码,就是redis-cluster集群已经创建完成并启动了
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
四.redis-cluster集群的连接
- 连接上redis-cluster集群中的某个节点
仍然需要借助src/redis-cli客户端来连接
./src/redis-cli -h 127.0.0.1 -c -p 7000 -a 123456
- 查看集群状态
可以通过这两条命令来查看集群的状态cluster nodes/info replication
**cluster nodes**
d1826fd298ef94e6fe7c71d1c4792dfbb1c1496d 127.0.0.1:7001@17001 master - 0 1582963346000 2 connected 5461-10922
23125cf80e6bd8a2eacce63f333524d83244cb50 127.0.0.1:7000@17000 myself,master - 0 1582963346000 1 connected 0-5460
ac2f2a46fe10651091b73cfd4eb10d2de33c7f82 127.0.0.1:7003@17003 slave d1826fd298ef94e6fe7c71d1c4792dfbb1c1496d 0 1582963346000 4 connected
3265277ca8991eb3b77573d7d959e8cf432d85d1 127.0.0.1:7002@17002 master - 0 1582963347312 3 connected 10923-16383
a17a19a0d51db9ff7c35adf9515284ed0b831964 127.0.0.1:7004@17004 slave 3265277ca8991eb3b77573d7d959e8cf432d85d1 0 1582963346301 5 connected
781bf8a0adfbf007a6b676ec48a59ddde611b766 127.0.0.1:7005@17005 slave 23125cf80e6bd8a2eacce63f333524d83244cb50 0 1582963346000 6 connected
**info replication**
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=7005,state=online,offset=1023,lag=1
master_replid:6565417229c062bb60e6e5c576a97bc12c5270c8
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1023
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1023
五.redis-cluster集群的关闭
- 创建一个shutdown.sh文件
vim shutdown.sh
需要注意这里的src/redis-cli的路径,一定在该文件能够读取到
src/redis-cli -c -h 127.0.0.1 -p 7000 -a 123456 shutdown
src/redis-cli -c -h 127.0.0.1 -p 7001 -a 123456 shutdown
src/redis-cli -c -h 127.0.0.1 -p 7002 -a 123456 shutdown
src/redis-cli -c -h 127.0.0.1 -p 7003 -a 123456 shutdown
src/redis-cli -c -h 127.0.0.1 -p 7004 -a 123456 shutdown
src/redis-cli -c -h 127.0.0.1 -p 7005 -a 123456 shutdown
- 修改shutdown.sh权限
chmod u+x shutdown.sh
- 执行shutdown.sh关闭集群
./shutdown.sh
[root@localhost redis-cluster]# ./shutdown.sh
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
启动也可以参考关闭这种形式