环境说明:
两台虚拟机,IP地址分别为:192.168.81.100和192.168.81.101 虚拟机系统为:CentOS 7.5 Redis为yum安装,版本为3.2 系统环境:关闭firewalld Redis Cluster集群节点包括:192.168.81.101机器的9000.9001,9002端口运行的redis-server以及192.168.81.100机器的9000.9001,9002端口运行的redis-server步骤:
1.Ruby环境准备 官网下载Ruby源码包,解压,编译,安装,进行软链接 2.Ruby环境安装redis模块 [root@mysql ruby_package]# ruby -v # ruby安装完成,查看版本 ruby 2.3.7p456 (2018-03-28 revision 63024) [x86_64-linux] [root@mysql ruby_package]# gem install redis # 安装ruby操作redis模块 Successfully installed redis-4.0.2 Parsing documentation for redis-4.0.2 Done installing documentation for redis after 0 seconds 1 gem installed [root@mysql ruby_package]# gem list -- check # 查看ruby环境已经安装的模块 *** LOCAL GEMS *** bigdecimal (1.2.8) did_you_mean (1.0.0) io-console (0.4.5) json (1.8.3.1) minitest (5.8.5) net-telnet (0.1.1) power_assert (0.2.6) psych (2.1.0.1) rake (10.4.2) rdoc (4.2.1) redis (4.0.2, 3.3.0) test-unit (3.1.5) 3.获取redis-trib.rb工具 [root@mysql ruby_package]# rz # 从Redis官网下载Redis安装包,并上传到虚拟机中 rz waiting to receive. Starting zmodem transfer. Press Ctrl+C to cancel. Transferring redis-3.2.12.tar.gz... 100% 1515 KB 1515 KB/sec 00:00:01 0 Errors [root@mysql ruby_package]# ls redis-3.2.12.tar.gz ruby-2.3.7 ruby-2.3.7.tar.gz rubygems-2.7.7.zip [root@mysql ruby_package]# tar xf redis-3.2.12.tar.gz # 对Redis安装包进行解压 [root@mysql ruby_package]# ls [root@mysql ruby_package]# cd redis-3.2.12/src # 进入Redis目录的src目录下 [root@mysql src]# ls adlist.c asciilogo.h crc64.h help.h Makefile.dep quicklist.h release.c setproctitle.c t_hash.c zipmap.c adlist.h bio.c db.c hyperloglog.c memtest.c rand.c replication.c sha1.c t_list.c zipmap.h ae.c bio.h debug.c intset.c mkreleasehdr.sh rand.h rio.c sha1.h t_set.c zmalloc.c ae_epoll.c bitops.c debugmacro.h intset.h multi.c rdb.c rio.h slowlog.c t_string.c zmalloc.h ae_evport.c blocked.c dict.c latency.c networking.c rdb.h scripting.c slowlog.h t_zset.c ae.h cluster.c dict.h latency.h notify.c redisassert.h sdsalloc.h solarisfixes.h util.c ae_kqueue.c cluster.h endianconv.c lzf_c.c object.c redis-benchmark.c sds.c sort.c util.h ae_select.c config.c endianconv.h lzf_d.c pqsort.c redis-check-aof.c sds.h sparkline.c valgrind.sup anet.c config.h fmacros.h lzf.h pqsort.h redis-check-rdb.c sentinel.c sparkline.h version.h anet.h crc16.c geo.c lzfP.h pubsub.c redis-cli.c server.c syncio.c ziplist.c aof.c crc64.c geo.h Makefile quicklist.c redis-trib.rb server.h testhelp.h ziplist.h [root@mysql src]# cp redis-trib.rb /usr/bin # 把redis-trib.rb拷贝到/usr/bin目录下 [root@mysql src]# redis-trib.rb # 查看redis-trib.rb的帮助文档 Usage: redis-trib <command> <options> <arguments ...> create mysql:port1 ... hostN:portN --replicas <arg> check host:port info host:port fix host:port --timeout <arg> reshard host:port --from <arg> --to <arg> --slots <arg> --yes --timeout <arg> --pipeline <arg> rebalance host:port --weight <arg> --auto-weights --use-empty-masters --timeout <arg> --simulate --pipeline <arg> --threshold <arg> add-node new_host:new_port existing_host:existing_port --slave --master-id <arg> del-node host:port node_id set-timeout host:port milliseconds call host:port command arg arg .. arg import host:port --from <arg> --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. 4.在192.168.81.101虚拟机上创建配置文件,并非常用启动Redis server [root@mysql ~]# ps aux | grep redis-server root 1684 0.0 0.0 112664 972 pts/0 R+ 11:44 0:00 grep --color=auto redis-server [root@mysql ~]# cd /opt/config/ [root@mysql config]# ls redis_7000.conf redis_7001.conf redis_7002.conf [root@mysql config]# sed 's/7000/9000/g' redis_7000.conf > redis_9000.conf # 创建redis_9000.conf配置文件 [root@mysql config]# sed 's/7000/9001/g' redis_7000.conf > redis_9001.conf # 创建redis_9001.conf配置文件 [root@mysql config]# sed 's/7000/9002/g' redis_7000.conf > redis_9002.conf # 创建redis_9002.conf配置文件 [root@mysql config]# redis-server /opt/config/redis_9000.conf # 指定配置文件,启动redis-server [root@mysql config]# redis-server /opt/config/redis_9001.conf # 指定配置文件,启动redis-server [root@mysql config]# redis-server /opt/config/redis_9002.conf # 指定配置文件,启动redis-server [root@mysql config]# ps aux | grep redis-server # 查看已经启动的redis-server root 1948 0.2 0.3 142916 7572 ? Ssl 12:21 0:02 redis-server 0.0.0.0:9002 [cluster] root 1952 0.2 0.3 142916 7560 ? Ssl 12:21 0:02 redis-server 0.0.0.0:9001 [cluster] root 1964 0.2 0.3 142916 7356 ? Ssl 12:21 0:02 redis-server 0.0.0.0:9000 [cluster] root 6640 0.0 0.0 112664 972 pts/1 R+ 12:42 0:00 grep --color=auto redis-server [root@mysql config]# cat redis_9000.conf # redis_9000.conf配置文件内容 port 9000 bind 0.0.0.0 daemonize yes dir '/var/lib/redis' logfile '/var/log/redis/redis_9000.log' dbfilename 'redis_9000.data' cluster-enabled yes cluster-config-file nodes-9000.conf cluster-require-full-coverage no [root@mysql config]# redis-cli -p 9000 cluster nodes # 查看集群中节点的信息,都只显示自身节点 5eba129e4f4a4be2ffe630fe9fac19ba30f5b419 :9000 myself,master - 0 0 0 connected [root@mysql config]# redis-cli -p 9001 cluster nodes # 查看集群中节点的信息,都只显示自身节点 32d2d969c41f4af646b1052f10d69fd29510f3e4 :9001 myself,master - 0 0 0 connected [root@mysql config]# redis-cli -p 9002 cluster nodes # 查看集群中节点的信息,都只显示自身节点 6c43b4ddfeaeb3030af397e7469bb0d0b7673979 :9002 myself,master - 0 0 0 connected 使用同样的步骤,在192.168.81.100虚拟机上生成配置文件:redis_9000.conf,redis_9001.conf,redis_9002.conf 5.在192.168.81.101虚拟机上使用redis-trib.rb工具构建集群 [root@mysql config]# redis-trib.rb create --replicas 1 127.0.0.1:9000 127.0.0.1:9001 127.0.0.1:9002 192.168.81.100:9000 192.168.81.100:9001 192.168.81.100:9002 # 构建集群,replicas后的参数为集群的节点 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: # 指定master 127.0.0.1:9000 192.168.81.100:9000 127.0.0.1:9001 Adding replica 192.168.81.100:9001 to 127.0.0.1:9000 # 为master添加slave Adding replica 127.0.0.1:9002 to 192.168.81.100:9000 Adding replica 192.168.81.100:9002 to 127.0.0.1:9001 M: cf74b2d9570665b74525802462c74cf2e072ef99 127.0.0.1:9000 slots:0-5460 (5461 slots) master M: fceba6001b95e2169ddd6622436b213324fe8f77 127.0.0.1:9001 slots:10923-16383 (5461 slots) master S: 6f369311b0ca4c503f337c4bb23424eed3eeb188 127.0.0.1:9002 replicates cb8c114d44d289687798508232d31e0a065fdab5 M: cb8c114d44d289687798508232d31e0a065fdab5 192.168.81.100:9000 slots:5461-10922 (5462 slots) master S: 71f5695dc1f4322c8ea9066d1b3cd8bb0ab7a329 192.168.81.100:9001 replicates cf74b2d9570665b74525802462c74cf2e072ef99 S: ac153a3c1fb10d9d502b153c32dec65c85b2f97c 192.168.81.100:9002 replicates fceba6001b95e2169ddd6622436b213324fe8f77 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 127.0.0.1:9000) M: cf74b2d9570665b74525802462c74cf2e072ef99 127.0.0.1:9000 slots:0-5460 (5461 slots) master 1 additional replica(s) M: cb8c114d44d289687798508232d31e0a065fdab5 192.168.81.100:9000 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 6f369311b0ca4c503f337c4bb23424eed3eeb188 127.0.0.1:9002 slots: (0 slots) slave replicates cb8c114d44d289687798508232d31e0a065fdab5 M: fceba6001b95e2169ddd6622436b213324fe8f77 127.0.0.1:9001 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: ac153a3c1fb10d9d502b153c32dec65c85b2f97c 192.168.81.100:9002 slots: (0 slots) slave replicates fceba6001b95e2169ddd6622436b213324fe8f77 S: 71f5695dc1f4322c8ea9066d1b3cd8bb0ab7a329 192.168.81.100:9001 slots: (0 slots) slave replicates cf74b2d9570665b74525802462c74cf2e072ef99 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.需要注意的是:如果上面的操作中,一直阻塞在'Waiting for the cluster to join',则是因为meet操作阻塞,可以手动进行meet操作