目录
一 案列概述
1.1 单节点Redis服务器带来的问题
●单点故障,服务不可用
●无法处理大量的并发数据请求
●数据丢失一大灾难
1.2 解决方法
●搭建Redis集群
二 案例前置知识点
2.1 Redis集群介绍
●Redis集群是一个提供在多个Redis间节点间共享数据的程序集
●Redis集群并不支持处理多个keys的命令,因为这需要在不同的节点间移动数据,从而达不到像Redis那样的性能,在高负载的情况下可能会导致不可预料的错误
●Redis集群通过分区来提供一定程度的可用性,在实际环境中当某个节点宕机或者不可达的情况下可继续处理命令
2.2 Redis集群的优势
●自动分割数据到不同的节点上
●整个集群的部分节点失败或者不可达的情况下能够继续处理命令
2.3 Redis集群的实现方法
●有客户端分片
●代理分片
●服务器端分片
2.4 Redis-Cluster数据分片
●Redis集群没有使用一致性hash,而是引入了哈希槽概念
●Redis集群有16384个哈希槽
●每个key通过CRC16校验后对16384取模来决定放置槽
●集群的每个节点负责一部分哈希槽
●以3个节点组成的集群为例
- 节点A包含0到5500号哈希槽
- 节点B包含5501到11000号哈希槽
- 节点C包含11001到16384号哈希槽
●支持添加或者删除节点
●添加删除节点无需停止服务
例如:
●如果想新添加个节点D,需要移动节点A,B,C中的部分槽到D上
●如果想移除节点A,需要将A中的槽移到B和C节点上,再将没有任何槽的A节点从集群中移除
2.5 Redis-Cluster的主从复制模型
●集群中具有A,B,C三个节点,如果节点B失败了,整个集群就会因缺少5501-11000这个范围的槽而不可用
●为每个节点添加一个从节点A1,B1,C1,整个群集便有三个master节点和三个slave节点组成,在节点B失败后,集群便会选举B1为新的主节点继续服务
●当B和B1都失败后,集群将不可用
三 实验步骤
3.1 实验环境
服务器 | IP地址 |
---|---|
master | 20.0.0.101 |
master | 20.0.0.102 |
master | 20.0.0.103 |
salve | 20.0.0.104 |
salve | 20.0.0.105 |
salve | 20.0.0.106 |
3.2 实验步骤
################所有节点################
[root@localhost ~]# vim /etc/redis/6379.conf
70 # bind 127.0.0.1 20.0.0.101 //注释掉,默认监听所有网卡
89 protected-mode no //关闭保护模式
93 port 6379
137 daemonize yes //以独立进程启动
833 cluster-enabled yes //开启群集功能
841 cluster-config-file nodes-6379.conf //群集名称文件设置
847 cluster-node-timeout 15000 //群集超时时间设置
700 appendonly yes //开启aof持久化
[root@localhost utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
[root@localhost utils]# cd /var/lib/redis/6379/ //正常启动后,目录下会多出两个文件
[root@localhost 6379]# ll
总用量 8
-rw-r--r-- 1 root root 0 11月 4 15:45 appendonly.aof //aof持久化文件
-rw-r--r-- 1 root root 92 11月 4 15:45 dump.rdb
-rw-r--r-- 1 root root 114 11月 4 15:45 nodes-6379.conf //节点首次启动生成的配置文件
#############仅在master上操作##################
[root@localhost 6379]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 //导入key文件
[root@localhost 6379]# cd /opt/ //提前把复制的脚本传进来
[root@localhost opt]# ll
总用量 1964
drwxrwxr-x 6 root root 334 11月 20 2019 redis-5.0.7
-rw-r--r-- 1 root root 1984203 11月 3 17:26 redis-5.0.7.tar.gz
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
-rw-r--r-- 1 root root 24535 11月 4 15:10 rvm-installer.sh
[root@localhost opt]# chmod +x rvm-installer.sh
[root@localhost opt]# ./rvm-installer.sh
(下载速度非常慢)
我们直接把下好的包源传进/opt目录中
[root@localhost profile.d]# cd /opt/
[root@localhost opt]# ll
总用量 3288
drwxrwxr-x 6 root root 334 11月 20 2019 redis-5.0.7
-rw-r--r-- 1 root root 1984203 11月 3 17:26 redis-5.0.7.tar.gz
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
-rwxr-xr-x 1 root root 24535 11月 4 15:10 rvm-installer.sh
drwxrwxr-x 20 root root 4096 10月 29 18:25 rvm-master
-rw-r--r-- 1 root root 1351586 11月 4 16:29 rvm-master.tar.gz
[root@localhost opt]# tar zxvf rvm-master.tar.gz
[root@localhost opt]# cd rvm-master/
[root@localhost rvm-master]# ./install
[root@localhost opt]# source /etc/profile.d/rvm.sh //执行环境变量
[root@localhost opt]# rvm list known //列出Ruby可安装的版本
[root@localhost opt]# rvm install 2.4.1 //安装Ruby2.4.1版本
[root@localhost opt]# rvm use 2.4.1 //使用Ruby2.4.1版本
Using /usr/local/rvm/gems/ruby-2.4.1
[root@localhost opt]# ruby -v //查看当前Ruby2.4.1版本
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
[root@localhost opt]# gem install redis //再次安装redis
[root@localhost opt]# redis-cli --cluster create 20.0.0.101:6379 20.0.0.102:6379 20.0.0.103:6379 20.0.0.104:6379 20.0.0.105:6379 20.0.0.106:6379 --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 20.0.0.105:6379 to 20.0.0.101:6379
Adding replica 20.0.0.106:6379 to 20.0.0.102:6379
Adding replica 20.0.0.104:6379 to 20.0.0.103:6379
M: 1984fd3b2f1da1aec077acbe5a7e0a20e6f6f5cc 20.0.0.101:6379
slots:[0-5460] (5461 slots) master
M: 0fab0b65a5ce2f9fba14fb2f60ddace7040d5225 20.0.0.102:6379
slots:[5461-10922] (5462 slots) master
M: e26af716aac290e645ac67cfba0dac882ee3824e 20.0.0.103:6379
slots:[10923-16383] (5461 slots) master
S: 60d823ddc62a23cec69004466273631ffc492e85 20.0.0.104:6379
replicates e26af716aac290e645ac67cfba0dac882ee3824e
S: 366dfc6158823e0e26a4e74c396db4a60d4da352 20.0.0.105:6379
replicates 1984fd3b2f1da1aec077acbe5a7e0a20e6f6f5cc
S: b46445ea1e8e030f7247884b3d68c110396f9fd9 20.0.0.106:6379
replicates 0fab0b65a5ce2f9fba14fb2f60ddace7040d5225
Can I set the above configuration? (type 'yes' to accept): yes //输入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 20.0.0.101:6379)
M: 1984fd3b2f1da1aec077acbe5a7e0a20e6f6f5cc 20.0.0.101:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: e26af716aac290e645ac67cfba0dac882ee3824e 20.0.0.103:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: b46445ea1e8e030f7247884b3d68c110396f9fd9 20.0.0.106:6379
slots: (0 slots) slave
replicates 0fab0b65a5ce2f9fba14fb2f60ddace7040d5225
S: 366dfc6158823e0e26a4e74c396db4a60d4da352 20.0.0.105:6379
slots: (0 slots) slave
replicates 1984fd3b2f1da1aec077acbe5a7e0a20e6f6f5cc
S: 60d823ddc62a23cec69004466273631ffc492e85 20.0.0.104:6379
slots: (0 slots) slave
replicates e26af716aac290e645ac67cfba0dac882ee3824e
M: 0fab0b65a5ce2f9fba14fb2f60ddace7040d5225 20.0.0.102:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost ~]# redis-cli -c -h 20.0.0.101 //登录redis
20.0.0.101:6379> set name zhangsan
-> Redirected to slot [5798] located at 20.0.0.102:6379
OK
20.0.0.102:6379> keys *
1) "name"
20.0.0.101:6379> get name
-> Redirected to slot [5798] located at 20.0.0.102:6379
"zhangsan"
20.0.0.102:6379> exit
[root@localhost ~]# redis-cli -c -h 20.0.0.102
20.0.0.102:6379> get name
"zhangsan"
20.0.0.102:6379> exit
[root@localhost ~]# redis-cli -c -h 20.0.0.103
20.0.0.103:6379> get name
-> Redirected to slot [5798] located at 20.0.0.102:6379
"zhangsan"
20.0.0.102:6379> exit
[root@localhost ~]# redis-cli -c -h 20.0.0.104
20.0.0.104:6379> get name
-> Redirected to slot [5798] located at 20.0.0.102:6379
"zhangsan"
20.0.0.102:6379> exit
[root@localhost ~]# redis-cli -c -h 20.0.0.105
20.0.0.105:6379> get name
-> Redirected to slot [5798] located at 20.0.0.102:6379
"zhangsan"
20.0.0.102:6379> exit
[root@localhost ~]# redis-cli -c -h 20.0.0.106
20.0.0.106:6379> get name
-> Redirected to slot [5798] located at 20.0.0.102:6379
"zhangsan"
//说明实现集群功能
3.2 删除集群
1. 删除进程
[root@localhost 6379]# ps -ef | grep redis
[root@localhost 6379]# pkill -9 redis
2. 删除文件
[root@localhost ~]# cd /var/lib/redis/6379/
[root@localhost 6379]# ll
总用量 8
-rw-r--r-- 1 root root 0 11月 4 19:16 appendonly.aof
-rw-r--r-- 1 root root 92 11月 5 08:36 dump.rdb
-rw-r--r-- 1 root root 131 11月 4 19:16 nodes-6379.conf
[root@localhost 6379]# rm -rf *
3. 重启redis
[root@localhost 6379]# /etc/init.d/redis_6379 restart
Stopping ...
Could not connect to Redis at 127.0.0.1:6379: Connection refused
Redis stopped
/var/run/redis_6379.pid exists, process is already running or crashed
[root@localhost 6379]# cd /var/run/
[root@localhost run]# rm -rf redis_6379.pid
[root@localhost run]# /etc/init.d/redis_6379 restart
//然后就可以重新创建集群了