前面讲述了有关redis的编译安装流程以及其配置优化的部分内容,本文旨在本地模拟redis服务器集群搭建的流程演示。
1)存在单点故障;
2)不满足高并发的需求;
3)数据丢失引发灾难(容错率非常低);
…
Redis集群没有使用一致性hash,而是引入了哈希槽的概念。
Redis集群总共有16384个哈希槽(0-16383)
每个Key通过CRC16校验后对16384取模来决定如何进行存放,
集群的每个节点负责一部分的hash槽。
在Redis集群中,支持添加或者删除节点,且无需停止服务。
Redis-Cluster数据分片
以3个节点组成的集群为例
节点A包含О到5500号哈希槽
节点B包含5501到11000号哈希槽
节点C包含11001到16384号哈希槽
支持添加或者删除节点
添加删除节点无需停止服务
例如
如果想新添加个节点D,需要移动节点A,B,C中的部分槽到D上
如果想移除节点A,需要将A中的槽移到B和C节点上,再将没有任何槽的A节点从集群中移除
设置网络参数、关闭防火墙和selinux(所有节点)
下载并安装Redis(所有节点)
修改Redis配置文件(所有节点)
创建Redis集群(master1节点)
导入key文件并安装rvm
执行环境变量让其生效
安装Ruby2.4.1版本
安装redis客户端
创建redis集群
调整Redis集群主从关系
删除所有的从节点(master1节点)
删除节点配置文件和持久化文件(slave1、2、3节点)
重新添加从节点(master1节点)
检查新的主从关系(master1节点)
测试集群数据读写
类别 | IP地址 | 系统 | 软件包 |
---|---|---|---|
master | ens33:192.168.100.140; ens36:192.168.100.128; ens37:192.168.100.130 | centos7 | redis-5.0.7.tar.gz; rvm-1.29.9.tar.gz |
slave | ens33:192.168.100.150; ens36:192.168.100.129; ens37:192.168.100.131 | centos7 | redis-5.0.7.tar.gz |
安装Redis
1.下载redis的安装包
[root@localhost ~]# mount.cfs //192.168.1.150/qq-Download /mnt
[root@localhost ~]# cd /mnt
2.通过yum工具,安装编译工具
[root@localhost ~]# yum install gcc gcc-c++ make -y
3.解压redis软件包
tar xzvf redis-5.0.7.tar.gz -C /opt
4.编译且安装
cd /opt/redis-5.0.7/
make
make PREFIX=/usr/local/redis install
5.开启服务,设置各类文件
cd /opt/redis-5.0.7/utils/
[root@localhost utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] '定义主配置文件'
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] "定义日志文件"
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] "数据文件"
Selected default - /var/lib/redis/6379
Please select the redis executable path [] /usr/local/redis/bin/redis-server "可执行文件路径,需要自行定义"
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/redis/bin/redis-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
[root@localhost redis]# netstat -natp | grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 6475/redis-server 1
6.创建软链接,便于服务控制
ln -s /usr/local/redis/bin/* /usr/local/bin/
7.进入redis数据库
[root@localhost init.d]# vim /etc/redis/6379.conf
bind 127.0.0.1 192.168.43.101 "//绑定本机地址"
[root@localhost init.d]# service redis_6379 restart "//重启服务"
Stopping ...
Redis stopped
Starting Redis server...
[root@localhost ~]# redis-cli -h 192.168.100.140 -p 6379 "//-h指定地址,-p指定端口"
192.168.100.140:6379>
redis的配置文件修改
[root@localhost ~]# cd /etc/redis/
[root@localhost redis]# ls
6379.conf
[root@localhost redis]# vim 6379.conf
#bind 127.0.0.1 "//注释第70行的监听127地址,表示监听所有地址,也可以写三个节点的位置"
protected-mode no "//去掉第89行注释,关闭安全保护"
port 6379 "//去掉第93行注释,开启端口6379"
daemonize yes "//去掉第137行注释,以独立进程启动"
appendonly yes "//去掉第700行注释,开启aof持久化功能"
cluster-enabled yes "//去掉第833行注释,开启群集功能"
cluster-config-file node-6379.conf "//去掉第841行注释,群集名称文件设置"
cluster-node-timeout 15000 "//去掉第847行注释,群集超时时间设置"
[root@localhost ~]# service redis_6379 restart "//重启服务"
[root@localhost ~]# ls /var/lib/redis/6379/ "//生成下列三个文件说明设置成功"
appendonly.aof dump.rdb nodes-6379.conf
[root@localhost ~]#
安装rvm、ruby等控制群集软件包
先安装rvm
[root@master1 6379]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 '//导入key文件,若出现error,重新导入一次即可'
[root@master1 6379]# curl -sSL https://get.rvm.io | bash -s stable '//安装rvm'
注:执行 gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3第一次时会出现超时错误,可继续
执行第二次执行curl -sSL https://get.rvm.io | bash -s stable时可能会出现curl: (7) Failed connect to get.rvm.io:443; 拒绝连接错误,要,把下载好的文件rvm-install.text重命名为rvm-install.sh
可以关闭虚拟机防火墙,安全性功能,也有也可能时网络比较差,此外也可以把https换成http试试
本实验把已经下载好的脚本直接拿过来使用
继续安装ruby
[root@localhost ~ ]# cd /opt
[root@localhost opt ]# ls
rvm-install.sh
[root@localhost opt]# chmod +x rvm-install.sh
[root@localhost opt]# ./rvm-install.sh
[root@localhost opt]# source /etc/profile.d/rvm.sh '//执行环境变量'
[root@localhost opt]# rvm list known '//列出ruby可以安装的版本'
[root@localhost rvm-master]# rvm install 2.4.1 '//安装ruby2.4.1版本,安装版本的事件会比较长,移动热点安装速度快'
[root@localhost opt]# rvm use 2.4.1
[root@localhost opt]# ruby -v '//查看当前ruby版本'
[root@localhost opt]# gem install redis '//再次安装redis'
在主服务器上添加两张网卡(NAT模式)
[root@localhost 6379]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.140 netmask 255.255.255.0 broadcast 192.168.100.255
ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.128 netmask 255.255.255.0 broadcast 192.168.100.255
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.130 netmask 255.255.255.0 broadcast 192.168.100.255
....
关闭防火墙,SElinux功能
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]#
创建群集(其他节点都安装Redis,按下文设置完slave,监听端口都存在,再到master创建群集)
[root@localhost 6379]# redis-cli --cluster create 192.168.100.140:6379 192.168.100.128:6379 192.168.100.130:6379 192.168.100.150:6379 192.168.100.129:6379 192.168.100.131: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 192.168.100.129:6379 to 192.168.100.140:6379 '//一主一从的绑定关系是随机的;140主对应从129'
Adding replica 192.168.100.131:6379 to 192.168.100.128:6379
Adding replica 192.168.100.150:6379 to 192.168.100.130:6379
M: ef8534eb9a9a9029220c08a8b4fc4a5b3fb591bb 192.168.100.140:6379
slots:[0-5460],[10923-16383] (5461 slots) master
M: ef8534eb9a9a9029220c08a8b4fc4a5b3fb591bb 192.168.100.128:6379
slots:[0-16383] (5462 slots) master
M: ef8534eb9a9a9029220c08a8b4fc4a5b3fb591bb 192.168.100.130:6379
slots:[0-5460],[10923-16383] (5461 slots) master
S: 637bebedda821add8127dc8355d2722cf61b73b1 192.168.100.150:6379
replicates ef8534eb9a9a9029220c08a8b4fc4a5b3fb591bb
S: 637bebedda821add8127dc8355d2722cf61b73b1 192.168.100.129:6379
replicates ef8534eb9a9a9029220c08a8b4fc4a5b3fb591bb
S: 637bebedda821add8127dc8355d2722cf61b73b1 192.168.100.131:6379
replicates ef8534eb9a9a9029220c08a8b4fc4a5b3fb591bb
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.100.140:6379)
M: ef8534eb9a9a9029220c08a8b4fc4a5b3fb591bb 192.168.100.140:6379
slots:[0-5460],[10923-16383] (10922 slots) master
M: 637bebedda821add8127dc8355d2722cf61b73b1 192.168.100.150:6379
slots:[5461-10922] (5462 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost 6379]#
'六个实例分为三组,每组一主一从,--replicas 1表示每组一个从,下面交互的时候需要输入yes才可以创建。'
'使用源码解压目录中的redis-trib.rb工具创建群集。'
生产环境rvm和ruby只在一个节点安装,搭建群集
其他节点只安装Redis开放数据库端口即可
安装redis
1.下载redis的安装包
[root@localhost ~]# mount.cfs //192.168.1.150/qq-Download /mnt
[root@localhost ~]# cd /mnt
2.通过yum工具,安装编译工具
[root@localhost ~]# yum install gcc gcc-c++ make -y
3.解压redis软件包
tar xzvf redis-5.0.7.tar.gz -C /opt
4.编译且安装
cd /opt/redis-5.0.7/
make
make PREFIX=/usr/local/redis install
5.开启服务,设置各类文件
cd /opt/redis-5.0.7/utils/
[root@localhost utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] '//定义主配置文件'
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] '//定义日志文件'
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]'数据文件'
Selected default - /var/lib/redis/6379
Please select the redis executable path [] /usr/local/redis/bin/redis-server '//可执行文件路径,需要自行定义'
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/redis/bin/redis-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
[root@localhost redis]# netstat -natp | grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 6475/redis-server 1
6.创建软链接,便于服务控制
ln -s /usr/local/redis/bin/* /usr/local/bin/
7.进入redis数据库
[root@localhost init.d]# vim /etc/redis/6379.conf
bind 127.0.0.1 //绑定本机地址
[root@localhost init.d]# service redis_6379 restart '//重启服务'
Stopping ...
Redis stopped
Starting Redis server...
[root@localhost ~]# redis-cli -h 192.168.100.150 -p 6379 '//-h指定地址,-p指定端口'
192.168.100.150:6379>
修改redis的配置文件
8.启用群集
#修改redis的配置文件
[root@localhost ~]# cd /etc/redis/
[root@localhost redis]# ls
6379.conf
[root@localhost redis]# vim 6379.conf
#bind 127.0.0.1 //注释第70行的监听127地址,表示监听所有地址
protected-mode no //去掉第89行注释,关闭安全保护
port 6379 //去掉第93行注释,开启端口6379
daemonize yes //去掉第137行注释,以独立进程启动
appendonly yes //去掉第700行注释,开启aof持久化功能
cluster-enabled yes //去掉第833行注释,开启群集功能
cluster-config-file node-6379.conf //去掉第841行注释,群集名称文件设置
cluster-node-timeout 15000 //去掉第847行注释,群集超时时间设置
[root@localhost ~]# service redis_6379 restart //重启服务
[root@localhost ~]# ls /var/lib/redis/6379/ //生成下列三个文件说明设置成功
appendonly.aof dump.rdb nodes-6379.conf "'//生成了三个文件,appendonly.aof是AOF持久化文件,dump.rdb是RDB快照文件,nodes-6379.conf是节点首次启动生成的配置文件'"
[root@localhost ~]#
添加两张网卡NAT模式
----添加两张网卡,NAT模式
[root@slave ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.150 netmask 255.255.255.0 broadcast 192.168.100.255
ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.129 netmask 255.255.255.0 broadcast 192.168.100.255
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.131 netmask 255.255.255.0 broadcast 192.168.100.255
[root@localhost ~]#
#关闭防火墙,SElinux
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]#
验证主从数据同步
#验证主从数据同步 "-c 指定登陆群集"
"创建键值只能在master"
[root@localhost ~]# redis-cli -c -h 192.168.100.140 '//登陆到140服务器数据库'
192.168.100.140:6379> keys *
(empty list or set)
192.168.100.140:6379> set user lisi '//创建一个键值'
OK
192.168.100.140:6379> keys * '//查看所有键'
1) "user"
192.168.100.140:6379> get user '//查看user键的值'
"lisi"
192.168.100.140:6379> exit '//退出'
[root@localhost ~]# redis-cli -c -h 192.168.43.247 '//登陆到150服务器数据库'
192.168.100.150:6379> keys *
1) "user"
192.168.100.150:6379> get user '查看user键的值'
(error) MOVED 5474 192.168.100.129:6379 '//提示移动到129节点上'
192.168.100.150:6379> exit
[root@localhost ~]# redis-cli -c 192.168.100.129 '//登陆到129服务器的数据库'
192.168.100.129:6379> keys *
1) "user"
192.168.100.129:6379> get user
"lisi"
192.168.100.129:6379> exit
"主master存储数据,只有与它对应的salve节点才有同步的数据;其他节点查看数据的时候是要到这两个节点取找"