Redis高可用集群
Redis 高可用集群Redis 的集群主从模型是一种高可用的集群架构。本章主要内容有:高可用集群的搭建,Jedis连接集群,新增集群节点,删除集群节点,其他配置补充说明。
高可用集群搭建
集群(cluster)技术是一种较新的技术,通过集群技术,可以在付出较低成本的情况下获得在性能、可靠性、灵活性方面的相对较高的收益,其任务调度则是集群系统中的核心技术。
Redis 3.0 之后便支持集群。Redis 集群中内置了 16384 个哈希槽。Redis 会根据节点数量大致均等的将哈希槽映射到不同的节点。
所有节点之间彼此互联(PING-PONG机制),当超过半数的主机认为某台主机挂了,则该主机就是真的挂掉了,整个集群就不可用了。
若给集群中的每台主机分配多台从机。主机挂了,从机上位,依然能正常工作。但是若集群中超过半数的主机挂了,无论是否有从机,该集群也是不可用的。
搭建前的准备工作
搭建ruby环境
redis集群管理工具 redis-trib.rb 是依赖 ruby 环境。
# yum install ruby
# yum install rubygems
# gem install redis
# cd redis-4.0.2/src/
# cp redis-trib.rb /usr/local/redis-4/bin/
第一步:安装 ruby 环境
第二步:安装 gem 软件包(gem是用来扩展或修改Ruby应用程序的)。
第三步:在redis解压目录中找到 redis-trib.rb 文件,将其拷贝到启动redis服务的目录下,方便管理。
可能存在的问题
1 redis requires Ruby version >= 2.2.2,解决方法如下
2 没有/usr/local/rvm/scripts/rvm 这个目录.可能是上一步执行失败
# ruby --version
ruby 1.8.7 (2013-06-27 patchlevel 374)
# yum install curl
# curl -L get.rvm.io | bash -s stable
# source /usr/local/rvm/scripts/rvm
# rvm list known
# rvm install 2.3.3
# rvm use 2.3.3
# gem install redis
准备六台redis服务器
和上一章节主从复制逻辑一样,将redis.conf文件拷贝6次,端口从6000~6005
# cp redis.conf redis6000.conf
# vim redis6000.conf
port xxxx #修改端口
cluster-enabled yes #打开注释,开启集群模式
cluster-config-file nodes-xxxx.conf #集群的配置文件
pidfile /var/run/redis_xxxx.pid #pidfile文件
logfile "xxxx.log" #日志文件
dbfilename dump_xxxx.rdb #rdb持久化文件
cluster-node-timeout 5000 #请求超时,单位毫秒
appendonly yes #开启aof持久化方式
# vim start-all.sh
./redis-server redis6000.conf
./redis-server redis6001.conf
./redis-server redis6002.conf
./redis-server redis6003.conf
./redis-server redis6004.conf
./redis-server redis6005.conf
# chmod u+x start-all.sh
# ./start-all.sh
# ps aux | grep redis
root 280010.00.9 1459649696 ? Ssl17:45 0:00 ./redis-server 112.74.83.71:6000
root 280030.00.9 1459649696 ? Ssl17:45 0:00 ./redis-server 112.74.83.71:6001
root 280080.00.9 1459649656 ? Ssl17:45 0:00 ./redis-server 112.74.83.71:6002
root 280130.00.9 1459649656 ? Ssl17:45 0:00 ./redis-server 112.74.83.71:6003
root 280180.10.9 1459649652 ? Ssl17:45 0:00 ./redis-server 112.74.83.71:6004
root 280230.00.9 1459649656 ? Ssl17:45 0:00 ./redis-server 112.74.83.71:6005
第一步:复制六个redis.conf 并修改相关配置,如果觉得麻烦可以用我配置的文件:https://github.com/ITDragonBlog/daydayup/tree/master/Redis/reids.conf
第二步:新增批量开启redis服务程序,并增加执行权限
第三步:查看六台redis服务是否启动成功
主从集群搭建
集群创建命令: ./redis-trib.rb create 创建集群,--replicas 1 给每个主机分配一个从机,后面其他参数都是redis服务的ip:port。最后输入yes来接受建议的配置
第一步:搭建集群 ./redis-trib.rb create ,选择yes接受建议的配置
第二步:进入集群客户端 ./redis-cli -h 任意主机host -p 任意主机port -c,-c表示以集群方式连接redis
第三步:保存数据
第四步:cluster info 查询集群状态信息
第五步:cluster nodes 查询集群结点信息,这里有一个坑,后面会介绍
可能存在的问题
Sorry, the cluster configuration file nodes.conf is already used by a different Redis Cluster node. Please make sure that different nodes use different cluster configuration files.
说的很明确,修改cluster-config-file nodes.conf 文件避免重名,或者删除该文件重新创建集群。
cluster nodes 查询集群节点信息
这是很重要的命令,我们需要关心的信息有:
第一个参数:节点ID
第二个参数:IP:PORT@TCP 这里一个坑,jedis-2.9.0之前的版本解析@出错
第三个参数:标志(Master,Slave,Myself,Fail...)
第四个参数:如果是从机则是主机的节点ID
最后两个参数:连接的状态和槽的位置。
Jedis 连接集群
首先要配置防火墙
# vim /etc/sysconfig/iptables
-A INPUT -p tcp -m tcp --dport 6000 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 6001 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 6002 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 6003 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 6004 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 6005 -j ACCEPT
# service iptables restart
最后是整合Spring
单元测试
可能存在的问题
java.lang.NumberFormatException: For input string: "6002@16002"
若redis 的版本在4.0.0之上,建议使用jedis-2.9.0以上。
集群节点操作
添加主节点
# cp redis6005.conf redis6006.conf
# ./redis-server redis6006.conf
# ./redis-trib.rb add-node 112.74.83.71:6006 112.74.83.71:6000
# ./redis-cli -h 112.74.83.71 -p 6000 cluster nodes
916d26e9638dc51e168f32969da11e19c875f48f 112.74.83.71:6006@16006 master - 0 1512115612162 0 connected # 没有分配槽
# ./redis-trib.rb reshard 112.74.83.71:6000
How many slots do you want to move (from 1 to 16384)? 500
What is the receiving node>
Please enter all the source node> Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes> Source node #1:all
Do you want to proceed with the proposed reshard plan (yes/no)? yes
# ./redis-cli -h 112.74.83.71 -p 6000 cluster nodes
> 916d26e9638dc51e168f32969da11e19c875f48f 112.74.83.71:6006@16006 master - 0 1512116047897 7 connected 0-165 5461-5627 10923-11088
第一步:创建 redis6006.conf 的新主机,并启动Redis服务
第二步:新增主机节点,若打印" New node added correctly." 表示添加成功
第三步:查询集群节点信息,发现6006端口的主机虽然已经添加,但连接状态后面没有内容,即没分配槽
第四步:给6006端口主机分配槽,
第一个参数:需要移动槽的个数,
第二个参数:接受槽的节点ID,
第三个参数:输入"all"表示从所有原节点中获取槽,
第四个参数:输入"yes"开始移动槽到目标结点id
第五步:查询集群节点信息,发现6006端口的主机已经分配了槽
核心命令:
./redis-trib.rb add-node 新增主机ip:port 集群任意节点ip:port
./redis-trib.rb reshard 集群任意节点ip:port
可能存在的问题
Sorry, can't connect to node 112.74.83.71:6006
说明:新增的主机必须要是启动状态。
添加从节点
第一步:创建 redis6007.conf 的新主机,并启动Redis服务
第二步:新增从机节点,在原来的命令上多了 --slave --master-id 主节点ID
第三步:查询集群节点信息
删除结点
删除节点前,要确保该节点没有值,否则提示:is not empty! Reshard data away and try again. 若该节点有值,则需要把槽分配出去
./redis-trib.rb del-node 112.74.83.71:6006 916d26e9638dc51e168f32969da11e19c875f48f
配置文件补充
前几章Redis教程中介绍了以下配置
1 开启Redis 的守护进程 :daemonize yes
2 指定pid文件写入文件名 :pidfile /var/run/redis.pid
3 指定Redis 端口:port 6379
4 绑定的主机地址 :bind 127.0.0.1
5 Redis持久化默认开启压缩数据:rdbcompression yes
6 指定rdb文件名:dbfilename dump.rdb
7 指定rdb文件位置:dir ./
8 从机启动时,它会自动从master进行数据同步:slaveof < masterip> < masterport>
9 开启aof持久化方式:appendonly yes
10 指定aof文件名:appendfilename appendonly.aof
11 触发aof快照机制:appendfsync everysec (no/always)
本章节是Redis教程中的最后一章,把剩下的配置也一起说了吧
1 设置客户端连接超时时间,0表示关闭 :timeout 300
2 设置Redis日志级别,debug、verbose(默认)、notice、warning:loglevel verbose
3 设置数据库的数量:databases 16
4 设置Redis连接密码:requirepass foobared
5 设置同一时间最大客户端连接数,默认无限制:maxclients 128
6 指定Redis最大内存限制:maxmemory < bytes>
7 指定是否启用虚拟内存机制:vm-enabled no
8 指定虚拟内存文件路径:vm-swap-file /tmp/redis.swap
9 指定包含其它的配置文件:include /path/to/local.conf
到这里Redis 的教程就结束了,还有其他知识可以访问官网学习。有什么不对的地方请指正。谢谢!
页:
[1]