环境
#environmentOS: CentOS 7.2 64#ip192.168.21.137~139
redis 节点
目录和文件准备
#三台各自执行mkdir /rediswget -P /redis http://download.redis.io/releases/redis-4.0.1.tar.gz#安装cd redis-4.0.1make && make install
节点
#通过配置文件给每台服务器配置三个redis节点mkdir -p /redis_cluster/6001 /redis_cluster/6002 /redis_cluster/6003cp /redis/redis-4.0.1/redis.conf /redis_cluster/6001/cp /redis/redis-4.0.1/redis.conf /redis_cluster/6002/cp /redis/redis-4.0.1/redis.conf /redis_cluster/6003/#配置#redis后台运行daemonize yes #pidfile文件 6001~6003pidfile /var/run/redis_6000.pid #6001~6003port 7000 #开启集群cluster-enabled yes #配置文件首次启动自动生成cluster-config-file nodes_6001.conf #请求超时cluster-node-timeout 5000 #aof日志appendonly no #绑定地址,需要别的机器能ping通的地址bind 192.168.21.137
启动
redis-server /redis_cluster/6001/redis.confredis-server /redis_cluster/6002/redis.confredis-server /redis_cluster/6003/redis.conf
检查是否启动成功及信息
[root@localhost /]# ps -ef | grep redisroot 16700 1 0 23:08 ? 00:00:00 redis-server 127.0.0.1:6001root 16705 1 0 23:08 ? 00:00:00 redis-server 127.0.0.1:6002root 16710 1 0 23:08 ? 00:00:00 redis-server 127.0.0.1:6003root 16715 12065 0 23:08 pts/1 00:00:00 grep --color=auto redis[root@localhost /]# netstat -tnlp | grep redistcp 0 0 127.0.0.1:6001 0.0.0.0:* LISTEN 16700/redis-server tcp 0 0 127.0.0.1:6002 0.0.0.0:* LISTEN 16705/redis-server tcp 0 0 127.0.0.1:6003 0.0.0.0:* LISTEN 16710/redis-server [root@localhost /]#
至此,redis 节点准备完成。
搭建集群
redis 官方提供了一个 redis-trib.rb
(src目录下) 工具用于搭建集群。很明显是 ruby 写的,所以需要 ruby 环境。
安装 ruby
yum -y install ruby ruby-devel rubygems rpm-build
用 gem 安装 redis 接口
[root@localhost /]# gem install redisFetching: redis-3.3.3.gem (100%)Successfully installed redis-3.3.3Parsing documentation for redis-3.3.3Installing ri documentation for redis-3.3.31 gem installed
确保开放端口
,可参考我的另一个微博
redis-trib.rb
查看 集群创建帮助 [root@localhost /]# /redis/redis-4.0.1/src/redis-trib.rb Usage: redis-tribcreate host1:port1 ... hostN:portN --replicas check host:port info host:port fix host:port --timeout reshard host:port --from --to --slots --yes --timeout --pipeline rebalance host:port --weight --auto-weights --use-empty-masters --timeout --simulate --pipeline --threshold add-node new_host:new_port existing_host:existing_port --slave --master-id del-node host:port node_id set-timeout host:port milliseconds call host:port command arg arg .. arg import host:port --from --copy --replace help (show this help)For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
创建集群
#其中 --replicas 2 意思为为每个 master 分配 2 各 slave[root@localhost ~]# /redis/redis-4.0.1/src/redis-trib.rb create --replicas 2 192.168.21.137:6001 192.168.21.137:6002 192.168.21.137:6003 192.168.21.138:6001 192.168.21.138:6002 192.168.21.138:6003 192.168.21.139:6001 192.168.21.139:6002 192.168.21.139:6003>>> Creating cluster>>> Performing hash slots allocation on 9 nodes...Using 3 masters:192.168.21.137:6001192.168.21.138:6001192.168.21.139:6001Adding replica 192.168.21.138:6002 to 192.168.21.137:6001Adding replica 192.168.21.139:6002 to 192.168.21.137:6001Adding replica 192.168.21.137:6002 to 192.168.21.138:6001Adding replica 192.168.21.137:6003 to 192.168.21.138:6001Adding replica 192.168.21.138:6003 to 192.168.21.139:6001Adding replica 192.168.21.139:6003 to 192.168.21.139:6001M: 40217a88d10bb85bdd0d082df24f036435b82513 192.168.21.137:6001 slots:0-5460 (5461 slots) masterS: bc847ed99feac417a066baaa48fa0748ac409c67 192.168.21.137:6002 replicates 4a391d4987ee5f0cf342108890cf8236d56ea412S: 840ab2d448387c801b67a958543090ce1d8ee1ba 192.168.21.137:6003 replicates 4a391d4987ee5f0cf342108890cf8236d56ea412M: 4a391d4987ee5f0cf342108890cf8236d56ea412 192.168.21.138:6001 slots:5461-10922 (5462 slots) masterS: b977d15fbeaa3e88b830bf5b6caa22a27a2dce8f 192.168.21.138:6002 replicates 40217a88d10bb85bdd0d082df24f036435b82513S: bd0fdf29c06fb27b2aa9eca478ec20ab9daf81da 192.168.21.138:6003 replicates c32b18ec7a947f01bde73a3c93abc31d60b060eeM: c32b18ec7a947f01bde73a3c93abc31d60b060ee 192.168.21.139:6001 slots:10923-16383 (5461 slots) masterS: e323d818a7be4149053669552230f4a98332734c 192.168.21.139:6002 replicates 40217a88d10bb85bdd0d082df24f036435b82513S: 3bcd9cf11589f418fe5cbc1895ac5a23b0b6714e 192.168.21.139:6003 replicates c32b18ec7a947f01bde73a3c93abc31d60b060eeCan 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.21.137:6001)M: 40217a88d10bb85bdd0d082df24f036435b82513 192.168.21.137:6001 slots:0-5460 (5461 slots) master 2 additional replica(s)S: bc847ed99feac417a066baaa48fa0748ac409c67 192.168.21.137:6002 slots: (0 slots) slave replicates 4a391d4987ee5f0cf342108890cf8236d56ea412S: 3bcd9cf11589f418fe5cbc1895ac5a23b0b6714e 192.168.21.139:6003 slots: (0 slots) slave replicates c32b18ec7a947f01bde73a3c93abc31d60b060eeS: bd0fdf29c06fb27b2aa9eca478ec20ab9daf81da 192.168.21.138:6003 slots: (0 slots) slave replicates c32b18ec7a947f01bde73a3c93abc31d60b060eeS: 840ab2d448387c801b67a958543090ce1d8ee1ba 192.168.21.137:6003 slots: (0 slots) slave replicates 4a391d4987ee5f0cf342108890cf8236d56ea412M: c32b18ec7a947f01bde73a3c93abc31d60b060ee 192.168.21.139:6001 slots:10923-16383 (5461 slots) master 2 additional replica(s)S: b977d15fbeaa3e88b830bf5b6caa22a27a2dce8f 192.168.21.138:6002 slots: (0 slots) slave replicates 40217a88d10bb85bdd0d082df24f036435b82513M: 4a391d4987ee5f0cf342108890cf8236d56ea412 192.168.21.138:6001 slots:5461-10922 (5462 slots) master 2 additional replica(s)S: e323d818a7be4149053669552230f4a98332734c 192.168.21.139:6002 slots: (0 slots) slave replicates 40217a88d10bb85bdd0d082df24f036435b82513[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.
测试集群
#在 192.168.21.138:6001 机器上设置一个键值对 testKey--testValue ,因为绑定了 IP ,所以 h 参数不能省略[root@localhost ~]# redis-cli -h 192.168.21.138 -c -p 6001192.168.21.138:6001> set testKey testValue-> Redirected to slot [5203] located at 192.168.21.137:6001OK192.168.21.137:6001> get testKey"testValue"192.168.21.137:6001> ...#在 192.168.21.137:6003 上获取 testKey 的值,发现自动定向到 192.168.21.137:6001,说明成功[root@localhost ~]# redis-cli -h 192.168.21.137 -c -p 6003192.168.21.137:6003> get testKey-> Redirected to slot [5203] located at 192.168.21.137:6001"testValue"192.168.21.137:6001> ...#在 192.168.21.139:6002 上获取 testKey 的值,发现自动定向到 192.168.21.137:6001,说明成功[root@localhost ~]# redis-cli -h 192.168.21.139 -c -p 6002192.168.21.139:6002> get testKey-> Redirected to slot [5203] located at 192.168.21.137:6001"testValue"192.168.21.137:6001>
至此,Redis 集群搭建完成
。
集群操作
检查状态
[root@localhost ~]# /redis/redis-4.0.1/src/redis-trib.rb check 192.168.21.139:6002>>> Performing Cluster Check (using node 192.168.21.139:6002)S: e323d818a7be4149053669552230f4a98332734c 192.168.21.139:6002 slots: (0 slots) slave replicates 40217a88d10bb85bdd0d082df24f036435b82513S: 840ab2d448387c801b67a958543090ce1d8ee1ba 192.168.21.137:6003 slots: (0 slots) slave replicates 4a391d4987ee5f0cf342108890cf8236d56ea412S: bd0fdf29c06fb27b2aa9eca478ec20ab9daf81da 192.168.21.138:6003 slots: (0 slots) slave replicates c32b18ec7a947f01bde73a3c93abc31d60b060eeM: c32b18ec7a947f01bde73a3c93abc31d60b060ee 192.168.21.139:6001 slots:10923-16383 (5461 slots) master 2 additional replica(s)S: b977d15fbeaa3e88b830bf5b6caa22a27a2dce8f 192.168.21.138:6002 slots: (0 slots) slave replicates 40217a88d10bb85bdd0d082df24f036435b82513S: bc847ed99feac417a066baaa48fa0748ac409c67 192.168.21.137:6002 slots: (0 slots) slave replicates 4a391d4987ee5f0cf342108890cf8236d56ea412M: 4a391d4987ee5f0cf342108890cf8236d56ea412 192.168.21.138:6001 slots:5461-10922 (5462 slots) master 2 additional replica(s)M: 40217a88d10bb85bdd0d082df24f036435b82513 192.168.21.137:6001 slots:0-5460 (5461 slots) master 2 additional replica(s)S: 3bcd9cf11589f418fe5cbc1895ac5a23b0b6714e 192.168.21.139:6003 slots: (0 slots) slave replicates c32b18ec7a947f01bde73a3c93abc31d60b060ee[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.
查看所有节点信息
[root@localhost ~]# redis-cli -h 192.168.21.138 -c -p 6001192.168.21.138:6001> cluster nodese323d818a7be4149053669552230f4a98332734c 192.168.21.139:6002@16002 slave 40217a88d10bb85bdd0d082df24f036435b82513 0 1503628318000 8 connectedbc847ed99feac417a066baaa48fa0748ac409c67 192.168.21.137:6002@16002 slave 4a391d4987ee5f0cf342108890cf8236d56ea412 0 1503628318620 4 connectedc32b18ec7a947f01bde73a3c93abc31d60b060ee 192.168.21.139:6001@16001 master - 0 1503628317000 7 connected 10923-16383bd0fdf29c06fb27b2aa9eca478ec20ab9daf81da 192.168.21.138:6003@16003 slave c32b18ec7a947f01bde73a3c93abc31d60b060ee 0 1503628318000 7 connected40217a88d10bb85bdd0d082df24f036435b82513 192.168.21.137:6001@16001 master - 0 1503628318117 1 connected 0-54604a391d4987ee5f0cf342108890cf8236d56ea412 192.168.21.138:6001@16001 myself,master - 0 1503628317000 4 connected 5461-10922840ab2d448387c801b67a958543090ce1d8ee1ba 192.168.21.137:6003@16003 slave 4a391d4987ee5f0cf342108890cf8236d56ea412 0 1503628318519 4 connected3bcd9cf11589f418fe5cbc1895ac5a23b0b6714e 192.168.21.139:6003@16003 slave c32b18ec7a947f01bde73a3c93abc31d60b060ee 0 1503628317111 9 connectedb977d15fbeaa3e88b830bf5b6caa22a27a2dce8f 192.168.21.138:6002@16002 slave 40217a88d10bb85bdd0d082df24f036435b82513 0 1503628317111 5 connected192.168.21.138:6001> cluster infocluster_state:okcluster_slots_assigned:16384cluster_slots_ok:16384cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:9cluster_size:3cluster_current_epoch:9cluster_my_epoch:4cluster_stats_messages_ping_sent:4391cluster_stats_messages_pong_sent:4314cluster_stats_messages_meet_sent:2cluster_stats_messages_sent:8707cluster_stats_messages_ping_received:4308cluster_stats_messages_pong_received:4393cluster_stats_messages_meet_received:6cluster_stats_messages_received:8707192.168.21.138:6001>
节点命令
#将 ip 和 port 所在的节点添加到集群当中cluster meet#从集群中移除 node_id 指定的节点。cluster forget #将当前结点设置为 node_id 的 slave(从节点)cluster replicate #保存节点配置文件cluster saveconfig
槽命令
#将一个或多个槽( slot)指派( assign)给当前节点。cluster addslots[slot ...] #移除一个或多个槽对当前节点的指派。cluster delslots [slot ...] #移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。cluster flushslots #将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。cluster setslot node #将本节点的槽 slot 迁移到 node_id 指定的节点中。cluster setslot migrating #从 node_id 指定的节点中导入槽 slot 到本节点。cluster setslot importing #取消对槽 slot 的导入( import)或者迁移( migrate)。cluster setslot stable
键
#计算键 key 应该被放置在哪个槽上。cluster keyslot#返回槽 slot 目前包含的键值对数量。cluster countkeysinslot #返回 count 个 slot 槽中的键 。cluster getkeysinslot
脚本
启动节点
#!/bin/bashfor i in 1 2 3do /redis/redis-4.0.1/src/redis-server /redis_cluster/600$i/redis.conf;done
启动集群
#!/bin/bash/redis/redis-4.0.1/src/redis-trib.rb create --replicas 2 192.168.21.137:6001 192.168.21.137:6002 192.168.21.137:6003 192.168.21.138:6001 192.168.21.138:6002 192.168.21.138:6003 192.168.21.139:6001 192.168.21.139:6002 192.168.21.139:6003
关闭
#!/bin/bashfor i in 137 138 139do for j in 1 2 3 do /redis/redis-4.0.1/src/redis-cli -c -h 192.168.21.$i -p 600$j shutdown; donedone