1、redis301集群安装详细步骤redis 3.0.0 集群安装详细步骤一:关于redis cluster1 redis cluster的现状目前redis支持的cluster特性(已亲测):1):节点自动发现2):slave-master 选举,集群容错3):Hot resharding:在线分片4):进群管理:cluster xxx5):基于配置(nodes-port.conf)的集群管理6):ASK 转向/MOVED 转向机制.2 redis cluster 架构1)redis-cluster架构图架构细节:(1)所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议
2、优化传输速度和带宽.(2)节点的fail是通过集群中超过半数的节点检测失效时才生效.(3)客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可(4)redis-cluster把所有的物理节点映射到0-16383slot上,cluster 负责维护nodeslotvalue2) redis-cluster选举:容错(1)领着选举过程是集群中所有master参与,如果半数以上master节点与master节点通信超过(cluster-node-timeout),认为当前master节点挂掉.(2):什么时候整个集群不可用(cluster_
3、state:fail),当集群不可用时,所有对集群的操作做都不可用,收到(error) CLUSTERDOWN The cluster is down)错误 a:如果集群任意master挂掉,且当前master没有slave.集群进入fail状态,也可以理解成进群的slot映射0-16383不完成时进入fail状态. b:如果进群超过半数以上master挂掉,无论是否有slave集群进入fail状态.二:redis cluster的使用1 安装redis cluster前的准备1):安装redis-cluster依赖:redis-cluster的依赖库在使用时有兼容问题,在reshard时会遇
4、到各种错误,请按指定版本安装.确保系统安装zlib,否则gem install会报(no such file to load - zlib)#download:zlib-1.2.6.tar./configuremakemakeinstall(1)安装ruby:version(1.9.2)#ruby1.9.2cd/path/ruby./configure-prefix=/usr/local/rubymakemakeinstallsudocpruby/usr/local/bin(2)安装rubygem:version(1.8.16)#rubygems-1.8.16.tgzcd/path/gemsu
5、dorubysetup.rbsudocpbin/gem/usr/local/bin(3)安装gem-redis:version(3.0.0)geminstallredis-version3.0.0#由于源的原因,可能下载失败,就手动下载下来安装#download地址:http:/rubygems.org/gems/redis/versions/3.0.0geminstall-l/data/soft/redis-3.0.0.gem2 安装redis-clustercd/path/redismakesudocp/opt/redis/src/redis-server/usr/local/binsud
6、ocp/opt/redis/src/redis-cli/usr/local/binsudocp/opt/redis/src/redis-trib.rb/usr/local/bin三 Redis集群部署(RedHat 6.0)(要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如下)1 集群环境准备主机1: 10.10.105.30 (7001/7002/7003)主机2: 10.10.105.50 (8001/8002/8003)2 创建集群需要的目录主机1: 10.10.105.30(path 为r
7、edis程序所在目录,下同)mkdir-p/path/clustercd/ path /clustermkdir7000mkdir7001mkdir7002主机2: 10.10.105.50mkdir-p/path/clustercd/ path /clustermkdir8000mkdir8001mkdir80023 修改配置文件redis.confcp/path/redis3.0/redis.conf/path/clusterviredis.conf#修改配置文件中的下面选项port7000daemonizeyescluster-enabledyescluster-config-filen
8、odes.confcluster-node-timeout5000appendonlyyes#修改完redis.conf配置文件中的这些配置项之后把这个配置文件分别拷贝到两台主机7000/7001/7002/8000/8001/7002目录下面主机1:10.10.105.30cp/path/cluster/redis.conf/ path /cluster/7000cp/ path /cluster/redis.conf/ path/cluster/7001cp/ path /cluster/redis.conf/ path /cluster/7002主机2:10.10.105.50cp/pa
9、th/cluster/redis.conf/ path /cluster/8000cp/ path /cluster/redis.conf/ path/cluster/8001cp/ path /cluster/redis.conf/ path /cluster/8002#注意:拷贝完成之后要修改7001/7002/7003/7004/7005目录下面redis.conf文件中的port参数,分别改为对应的文件夹的名称4 分别启动这6个redis实例cd/path/cluster/7000redis-serverredis.confcd/ path /cluster/7001redis-ser
10、verredis.confcd/ path /cluster/7002redis-serverredis.confcd/ path /cluster/8000redis-serverredis.confcd/ path /cluster/8001redis-serverredis.confcd/ path /cluster/8002redis-serverredis.conf#启动之后使用命令查看redis的启动情况ps-ef|grepredis如下显示则说明启动成功,注意显示信息的最后面要有” cluster”字样,否则表示redis未工作在集群模式。bl460g8-30:ps -ef|gr
11、ep redisrcsm 9841 1 0 08:40 ? 00:00:19 ./redis-server *:7001 clusterrcsm 9848 1 0 08:40 ? 00:00:19 ./redis-server *:7002 clusterrcsm 9855 1 0 08:40 ? 00:00:19 ./redis-server *:7003 cluster5 执行redis的创建集群命令创建集群cd/usr/local/redis3.0/src./redis-trib.rbcreate-replicas110.10.105.30:700010.10.105.30:700110
12、.10.105.30:700210.10.105.50:800010.10.105.50:800110.10.105.50:8002命令说明:1 redis-trib.rb:是用ruby脚本语言编写的命令,脚本里面调用的是redis里面的集群指令,关于集群的指令在附录中有描述2 replicas: 代表创建master的同时也创建slave,slave的数量为1如果看到下面的信息,则表示集群创建成功(图片供参考)输入yes,然后配置完成。至此redis集群即搭建成功!6 使用redis-cli命令进入集群环境redis-cli-ch 10.10.105.30 -p7000: -c 代表以支持集
13、群的方式访问方式: -p port : -h hostname7 redis集群功能演示/home/rcsm/zhangj/redis-3.0.1/src:./redis-cli -c -p 8001 -h 10.10.105.5010.10.105.50:8001 set a b- Redirected to slot 15495 located at 10.10.105.50:8003OK10.10.105.50:8003 set c d- Redirected to slot 7365 located at 10.10.105.30:7002OK10.10.105.30:7002 set
14、 e f- Redirected to slot 15363 located at 10.10.105.50:8003OK10.10.105.50:8003 get ab10.10.105.50:8003 get b- Redirected to slot 3300 located at 10.10.105.50:8001(nil)10.10.105.50:8001 get c- Redirected to slot 7365 located at 10.10.105.30:7002d10.10.105.30:7002 /home/rcsm/zhangj/redis-3.0.1/src:附录
15、1)redis通用配置#GENERALdaemonizenotcp-backlog511timeout0tcp-keepalive0loglevelnoticedatabases16dir/opt/redis/dataslave-serve-stale-datayes#slave只读slave-read-onlyyes#notusedefaultrepl-disable-tcp-nodelayyesslave-priority100#打开aof持久化appendonlyyes#每秒一次aof写appendfsynceverysec#关闭在aofrewrite的时候对新的写操作进行fsyncno
16、-appendfsync-on-rewriteyesauto-aof-rewrite-min-size64mblua-time-limit50002) redis集群配置cluster-enabledyes#节点互连超时的阀值cluster-node-timeout15000cluster-migration-barrier1slowlog-log-slower-than10000slowlog-max-len128notify-keyspace-eventshash-max-ziplist-entries512hash-max-ziplist-value64list-max-ziplist-
17、entries512list-max-ziplist-value64set-max-intset-entries512zset-max-ziplist-entries128zset-max-ziplist-value64activerehashingyesclient-output-buffer-limitnormal000client-output-buffer-limitslave256mb64mb60client-output-buffer-limitpubsub32mb8mb60hz10aof-rewrite-incremental-fsyncyes3) redis特殊配置#包含通用配
18、置include/opt/redis/redis-common.conf#监听tcp端口port6379#最大可用内存maxmemory100m#内存耗尽时采用的淘汰策略:#volatile-lru-removethekeywithanexpiresetusinganLRUalgorithm#allkeys-lru-removeanykeyaccordinglytotheLRUalgorithm#volatile-random-removearandomkeywithanexpireset#allkeys-random-removearandomkey,anykey#volatile-ttl-
19、removethekeywiththenearestexpiretime(minorTTL)#noeviction-dontexpireatall,justreturnanerroronwriteoperationsmaxmemory-policyallkeys-lru#aof存储文件appendfilenameappendonly-6379.aof#rdb文件,只用于动态添加slave过程dbfilenamedump-6379.rdb#cluster配置文件(启动自动生成)cluster-config-filenodes-6379.conf#部署在同一机器的redis实例,把auto-aof
20、-rewrite搓开,防止瞬间fork所有redis进程做rewrite,占用大量内存auto-aof-rewrite-percentage80-1004) cluster 操作cluster集群相关命令,更多redis相关命令见文档:http:/redis.readthedocs.org/en/latest/集群CLUSTERINFO 打印集群的信息CLUSTERNODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。节点CLUSTERMEET 将ip和port所指定的节点添加到集群当中,让它成为集群的一份子。CLUSTERFORGET 从集群中移除node_id指定的节
21、点。CLUSTERREPLICATE 将当前节点设置为node_id指定的节点的从节点。CLUSTERSAVECONFIG 将节点的配置文件保存到硬盘里面。槽(slot)CLUSTERADDSLOTSslot. 将一个或多个槽(slot)指派(assign)给当前节点。CLUSTERDELSLOTSslot. 移除一个或多个槽对当前节点的指派。CLUSTERFLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。CLUSTERSETSLOTNODE 将槽slot指派给node_id指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽,然后再进行指派。CLUSTERSETSLOTMIGRATING 将本节点的槽slot迁移到node_id指定的节点中。CLUSTERSETSLOTIMPORTING 从node_id指定的节点中导入槽slot到本节点。CLUSTERSETSLOTSTABLE 取消对槽slot的导入(import)或者迁移(migrate)。键CLUSTERKEYSLOT 计算键key应该被放置在哪个槽上。CLUSTERCOUNTKEYSINSLOT 返回槽slot目前包含的键值对数量。CLUSTERGETKEYSINSLOT 返回count个slot槽中的键。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1