首页 / 数据库 / MySQL / Redis3.0.7集群部署完整版
Redis集群没有出来前,一直使用Codis集群,现在部署Redis集群看看效果如何。一,架构CentOS6.5 64位redis1 redis1:6379主 redis3:6380从redis2 redis2:6379主 redis1:6380从redis3 redis3:6379主 redis2:6380从二,部署Redis实例1,安装依赖yum -y install tcl-devel2,下载wget http://download.redis.io/releases/redis-3.0.7.tar.gz3,编译cd /usr/local/src/tar zxvf redis-3.0.7.tar.gzcd redis-3.0.7make4,测试make test5,安装二进制程序到/usr/local/bin/目录make installls /usr/local/bin/ -lh6,安装服务sh ./utils/install_server.shWelcome to the redis service installerThis script will help you easily set up a running redis serverPlease select the redis port for this instance: [6379]Selecting default: 6379Please select the redis config file name [/etc/redis/6379.conf]Selected default - /etc/redis/6379.confPlease select the redis log file name [/var/log/redis_6379.log] /data/redis/redis_6379.logPlease select the data directory for this instance [/var/lib/redis/6379] /data/redis/6379Please select the redis executable path [/usr/local/bin/redis-server]Selected config:Port : 6379Config file : /etc/redis/6379.confLog file : /data/redis/redis_6379.logData dir : /data/redis/6379Executable : /usr/local/bin/redis-serverCli Executable : /usr/local/bin/redis-cliIs this ok? Then press ENTER to go on or Ctrl-C to abort.Copied /tmp/6379.conf => /etc/init.d/redis_6379Installing service...Successfully added to chkconfig!Successfully added to runlevels 345!Starting Redis server...Installation successful!7,验证服务/etc/init.d/redis_6379 status8,其他节点类似安装三,部署Cluster环境1,集群主从节点关系redis1 redis1:6379主 redis3:6380从redis2 redis2:6379主 redis1:6380从redis3 redis3:6379主 redis2:6380从2,集群环境准备这是源码安装tar -xf ruby-2.2.3.tar.gzcd ruby-2.2.3./configure --disable-install-rdocmake && make install/usr/local/bin/ruby/usr/local/bin/gemYUM安装yum install ruby ruby-devel rubygems设置ruby gem源为淘宝,安装gems更快。# gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/ 查看gem源# gem sources -l 安装ruby的redis模块(依赖接口)# gem install redis(注意这地方要选定版本排错时会提到)3,配置文件(其他实例相应修改端口)cat /etc/redis/6379.conf |grep -Ev "^#|^$"daemonize yespidfile /var/run/redis_6379.pidport 6379timeout 0logfile /data/redis/redis_6379.logdbfilename dump.rdbdir /data/redis/6379appendonly noappendfilename "appendonly.aof"appendfsync everysec取消如下注释,让Redis在集群模式下运行cluster-enabled yes 启动cluster模式cluster-config-file nodes-6379.conf 集群信息文件名,由redis自己维护cluster-node-timeout 5000 5秒中联系不到对方node,即认为对方有故障可能4,重启redis (其他实例相应修改端口)/etc/init.d/redis_6379 restart会在/data/redis/6379/下面多出一个文件nodes-6379.conf5,利用脚本工具建立集群首先查看帮助看redis-trib脚本功能注意编译安装时redis-trib.rb没有复制过来cp ./src/redis-trib.rb /usr/local/bin/ruby redis-trib.rb helpcreate:创建集群check:检查集群info:查看集群信息fix:修复集群reshard:在线迁移slotrebalance:平衡集群节点slot数量add-node:将新节点加入集群del-node:从集群中删除节点set-timeout:设置集群节点间心跳连接的超时时间call:在集群全部节点上执行命令import:将外部redis数据导入集群开始创建集群注意前三个为主后三个为从(但是不能指定主从关系,后面可以在Cluster里面操作,目的是主从实例不能在统一节点服务器上), 选项 --replicas 1 表示集群中的每个主节点创建一个从节点。/usr/local/bin/redis-trib.rb create --replicas 1 192.168.188.18:6379 192.168.188.28:6379 192.168.188.38:6379 192.168.188.18:6380 192.168.188.28:6380 192.168.188.38:6380>>> Creating cluster>>> Performing hash slots allocation on 6 nodes...Using 3 masters:192.168.188.38:6379192.168.188.28:6379192.168.188.18:6379Adding replica 192.168.188.28:6380 to 192.168.188.38:6379Adding replica 192.168.188.38:6380 to 192.168.188.28:6379Adding replica 192.168.188.18:6380 to 192.168.188.18:6379M: 53c202561155d141a8e24d42bb8eb2907998fce9 192.168.188.18:6379 slots:10923-16383 (5461 slots) masterM: 654c0a7853724fe33683f54e8df665993ea704ed 192.168.188.28:6379 slots:5461-10922 (5462 slots) masterM: b4cc74e0ac6ec9b634d93e86fdb376b4d89dc318 192.168.188.38:6379 slots:0-5460 (5461 slots) masterS: ee3d5f85034e5a3c9d9369357b545d0a92751728 192.168.188.18:6380 replicates 53c202561155d141a8e24d42bb8eb2907998fce9S: 10b558217d6618093b87f75a453ca39679bbcdf4 192.168.188.28:6380 replicates b4cc74e0ac6ec9b634d93e86fdb376b4d89dc318S: 5dc55e90d3d3b56502a319ec9845e901102cfa6c 192.168.188.38:6380 replicates 654c0a7853724fe33683f54e8df665993ea704edCan 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 clusterWaiting for the cluster to join......>>> Performing Cluster Check (using node 192.168.188.18:6379)M: 53c202561155d141a8e24d42bb8eb2907998fce9 192.168.188.18:6379 slots:10923-16383 (5461 slots) masterM: 654c0a7853724fe33683f54e8df665993ea704ed 192.168.188.28:6379 slots:5461-10922 (5462 slots) masterM: b4cc74e0ac6ec9b634d93e86fdb376b4d89dc318 192.168.188.38:6379 slots:0-5460 (5461 slots) masterM: ee3d5f85034e5a3c9d9369357b545d0a92751728 192.168.188.18:6380 slots: (0 slots) master replicates 53c202561155d141a8e24d42bb8eb2907998fce9M: 10b558217d6618093b87f75a453ca39679bbcdf4 192.168.188.28:6380 slots: (0 slots) master replicates b4cc74e0ac6ec9b634d93e86fdb376b4d89dc318M: 5dc55e90d3d3b56502a319ec9845e901102cfa6c 192.168.188.38:6380 slots: (0 slots) master replicates 654c0a7853724fe33683f54e8df665993ea704ed[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.四,测试验证调整主从状态登录redis集群:设置192.168.188.18:6380为192.168.188.28:6379的从redis-cli -c -p 6380 -h 192.168.188.18192.168.188.18:6380> cluster replicate 654c0a7853724fe33683f54e8df665993ea704edOK设置192.168.188.38:6380为192.168.188.18:6379的从redis-cli -c -p 6380 -h 192.168.188.38192.168.188.38:6380> cluster replicate 53c202561155d141a8e24d42bb8eb2907998fce9OK最后保存配置到硬盘192.168.188.38:6380> cluster saveconfigOK检查集群状态:redis-trib.rb check 192.168.188.18:6379>>> Performing Cluster Check (using node 192.168.188.18:6379)M: 53c202561155d141a8e24d42bb8eb2907998fce9 192.168.188.18:6379 slots:10923-16383 (5461 slots) master 1 additional replica(s)S: 5dc55e90d3d3b56502a319ec9845e901102cfa6c 192.168.188.38:6380 slots: (0 slots) slave replicates 53c202561155d141a8e24d42bb8eb2907998fce9M: 654c0a7853724fe33683f54e8df665993ea704ed 192.168.188.28:6379 slots:5461-10922 (5462 slots) master 1 additional replica(s)S: 10b558217d6618093b87f75a453ca39679bbcdf4 192.168.188.28:6380 slots: (0 slots) slave replicates b4cc74e0ac6ec9b634d93e86fdb376b4d89dc318S: ee3d5f85034e5a3c9d9369357b545d0a92751728 192.168.188.18:6380 slots: (0 slots) slave replicates 654c0a7853724fe33683f54e8df665993ea704edM: b4cc74e0ac6ec9b634d93e86fdb376b4d89dc318 192.168.188.38:6379 slots:0-5460 (5461 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.使用方法集群 CLUSTER INFO 打印集群的信息 CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。 节点 CLUSTER MEET <ip> <port> 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。 CLUSTER FORGET <node_id> 从集群中移除 node_id 指定的节点。 CLUSTER REPLICATE <node_id> 将当前节点设置为 node_id 指定的节点的从节点。 CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。 槽(slot) CLUSTER ADDSLOTS <slot> [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。 CLUSTER DELSLOTS <slot> [slot ...] 移除一个或多个槽对当前节点的指派。 CLUSTER FLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。 CLUSTER SETSLOT <slot> NODE <node_id> 将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。 CLUSTER SETSLOT <slot> MIGRATING <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。 CLUSTER SETSLOT <slot> IMPORTING <node_id> 从 node_id 指定的节点中导入槽 slot 到本节点。 CLUSTER SETSLOT <slot> STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。 键 CLUSTER KEYSLOT <key> 计算键 key 应该被放置在哪个槽上。 CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的键值对数量。 CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 个 slot 槽中的键。 五,排错优化报错/usr/lib/ruby/gems/1.8/gems/redis-3.0.7/lib/redis/client.rb:97:in `call": ERR Slot 16011 is already busy (Redis::CommandError)from /usr/lib/ruby/gems/1.8/gems/redis-3.0.7/lib/redis.rb:2432:in `method_missing"from /usr/lib/ruby/gems/1.8/gems/redis-3.0.7/lib/redis.rb:37:in `synchronize"from /usr/lib/ruby/1.8/monitor.rb:242:in `mon_synchronize"from /usr/lib/ruby/gems/1.8/gems/redis-3.0.7/lib/redis.rb:37:in `synchronize"from /usr/lib/ruby/gems/1.8/gems/redis-3.0.7/lib/redis.rb:2431:in `method_missing"from /usr/local/bin/redis-trib.rb:212:in `flush_node_config"from /usr/local/bin/redis-trib.rb:775:in `flush_nodes_config"from /usr/local/bin/redis-trib.rb:774:in `each"from /usr/local/bin/redis-trib.rb:774:in `flush_nodes_config"from /usr/local/bin/redis-trib.rb:1295:in `create_cluster_cmd"from /usr/local/bin/redis-trib.rb:1695:in `send"from /usr/local/bin/redis-trib.rb:1695
处理 rm -rf /data/redis/6379/nodes-6379.confrm -rf /data/redis/6380/nodes-6380.conf/etc/init.d/redis_6379 restart/etc/init.d/redis_6380 restartgem listgem uninstall redis --version 3.3.2gem install redis --version 3.0.7gem list
警告 WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add "vm.overcommit_memory = 1" to /etc/sysctl.conf and then reboot or run the command "sysctl vm.overcommit_memory=1" for this to take effect.7997:M 08 Dec 15:04:59.760 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command "echo never > /sys/kernel/mm/transparent_hugepage/enabled" as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.优化vm.overcommit_memory = 1echo never > /sys/kernel/mm/transparent_hugepage/enabled下面关于Redis的文章您也可能喜欢,不妨参考下:Ubuntu 14.04下Redis安装及简单测试 http://www.linuxidc.com/Linux/2014-05/101544.htmRedis主从复制基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htmRedis集群明细文档 http://www.linuxidc.com/Linux/2013-09/90118.htmUbuntu 16.04环境中安装PHP7.0 Redis扩展 http://www.linuxidc.com/Linux/2016-09/135631.htmCentOS 7.0 安装Redis 3.2.1详细过程和使用常见问题 http://www.linuxidc.com/Linux/2016-09/135071.htmUbuntu 16.04环境中安装PHP7.0 Redis扩展 http://www.linuxidc.com/Linux/2016-09/135631.htmUbuntu 15.10下Redis集群部署文档 http://www.linuxidc.com/Linux/2016-06/132340.htmRedis实战 中文PDF http://www.linuxidc.com/Linux/2016-04/129932.htmRedis 的详细介绍:请点这里
Redis 的下载地址:请点这里本文永久更新链接地址