一 redis概述
Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSIC语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。从2010年3月15日起,Redis的开发工作由VMware主持。从2013年5月开始,Redis的开发由Pivotal赞助。`
redis集群技术:
redis2.x使用客户端分片技术
redis3.x使用cluster集群技术
二 安装与启动
2.1 下载地址
redis下载地址:http://download.redis.io/releases/
ruby下载地址:http://www.ruby-lang.org/en/news/2018/03/28/ruby-2-5-1-released/
gem下载地址:https://rubygems.org/gems/redis/versions/3.2.2
2.2 环境搭建
redis-cluster搭建(redis-3.2.6)
os:centos7
ip:192.168.1.1 192.168.1.2
redis:3.2.6
gem-redis:3.2.2
ruby:2.5.1
2.3 安装redis包
2.3.1 下载redis-3.2.6.tar.gz
https://redis.io/download
https://download.redis.io/releases/
2.3.2 上传服务器192.168.1.1 192.168.1.2该目录下/laity/home/redis/zt
rz上传
2.3.3 解压
cd /laity/home/redis/zt
tar -zxvf redis-3.2.6.tar.gz
cd /laity/home/redis/zt/redis-3.2.6/
make install PREFIX=/laity/home/redis/cluster --安装路径
2.3.4 创建对应文件夹
cd /laity/home/redis/cluster/
mkdir 7000 7001 7002
cd 7001
mkdir pid data logs
2.3.5 拷贝修改配置文件
cp -a /laity/home/redis/zt/redis-3.2.6/redis.conf
/laity/home/redis/cluster/7000/redis_7000.conf
vi redis_7000.conf
bind 192.168.1.1 --绑定ip
port 7000 --端口号
daemonize yes --开启守护进程
cluster-enabled yes --启用集群功能
cluster-config-file nodes.conf --集群配置文件名字
cluster-node-timeout 15000 --请求超时 默认15秒,可自行设置
dir /laity/home/redis/cluster/7000/data/ --集群的工作目录
logfile "/laity/home/redis/cluster/7000/logs/redis_7000.log" --日志文件
pidfile /laity/home/redis/cluster/7000/pid/redis_7000.pid --pid文件
requirepass abc
masterauth abc --登录密码,根据实际情况添加,如果程序中需要密码,配置文件中加上,如果不需要,这里不需要配置
拷贝redis_7000.conf至7001和7002,并修改对应的端口号
192.168.1.2 主机按照上面2.3.3-2.3.5步骤再进行一遍
2.4 安装ruby
2.4.1 安装ruby的目的
因为在redis3.2.6/src中有一个文件叫做redis-trib.rb,这个文件就是作者用Ruby写的,用来搭建redis集群(redis3.0版本时才开始支持集群),redis-trib.rb的后缀就是Ruby的简写,所以想要搭建redis集群需要有一个能执行.rb这种文件的运行环境,这个环境就是Ruby。
2.4.2 下载ruby-2.5.1.tar.gz,注意安装ruby需要root用户权限
http://www.ruby-lang.org/en/news/2018/03/28/ruby-2-5-1-released/
cd /root
mkdir ruby
rz ruby-2.5.1.tar.gz
tar -zxvf ruby-2.5.1.tar.gz
cd /root/ruby/ruby-2.5.1
./configure –-prefix=/laity/home/ruby
make && make install
需要配置环境变量:pathmunge /laity/home/ruby/bin
2.5 安装gem
下载安装redis-3.2.2.gem
https://rubygems.org/gems/redis/versions/3.2.2
cd /export/home/ruby
rz redis-3.2.2.gem
gem install -l ./redis-3.2.2.gem
note:版本查看
ruby -v
gem -v
2.6 集群管理工具
redis-trib.rb是redis官方推出的管理redis集群的工具。
cp -a /export/home/redis/zt/redis-3.2.6/src/redis-trib.rb /export/home/redis/cluster/bin/redis-trib.rb
2.7 启动6个redis实例
192.168.1.1 和192.168.1.2
--快速启动
for ((i=0;i<=2;i++)); do /laity/home/redis/cluster/bin/redis-server /laity/home/redis/cluster/700$i/redis_700$i.conf; done
--快速关闭
for((i=0;i<=2;i++)); do /laity/home/redis/cluster/bin/redis-cli -c -h 192.168.1.1 -p 700$i shutdown; done
for((i=0;i<=2;i++)); do /laity/home/redis/cluster/bin/redis-cli -c -h 192.168.1.2 -p 700$i shutdown; done
--一个一个启动
nohup /laity/home/redis/cluster/bin/redis-server /laity/home/redis/cluster/7000/redis_7000.conf >/dev/null &
sleep 1
nohup /laity/home/redis/cluster/bin/redis-server /laity/home/redis/cluster/7001/redis_7001.conf >/dev/null &
sleep 1
nohup /laity/home/redis/cluster/bin/redis-server /laity/home/redis/cluster/7002/redis_7002.conf >/dev/null &
2.8 创建集群
登录192.168.1.1
/laity/home/redis/cluster/bin/redis-trib.rb create --replicas 1 192.168.1.1:7000 192.168.1.1:7001 192.168.1.1:7002 192.168.1.2:7000 192.168.1.2:7001 192.168.1.2:7002
--replicas 1:为集群中的每个主节点创建一个从节点
集群正常工作至少需要3个主节点:以上是三主三从
集群创建成功:
[redis@jdmobile-2021-b963d1e2 bin]$ ./redis-trib.rb create --replicas 1 192.168.1.1:7000 192.168.1.1:7001 192.168.1.1:7002 192.168.1.2:7000 192.168.1.2:7001 192.168.1.2:7002
>>> Creating cluster
/laity/home/ruby/lib/ruby/gems/2.5.0/gems/redis-3.2.2/lib/redis/client.rb:441: warning: constant ::Fixnum is deprecated
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.1:7000
192.168.1.1:7000
192.168.1.1:7001
Adding replica 192.168.1.2:7001 to 192.168.1.1:7000
Adding replica 192.168.1.1:7002 to 192.168.1.2:7000
Adding replica 192.168.1.2:7002 to 192.168.1.1:7001
M: 6aa2ad7ef5037f1cd0aa99cae8bc4d499502a780 192.168.1.1:7000
slots:0-5460 (5461 slots) master
M: de6402e1a48483a42dc38a48d4cd30ffc3ac6b20 192.168.1.1:7001
slots:10923-16383 (5461 slots) master
S: e8898127e29a07902742cd6c45c31c5ee44d0c6b 192.168.1.1:7002
replicates d93ec459567e394c501fc020770277059763996d
M: d93ec459567e394c501fc020770277059763996d 192.168.1.2:7000
slots:5461-10922 (5462 slots) master
S: 24ecff1ba09b4a6580276749dc584c1166c91356 192.168.1.2:7001
replicates 6aa2ad7ef5037f1cd0aa99cae8bc4d499502a780
S: 75f0dd30328e4b0f95f0b3dfb3daaf8519cc9c15 192.168.1.2:7002
replicates de6402e1a48483a42dc38a48d4cd30ffc3ac6b20
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.1:7000)
M: 6aa2ad7ef5037f1cd0aa99cae8bc4d499502a780 192.168.1.1:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 75f0dd30328e4b0f95f0b3dfb3daaf8519cc9c15 192.168.1.2:7002
slots: (0 slots) slave
replicates de6402e1a48483a42dc38a48d4cd30ffc3ac6b20
S: e8898127e29a07902742cd6c45c31c5ee44d0c6b 192.168.1.1:7002
slots: (0 slots) slave
replicates d93ec459567e394c501fc020770277059763996d
M: de6402e1a48483a42dc38a48d4cd30ffc3ac6b20 192.168.1.1:7001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: d93ec459567e394c501fc020770277059763996d 192.168.1.2:7000
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 24ecff1ba09b4a6580276749dc584c1166c91356 192.168.1.2:7001
slots: (0 slots) slave
replicates 6aa2ad7ef5037f1cd0aa99cae8bc4d499502a780
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
2.9 测试
可以先在192.168.1.1机器redis 7000 的节点set一个key
[app@bl-redis01 ~]$ /laity/home/redis/cluster/bin/redis-cli -h 192.168.1.1 -c -p 7000
192.168.1.1:7000> set name www.ymq.io
-> Redirected to slot [5798] located at 192.168.1.2:7003
OK
192.168.1.2:7003> get name
"www.ymq.io"
192.168.1.2:7003>
由上面信息可发现redis set name 之后重定向到192.168.1.2机器 redis 7003 这个节点
然后在192.168.1.1机器redis 7001 的节点get一个key
[app@bl-redis03 ~]$ /laity/home/redis/cluster/bin/redis-cli -h 192.168.1.1 -c -p 7001
192.168.1.1:7001> get name
-> Redirected to slot [5798] located at 192.168.1.2:7003
"www.ymq.io"
192.168.1.2:7003>
发现redis get name 重定向到192.168.1.2机器 redis 7003 这个节点.
如果看到这样的现象,说明redis cluster集群已经是可用的了!!!!!!
2.10 集群查看
1、cluster nodes:列出集群当前已知的所有节点(node),以及这些节点的相关信息。
[redis@jdmobile-2021-b963d1e2 bin]$ ./redis-cli -c -p 7001 -h 192.168.1.1
192.168.1.1:7001> cluster nodes
e8898127e29a07902742cd6c45c31c5ee44d0c6b 192.168.1.1:7002 master - 0 1639380640019 7 connected 5461-10922
de6402e1a48483a42dc38a48d4cd30ffc3ac6b20 192.168.1.1:7001 myself,master - 0 0 2 connected 10923-16383
75f0dd30328e4b0f95f0b3dfb3daaf8519cc9c15 192.168.1.2:7002 slave de6402e1a48483a42dc38a48d4cd30ffc3ac6b20 0 1639380641021 6 connected
d93ec459567e394c501fc020770277059763996d 192.168.1.2:7000 slave e8898127e29a07902742cd6c45c31c5ee44d0c6b 0 1639380639017 7 connected
6aa2ad7ef5037f1cd0aa99cae8bc4d499502a780 192.168.1.2:7000 master - 0 1639380637015 1 connected 0-5460
24ecff1ba09b4a6580276749dc584c1166c91356 192.168.1.2:7001 slave 6aa2ad7ef5037f1cd0aa99cae8bc4d499502a780 0 1639380638017 5 connected
2、cluster info:打印集群信息
[redis@jdmobile-2021-b963d1e2 bin]$ ./redis-cli -c -p 7000 -h 192.168.1.1
192.168.1.1:7000> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:7
cluster_my_epoch:1
cluster_stats_messages_sent:2619
cluster_stats_messages_received:1877
2.11 redis-cli用法
-c --开启reidis cluster模式,连接redis cluster节点时候使用。
-a --如果Redis配置了密码,可以用-a(auth)选项,有了这个选项就不需要手动输入auth命令
-p --端口
-h --ip地址
-raw --中文乱码时加上这个参数
2.12 flushall用法
/laity/home/redis/cluster/bin/redis-cli -c -p 7004 -h 192.168.1.1 -a abc -c flushall
在redis中,flushdb和flushall 都是清空当前数据库的操作,但是两者有很大的区别:
1、flushall 清空数据库并执行持久化操作,也就是rdb文件会发生改变,变成76个字节大小(初始状态下为76字节),所以执行flushall之后数据库真正意义上清空了.
2、flushdb 清空数据库,但是不执行持久化操作,也就是说rdb文件不发生改变。而redis的数据是从rdb快照文件中读取加载到内存的。所以在flushdb之后,如果想恢复数据库,则可以直接kill掉redis-server进程,然后重新启动服务,这样redis重新读取rdb文件,数据恢复到flushdb操作之前的状态。
2.13 错误汇总
/laity/home/redis/cluster/bin/redis-cli -c -p 7004 -h 192.168.1.1 -a abc -c flushall
如果出现不能写的报错((error) READONLY You can't write against a read only slave.),可以使用临时修改配置进行刷新
临时修改配置方法:
[redis@bossbilling-1274746c bin]$ ./redis-cli -c -p 7004 -h 192.168.1.1 -a abc
192.168.1.1:7004> ping
PONG
192.168.1.1:7004> config get slave-read-only
1) "slave-read-only"
2) "yes"
192.168.1.1:7004> config set slave-read-only no
OK
192.168.1.1:7004> config get slave-read-only
1) "slave-read-only"
2) "no"
192.168.1.1:7004> exit
[redis@bossbilling-1274746c bin]$ /laity/home/redis/cluster/bin/redis-cli -c -p 7004 -h 192.168.1.1 -a abc -c flushall
OK