Redis Sentinel与Cluster安装部署(三)
4.3redis-cluster-resharding
重新分配槽
1、查看当前集群节点状态信息 # redis-cli --cluster check 172.32.1.59:7000 -a dt2020 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 172.32.1.91:7001 (f5d02386...) -> 0 keys | 5462 slots | 1 slaves. 172.32.1.140:7002 (75427ec8...) -> 0 keys | 5461 slots | 1 slaves. 172.32.1.140:7011 (3ae32dd1...) -> 0 keys | 5461 slots | 1 slaves. [OK] 0 keys in 3 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 172.32.1.59:7000) S: 372c67215d9ebcbd3e54c80120263a7300fa173a 172.32.1.59:7000 slots: (0 slots) slave replicates 3ae32dd1aa45786118052cfa65963b7467c68e9d S: 3257571369e23dd7ed6a69b79d8f40d095b5982b 172.32.1.59:7012 slots: (0 slots) slave replicates f5d0238686f868684ff55910229cec62d64bc3fd M: f5d0238686f868684ff55910229cec62d64bc3fd 172.32.1.91:7001 slots:[5461-10922] (5462 slots) master 1 additional replica(s) M: 75427ec80ad8634c4f1bd95c5fb35c1349bdd23d 172.32.1.140:7002 slots:[10923-16383] (5461 slots) master 1 additional replica(s) M: 3ae32dd1aa45786118052cfa65963b7467c68e9d 172.32.1.140:7011 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: c1ae0f9c1b2ccdbed0c054d83653e1aeae34a408 172.32.1.91:7010 slots: (0 slots) slave replicates 75427ec80ad8634c4f1bd95c5fb35c1349bdd23d [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. 2、将 7001 节点的1000个槽,分给 7002 # redis-cli --cluster reshard 172.32.1.91:7001 -a dt2020 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 172.32.1.91:7001 (f5d02386...) -> 0 keys | 5462 slots | 1 slaves. 172.32.1.140:7002 (75427ec8...) -> 0 keys | 5461 slots | 1 slaves. 172.32.1.140:7011 (3ae32dd1...) -> 0 keys | 5461 slots | 1 slaves. [OK] 0 keys in 3 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 172.32.1.59:7000) S: 372c67215d9ebcbd3e54c80120263a7300fa173a 172.32.1.59:7000 slots: (0 slots) slave replicates 3ae32dd1aa45786118052cfa65963b7467c68e9d S: 3257571369e23dd7ed6a69b79d8f40d095b5982b 172.32.1.59:7012 slots: (0 slots) slave replicates f5d0238686f868684ff55910229cec62d64bc3fd M: f5d0238686f868684ff55910229cec62d64bc3fd 172.32.1.91:7001 slots:[5461-10922] (5462 slots) master 1 additional replica(s) M: 75427ec80ad8634c4f1bd95c5fb35c1349bdd23d 172.32.1.140:7002 slots:[10923-16383] (5461 slots) master 1 additional replica(s) M: 3ae32dd1aa45786118052cfa65963b7467c68e9d 172.32.1.140:7011 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: c1ae0f9c1b2ccdbed0c054d83653e1aeae34a408 172.32.1.91:7010 slots: (0 slots) slave replicates 75427ec80ad8634c4f1bd95c5fb35c1349bdd23d [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. ##上方展示的是集群的状态信息 ##下方输入需要迁移多少个槽 How many slots do you want to move (from 1 to 16384)? 1000 ##下方需要输入将槽迁至哪个节点,需要输入的是节点ID What is the receiving node ID? 75427ec80ad8634c4f1bd95c5fb35c1349bdd23d Please enter all the source node IDs. Type 'all' to use all the nodes as source nodes for the hash slots. Type 'done' once you entered all the source nodes IDs. ##下方输入需要从哪些节点迁移这些槽,需要输入的是节点ID Source node #1: f5d0238686f868684ff55910229cec62d64bc3fd Source node #2: done ##下方展示的是分配槽的相关信息 Ready to move 1000 slots. Source nodes: M: f5d0238686f868684ff55910229cec62d64bc3fd 172.32.1.91:7001 slots:[5461-10922] (5462 slots) master 1 additional replica(s) Destination node: M: 75427ec80ad8634c4f1bd95c5fb35c1349bdd23d 172.32.1.140:7002 slots:[10923-16383] (5461 slots) master 1 additional replica(s) Resharding plan: Moving slot 5461 from f5d0238686f868684ff55910229cec62d64bc3fd Moving slot 5462 from f5d0238686f868684ff55910229cec62d64bc3fd ...... Moving slot 6460 from f5d0238686f868684ff55910229cec62d64bc3fd ##下方需要输入yes确认执行 Do you want to proceed with the proposed reshard plan (yes/no)? yes ...... 3、查看分配后的集群状态,发现已将7001节点的1000个槽,分给7002节点 # redis-cli --cluster check 172.32.1.59:7000 -a dt2020 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 172.32.1.91:7001 (f5d02386...) -> 0 keys | 4462 slots | 1 slaves. 172.32.1.140:7002 (75427ec8...) -> 0 keys | 6461 slots | 1 slaves. 172.32.1.140:7011 (3ae32dd1...) -> 0 keys | 5461 slots | 1 slaves. [OK] 0 keys in 3 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 172.32.1.59:7000) S: 372c67215d9ebcbd3e54c80120263a7300fa173a 172.32.1.59:7000 slots: (0 slots) slave replicates 3ae32dd1aa45786118052cfa65963b7467c68e9d S: 3257571369e23dd7ed6a69b79d8f40d095b5982b 172.32.1.59:7012 slots: (0 slots) slave replicates f5d0238686f868684ff55910229cec62d64bc3fd M: f5d0238686f868684ff55910229cec62d64bc3fd 172.32.1.91:7001 slots:[6461-10922] (4462 slots) master 1 additional replica(s) M: 75427ec80ad8634c4f1bd95c5fb35c1349bdd23d 172.32.1.140:7002 slots:[5461-6460],[10923-16383] (6461 slots) master 1 additional replica(s) M: 3ae32dd1aa45786118052cfa65963b7467c68e9d 172.32.1.140:7011 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: c1ae0f9c1b2ccdbed0c054d83653e1aeae34a408 172.32.1.91:7010 slots: (0 slots) slave replicates 75427ec80ad8634c4f1bd95c5fb35c1349bdd23d [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. 4、如果不想以上方交互式的命令分配槽,可以直接执行下方命令 # redis-cli --cluster reshard <host>:<port> --cluster-from <node-id> --cluster-to <node-id> --cluster-slots <number of slots> --cluster-yes -a <password>
增删节点
1、重新安装一个新的redis节点,修改redis.conf配置文件,按照“cluster部署”内配置文件的要求区修改相关参数,并用redis-server redis.conf命令启动新节点 2、将新节点添加到集群中,让新节点作为master节点 # redis-cli --cluster add-node <IP_1>:<port_1> <IP_2>:<port_2> -a <password> 说明:<IP_1>:<port_1>是要加入集群的新节点地址,<IP_2>:<port_2>是原集群任一节点的地址,-a指定密码 此时新节点被加入集群中,但没有分配哈希槽: a.由于没有分配的哈希槽,因此不保存任何数据 b.因为它是没有分配哈希槽的节点,所以当发生failover,某个slave要成为master时,它不会参与选举过程 3、为主节点非配哈希槽 # redis-cli --cluster reshard <IP>:<port> -a <password> 说明:<IP>:<port>是集群中任一节点的地址,-a指定密码 4、将新节点添加到集群中,并将新节点作为集群中某个master的slave节点 4-1、将新节点随机添加到有较少slave节点的master节点下: # redis-cli --cluster add-node <IP_1>:<port_1> <IP_2>:<port_2> --cluster-slave -a <password> 说明:<IP_1>:<port_1>是要加入集群的新节点地址,<IP_2>:<port_2>是原集群任一节点的地址,-a指定密码 4-2、将新节点添加到指定的master节点下: # redis-cli --cluster add-node <IP_1>:<port_1> <IP_2>:<port_2> --cluster-slave --cluster-master-id <master-node-id> -a <password> 说明:<IP_1>:<port_1>是要加入集群的新节点地址,<IP_2>:<port_2>是原集群任一节点的地址,<master-node-id>是主节点的ID,-a指定密码 4-3、可以将新节点添加为集群中的主节点(不分配哈希槽),然后使用CLUSTER REPLICATE命令将该节点作为另一个master的从节点 在空的主节点执行以下命令,<master-node-id>是主节点的ID # CLUSTER REPLICATE <master-node-id> 5、删除节点 如果删除的是主节点,需要先将主节点的哈希槽转移至其它主节点,如果是从节点,可以直接删除,命令如下: # redis-cli --cluster del-node <IP>:<port> <node-id> 说明:<IP>:<port>是集群中任一节点的地址,<node-id> 是要删除的节点ID
5、压测
5.1 redis-benchmark说明
说明:redis-benchmark是redis自带的压测工具,用redis-benchmark -h可以查看具体如何使用。
redis-benchmark命令详解:
# redis-benchmark -h Invalid option "-h" or option argument missing Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests>] [-k <boolean>] -h <hostname> Server hostname (default 127.0.0.1) -p <port> Server port (default 6379) -s <socket> Server socket (overrides host and port) -a <password> Password for Redis Auth -c <clients> Number of parallel connections (default 50) //并发连接数 -n <requests> Total number of requests (default 100000) //请求总数 -d <size> Data size of SET/GET value in bytes (default 3)//SET/GET值的数据大小(以字节为单位),默认3字节 --dbnum <db> SELECT the specified db number (default 0)//选择指定的db号 -k <boolean> 1=keep alive 0=reconnect (default 1) -r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADD//SET/GET/INCR使用随机key,SADD使用随机值 Using this option the benchmark will expand the string __rand_int__ inside an argument with a 12 digits number in the specified range from 0 to keyspacelen-1. The substitution changes every time a command is executed. Default tests use this to hit random keys in the specified range. -P <numreq> Pipeline <numreq> requests. Default 1 (no pipeline).//通过管道传输<numreq>请求,默认为1不使用管道 -e If server replies with errors, show them on stdout. (no more than 1 error per second is displayed) -q Quiet. Just show query/sec values//输出的结果将只显示requests per second(QPS)信息 --csv Output in CSV format -l Loop. Run the tests forever -t <tests> Only run the comma separated list of tests. The test //对指定命令进行基准测试,如-t get,set names are the same as the ones produced as output. -I Idle mode. Just open N idle connections and wait. Examples: Run the benchmark with the default configuration against 127.0.0.1:6379: $ redis-benchmark Use 20 parallel clients, for a total of 100k requests, against 192.168.1.1: $ redis-benchmark -h 192.168.1.1 -p 6379 -n 100000 -c 20 Fill 127.0.0.1:6379 with about 1 million keys only using the SET test: $ redis-benchmark -t set -n 1000000 -r 100000000 Benchmark 127.0.0.1:6379 for a few commands producing CSV output: $ redis-benchmark -t ping,set,get -n 100000 --csv Benchmark a specific command line: $ redis-benchmark -r 10000 -n 10000 eval 'return redis.call("ping")' 0 Fill a list with 10000 random elements: $ redis-benchmark -r 10000 -n 10000 lpush mylist __rand_int__ On user specified command lines __rand_int__ is replaced with a random integer with a range of values selected by the -r option.
redis-benchmark输出信息:
用redis-benchmark进行压测,会输出很多命令在指定的并发、请求下的QPS值
总共会输出以下命令:
(1) PING_INLINE (2) PING_BULK (3) SET:将字符串值value关联到key; (4) GET:返回key所关联的字符串值,如果key存储的值不是字符串类型,返回一个错误; (5) INCR:将key中存储的数字值增一。不能转换为数字则报错; (6) LPUSH:将一个或多个值value插入到列表key的表头; (7) RPUSH:将一个或多个值value插入到列表key的表尾; (8) LPOP:移除并返回列表key的头元素; (9) RPOP:移除并返回列表key的尾元素; (10) SADD:将一个或多个member元素加入到集合set当中,已经存在于集合的member元素将被忽略; (11) SPOP:移除并返回集合中的一个随机元素; (12) LPUSH:将一个或多个value插入到列表key的表头; (13) LRANGE_100:返回列表key中指定区间内的元素,前100条元素; (14) LRANGE_300:返回列表key中指定区间内的元素,前300条元素; (15) LRANGE_500:返回列表key中指定区间内的元素,前500条元素; (16) LRANGE_600:返回列表key中指定区间内的元素,前600条元素; (17) MSET :同时设置一个或多个key-value对,value为字符串
以GET为例,输出内容的含义如下:
压测输出的GET信息: ====== GET ====== 20000 requests completed in 0.27 seconds 100 parallel clients 3 bytes payload keep alive: 1 99.11% <= 1 milliseconds 100.00% <= 1 milliseconds 73529.41 requests per second 运行结果说明: 一共在0.27s内运行了20000次GET 100个并发 每个请求数据量是3个字节 99.11%的命令执行时间小于1毫秒 Redis每秒可以处理73529.41次get请求(QPS)
5.2压测
对不同场景进行压测,命令如下:
1、单一key,50连接,10万请求
# redis-benchmark -h 172.32.1.59 -p 6379 -c 50 -n 100000 -q
2、随机key,50连接,10万请求
# redis-benchmark -h 172.32.1.59 -p 6379 -c 50 -n 100000 -q -r 100000
3、随机key,100连接,10万请求
# redis-benchmark -h 172.32.1.59 -p 6379 -c 100 -n 100000 -q -r 100000
4、随机key,200连接,10万请求
# redis-benchmark -h 172.32.1.59 -p 6379 -c 200 -n 100000 -q -r 100000
5、随机key,300连接,10万请求
# redis-benchmark -h 172.32.1.59 -p 6379 -c 300 -n 100000 -q -r 100000
6、随机key,400连接,10万请求
# redis-benchmark -h 172.32.1.59 -p 6379 -c 400 -n 100000 -q -r 100000
7、随机key,500连接,10万请求
# redis-benchmark -h 172.32.1.59 -p 6379 -c 500 -n 100000 -q -r 100000
8、随机key,1000连接,10万请求
# redis-benchmark -h 172.32.1.59 -p 6379 -c 1000 -n 100000 -q -r 100000
9、随机key,1000连接,10万请求,16管道
# redis-benchmark -h 172.32.1.59 -p 6379 -c 1000 -n 100000 -q -r 100000 -P 16
压测结果如下:
单元格中的值为QPS
场景 | 单一key,50连接,10万请求 | 随机key,50连接,10万请求 | 随机key,100连接,10万请求 | 随机key,200连接,10万请求 | 随机key,300连接,10万请求 | 随机key,400连接,10万请求 | 随机key,500连接,10万请求 | 随机key,1000连接,10万请求 | 随机key,1000连接,10万请求,16管道 |
PING_INLINE | 73583.52 | 84961.77 | 81234.77 | 80385.85 | 84817.64 | 82576.38 | 82508.25 | 82918.74 | 775193.81 |
PING_BULK | 79808.46 | 84674.01 | 81366.97 | 82781.46 | 79365.08 | 82372.32 | 84104.29 | 79239.3 | 847457.62 |
SET | 77279.75 | 84817.64 | 81699.35 | 82440.23 | 80775.45 | 76277.65 | 82440.23 | 72463.77 | 675675.69 |
GET | 79936.05 | 83752.09 | 81300.81 | 78554.59 | 81366.97 | 75187.97 | 80710.25 | 79617.83 | 826446.31 |
INCR | 79808.46 | 80906.16 | 80515.3 | 81967.21 | 81234.77 | 83542.19 | 81766.15 | 81234.77 | 714285.69 |
LPUSH | 79936.05 | 82169.27 | 81433.22 | 82440.23 | 81366.97 | 82781.46 | 87796.3 | 82781.46 | 819672.12 |
RPUSH | 81766.15 | 84745.77 | 83056.48 | 82304.52 | 81900.09 | 82918.74 | 86430.43 | 81168.83 | 840336.12 |
LPOP | 80710.25 | 86805.56 | 82372.32 | 83892.62 | 80515.3 | 78125 | 87412.59 | 84033.61 | 757575.75 |
RPOP | 81366.97 | 85178.88 | 85034.02 | 82508.25 | 83402.84 | 76452.6 | 87489.06 | 79491.26 | 892857.12 |
SADD | 80192.46 | 80128.2 | 81103 | 83056.48 | 80385.85 | 80515.3 | 89126.56 | 65487.89 | 813008.12 |
HSET | 83682.01 | 84175.09 | 80128.2 | 83752.09 | 79808.46 | 77639.75 | 87796.3 | 79365.08 | 684931.5 |
SPOP | 74074.07 | 79808.46 | 82034.45 | 81433.22 | 72202.16 | 82781.46 | 86505.19 | 79617.83 | 892857.12 |
LPUSH | 81234.77 | 82781.46 | 81900.09 | 80775.45 | 77399.38 | 79491.26 | 88495.58 | 78740.16 | 854700.88 |
LRANGE_100 | 74962.52 | 86956.52 | 76804.91 | 84388.19 | 79113.92 | 81499.59 | 87642.41 | 77399.38 | 641025.62 |
LRANGE_300 | 82781.46 | 85324.23 | 78988.94 | 79936.05 | 83125.52 | 83752.09 | 88339.23 | 78616.35 | 833333.38 |
LRANGE_500 | 84530.86 | 83263.95 | 82508.25 | 85106.38 | 80192.46 | 80580.17 | 89206.06 | 75585.79 | 709219.88 |
LRANGE_600 | 81300.81 | 81300.81 | 82304.52 | 78431.38 | 80064.05 | 81967.21 | 88573.96 | 76103.5 | 800000 |
MSET | 84104.29 | 78369.91 | 80580.17 | 80000 | 81967.21 | 74183.98 | 77881.62 | 71942.45 | 336700.34 |