三,故障转移
1.模拟redis2故障
[root@localhost ~]# redis-cli -h 192.168.2.2 -p 6380 shutdown
2.查看从是否晋升为主
[root@localhost ~]# redis-cli -h 192.168.2.1 -p 6380 cluster nodes b6f47e859ad6a3b9b238778d1cf0df4f95710a1f 192.168.2.3:6380@16380 master - 0 1678679339733 4 connected 10923-16383 dee913166afa30b567b151e6e1196847ed0a8a96 192.168.2.2:6381@16381 slave b6f47e859ad6a3b9b238778d1cf0df4f95710a1f 0 1678679339000 4 connected 693d6d17f5860887d3343ebe0083351cd9815c8d 192.168.2.3:6381@16381 slave 9f48a4262474744dc22524b28096ccba111d236d 0 1678679337719 5 connected 961bb9571f4e2f64a320da2631ae8cfc3b78d1e1 192.168.2.1:6381@16381 master - 0 1678679338726 6 connected 5461-10922 9f48a4262474744dc22524b28096ccba111d236d 192.168.2.1:6380@16380 myself,master - 0 1678679338000 2 connected 0-5460 d9ea4f26b2221a5497d78326a7c1aff828e98426 192.168.2.2:6380@16380 master,fail - 1678679289623 1678679289320 3 disconnected
3.启动redis2:6380
[root@localhost redis]# redis-server /opt/redis_cluster/redis_6380/conf/redis_6380.conf
4.重回master
[root@localhost ~]# redis-cli -h 192.168.2.2 -p 6380 cluster failover OK [root@localhost ~]# redis-cli -h 192.168.2.1 -p 6380 cluster nodes b6f47e859ad6a3b9b238778d1cf0df4f95710a1f 192.168.2.3:6380@16380 master - 0 1678679584000 4 connected 10923-16383 dee913166afa30b567b151e6e1196847ed0a8a96 192.168.2.2:6381@16381 slave b6f47e859ad6a3b9b238778d1cf0df4f95710a1f 0 1678679586000 4 connected 693d6d17f5860887d3343ebe0083351cd9815c8d 192.168.2.3:6381@16381 slave 9f48a4262474744dc22524b28096ccba111d236d 0 1678679586593 5 connected 961bb9571f4e2f64a320da2631ae8cfc3b78d1e1 192.168.2.1:6381@16381 slave d9ea4f26b2221a5497d78326a7c1aff828e98426 0 1678679583000 7 connected 9f48a4262474744dc22524b28096ccba111d236d 192.168.2.1:6380@16380 myself,master - 0 1678679584000 2 connected 0-5460 d9ea4f26b2221a5497d78326a7c1aff828e98426 192.168.2.2:6380@16380 master - 0 1678679585588 7 connected 5461-10922
三,自动搭建部署redis cluster
1.低版本部署
手动搭建集群便于理解集群创建的流程和细节,不过手动搭建集群需要很多步骤,当集群节点众多时,必然会加大搭建集群的复杂度和运维成本,因此官方提供了 redis-trib.rb的工具方便我们快速搭建集群。redis-trib.rb是采用 Ruby 实现的 redis 集群管理工具,内部通过 Cluster相关命令帮我们简化集群创建、检查、槽迁移和均衡等常见运维操作,使用前要安装 ruby 依赖环境
前提准备:
停掉所有的节点,然后清空数据,恢复成一个全新的集群,所有机器执行命令
pkill redis rm -rf /data/redis_cluster/redis_6380/* rm -rf /data/redis_cluster/redis_6381/*
全部清空之后启动所有的节点,所有机器执行
redis-server /opt/redis_cluster/redis_6380/conf/redis_6380.conf redis-server /opt/redis_cluster/redis_6381/conf/redis_6381.conf
(1)安装命令:注意新版本redis不需安装,直接采用步骤(2)
yum makecache fast yum install rubygems gem sources --remove https://rubygems.org/ gem sources -a http://mirrors.aliyun.com/rubygems/ gem update –system gem install redis -v 3.3.5
redis1执行创建集群命令
cd /opt/redis_cluster/redis/src/ ./redis-trib.rb create --replicas 1 192.168.8.10:6380 192.168.8.20:6380 192.168.8.20:6380 192.168.8.10:6381 192.168.8.30:6381 192.168.8.30:6381
检查集群完整性
./redis-trib.rb check 192.168.8.10:6380
2.redis5.0版本自动部署cluster
清空手动创建的缓存
pkill redis rm -rf /data/redis_cluster/redis_6380/* rm -rf /data/redis_cluster/redis_6381/*
自动部署cluser
redis-cli --cluster create --cluster-replicas 1 192.168.2.1:6380 192.168.2.2:6380 192.168.2.3:6380 192.168.2.1:6381 192.168.2.2:6381 192.168.2.3:6381
检查完整性:
redis-cli --cluster check 192.168.8.10:6380
3.扩容节点
1.添加节点
[root@localhost ~]# mkdir -p /opt/redis_cluster/redis_{6390,6391}/{conf,logs,pid} [root@localhost ~]# mkdir -p /data/redis_cluster/redis_{6390,6391} [root@localhost ~]# cd /opt/redis_cluster/ [root@localhost redis_cluster]# cp redis_6380/conf/redis_6380.conf redis_6390/conf/redis_6390.conf [root@localhost redis_cluster]# cp redis_6380/conf/redis_6380.conf redis_6391/conf/redis_6391.conf [root@localhost redis_cluster]# sed -i 's#6380#6390#g' redis_6390/conf/redis_6390.conf [root@localhost redis_cluster]# sed -i 's#6380#6391#g' redis_6391/conf/redis_6391.conf [root@localhost redis_cluster]# redis-server /opt/redis_cluster/redis_6390/conf/redis_6390.conf [root@localhost redis_cluster]# redis-server /opt/redis_cluster/redis_6391/conf/redis_6391.conf
2.发现节点
[root@localhost redis_cluster]# redis-cli -h 192.168.2.1 -p 6380 cluster meet 192.168.2.1 6390 OK [root@localhost redis_cluster]# redis-cli -h 192.168.2.1 -p 6380 cluster meet 192.168.2.1 6391 OK
3.分配slots
redis-cli --cluster reshard 192.168.2.1:6390 输入分配的slots数:4096 #你分配多少空间给他 再输入6390的id号: #确认一下6390的ID 再输入all #从那几台集群服务器分配 all所有服务器平均分配
4.分配slave
[root@localhost redis_cluster]# redis-cli -h 192.168.2.1 -p 6380 cluster nodes b6f47e859ad6a3b9b238778d1cf0df4f95710a1f 192.168.2.3:6380@16380 master - 0 1678681164252 4 connected 12288-16383 dee913166afa30b567b151e6e1196847ed0a8a96 192.168.2.2:6381@16381 slave b6f47e859ad6a3b9b238778d1cf0df4f95710a1f 0 1678681164000 4 connected c03a5451d189fb10451a58ccc44846a66f86a83f 192.168.2.1:6391@16391 master - 0 1678681164000 8 connected 693d6d17f5860887d3343ebe0083351cd9815c8d 192.168.2.3:6381@16381 slave 9f48a4262474744dc22524b28096ccba111d236d 0 1678681166266 5 connected 961bb9571f4e2f64a320da2631ae8cfc3b78d1e1 192.168.2.1:6381@16381 slave d9ea4f26b2221a5497d78326a7c1aff828e98426 0 1678681164000 7 connected ec5e8ba00823c8d972f8c6c048ea0b964df10f40 192.168.2.1:6390@16390 master - 0 1678681165259 9 connected 0-1364 5461-6826 10923-12287 9f48a4262474744dc22524b28096ccba111d236d 192.168.2.1:6380@16380 myself,master - 0 1678681163000 2 connected 1365-5460 d9ea4f26b2221a5497d78326a7c1aff828e98426 192.168.2.2:6380@16380 master - 0 1678681167272 7 connected 6827-10922 [root@localhost redis_cluster]# redis-cli -h 192.168.2.1 -p 6391 cluster replicate ec5e8ba00823c8d972f8c6c048ea0b964df10f40 OK
4.收缩节点
1.移除节点槽位
[root@localhost redis_cluster]# redis-cli --cluster reshard 192.168.8.10:6390 How many slots do you want to move (from 1 to 16384)? 1365 What is the receiving node ID? 9f48a4262474744dc22524b28096ccba111d236d 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. Source node #1: ec5e8ba00823c8d972f8c6c048ea0b964df10f40 Source node #2: done 翻译: 你想移动多少个槽位(从1到16384)?1365 接收节点的ID是什么?9 f48a4262474744dc22524b28096ccba111d236d 请输入所有源节点id 键入'all'以使用所有节点作为散列槽的源节点。 输入所有源节点id后,输入` done `。
本课案例中分三次移除:分别 1365 给redis1的6380 1366 给redis2的6380 1365 给redis3的6380
2.移除节点
1. redis-cli -c -h 192.168.8.10 -p 6380 cluster forget 6390的ID 2. 3. redis-cli -c -h 192.168.8.10 -p 6380 cluster forget 6391的ID
[root@localhost redis_cluster]# redis-cli -c -h 192.168.2.1 -p 6380 cluster nodes b6f47e859ad6a3b9b238778d1cf0df4f95710a1f 192.168.2.3:6380@16380 master - 0 1678681998352 12 connected 10923-16383 dee913166afa30b567b151e6e1196847ed0a8a96 192.168.2.2:6381@16381 slave b6f47e859ad6a3b9b238778d1cf0df4f95710a1f 0 1678681998000 12 connected c03a5451d189fb10451a58ccc44846a66f86a83f 192.168.2.1:6391@16391 slave b6f47e859ad6a3b9b238778d1cf0df4f95710a1f 0 1678682003000 12 connected 693d6d17f5860887d3343ebe0083351cd9815c8d 192.168.2.3:6381@16381 slave 9f48a4262474744dc22524b28096ccba111d236d 0 1678682000000 10 connected 961bb9571f4e2f64a320da2631ae8cfc3b78d1e1 192.168.2.1:6381@16381 slave d9ea4f26b2221a5497d78326a7c1aff828e98426 0 1678682003386 11 connected ec5e8ba00823c8d972f8c6c048ea0b964df10f40 192.168.2.1:6390@16390 master - 0 1678682002382 9 connected 9f48a4262474744dc22524b28096ccba111d236d 192.168.2.1:6380@16380 myself,master - 0 1678682001000 10 connected 0-5460 d9ea4f26b2221a5497d78326a7c1aff828e98426 192.168.2.2:6380@16380 master - 0 1678682002000 11 connected 5461-10922 [root@localhost redis_cluster]# redis-cli -c -h 192.168.2.1 -p 6380 cluster forget ec5e8ba00823c8d972f8c6c048ea0b964df10f40 OK [root@localhost redis_cluster]# redis-cli -c -h 192.168.2.1 -p 6380 cluster forget c03a5451d189fb10451a58ccc44846a66f86a83f OK [root@localhost redis_cluster]# redis-cli -c -h 192.168.2.1 -p 6380 cluster nodes b6f47e859ad6a3b9b238778d1cf0df4f95710a1f 192.168.2.3:6380@16380 master - 0 1678682062784 12 connected 10923-16383 dee913166afa30b567b151e6e1196847ed0a8a96 192.168.2.2:6381@16381 slave b6f47e859ad6a3b9b238778d1cf0df4f95710a1f 0 1678682061777 12 connected 693d6d17f5860887d3343ebe0083351cd9815c8d 192.168.2.3:6381@16381 slave 9f48a4262474744dc22524b28096ccba111d236d 0 1678682061000 10 connected 961bb9571f4e2f64a320da2631ae8cfc3b78d1e1 192.168.2.1:6381@16381 slave d9ea4f26b2221a5497d78326a7c1aff828e98426 0 1678682059764 11 connected 9f48a4262474744dc22524b28096ccba111d236d 192.168.2.1:6380@16380 myself,master - 0 1678682060000 10 connected 0-5460 d9ea4f26b2221a5497d78326a7c1aff828e98426 192.168.2.2:6380@16380 master - 0 1678682059000 11 connected 5461-10922
3.删除节点数据,关闭
1. pkill redis 2. rm -rf /data/redis_cluster/redis_6390/* 3. rm -rf /data/redis_cluster/redis_6391/*
七,科普
一,常用命令
集群(cluster) CLUSTER INFO 打印集群的信息 CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。 节点(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)。 键 (key) CLUSTER KEYSLOT <key> 计算键 key 应该被放置在哪个槽上。 CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的键值对数量。CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 个 slot 槽中的键。
二,redis运维工具
1.redis管理脚本
[root@redis1 ~]# cat redis_shell.sh #!/bin/bash USAG(){ echo "sh $0 {start|stop|restart|login|ps|tail} PORT" } if [ "$#" = 1 ] then REDIS_PORT='6379' elif [ "$#" = 2 -a -z "$(echo "$2"|sed 's#[0-9]##g')" ] then REDIS_PORT="$2" else USAG exit 0 fi REDIS_IP=$(hostname -I|awk '{print $1}') PATH_DIR=/opt/redis_cluster/redis_${REDIS_PORT}/ PATH_CONF=/opt/redis_cluster/redis_${REDIS_PORT}/conf/redis_${REDIS_PORT}.conf PATH_LOG=/opt/redis_cluster/redis_${REDIS_PORT}/logs/redis_${REDIS_PORT}.log CMD_START(){ redis-server ${PATH_CONF} } CMD_SHUTDOWN(){ redis-cli -c -h ${REDIS_IP} -p ${REDIS_PORT} shutdown } CMD_LOGIN(){ redis-cli -c -h ${REDIS_IP} -p ${REDIS_PORT} } CMD_PS(){ ps -ef|grep redis } CMD_TAIL(){ tail -f ${PATH_LOG} } case $1 in start) CMD_START CMD_PS ;; stop) CMD_SHUTDOWN CMD_PS ;; restart) CMD_START CMD_SHUTDOWN CMD_PS ;; login) CMD_LOGIN ;; ps) CMD_PS ;; tail) CMD_TAIL ;; *) USAG esac
2.数据导出导入工具
刚切换到redis集群的时候肯定会面临数据导入的问题,所以这里推荐使用redis-migrate-tool工具来导入单节点数据到集群里 官方地址: http://www.oschina.net/p/redis-migrate-tool 安装工具 cd /opt/redis_cluster/ git clone https://github.com/vipshop/redis-migrate-tool.git cd redis-migrate-tool/ autoreconf -fvi ./configure make && make install 创建配置文件 [root@redis1 ~]# cat redis_6379_to_6380.conf [source] type: single servers: - 192.168.8.10:6379 [target] type: redis cluster servers: - 192.168.8.10:6380 [common] listen: 0.0.0.0:8888 source_safe: true 生成测试数据 [root@redis1 ~]# cat input_key.sh #!/bin/bash for i in $(seq 1 1000) do redis-cli -c -h redis1 -p 6379 set k_${i} v_${i} && echo "set k_${i} is ok" done 执行导入命令 [root@redis1 ~]# redis-migrate-tool -c redis_6379_to_6380.conf 数据校验 [root@redis1 ~]# redis-migrate-tool -c redis_6379_to_6380.conf -C redis_check
3.分析键值大小
需求背景
redis的内存使用太大键值太多,不知道哪些键值占用的容量比较大,而且在线分析会影响性能.
安装工具
yum install python-pip gcc python-devel cd /opt/ git clone https://github.com/sripathikrishnan/redis-rdb-tools cd redis-rdb-tools python setup.py install
使用方法
1. cd /data/redis_cluster/redis_6380/ 2. rdb -c memory redis_6380.rdb -f redis_6380.rdb.csv
分析rdb并导出
awk -F ',' '{print $4,$2,$3,$1}' redis_6380.rdb.csv |sort > 6380.txt
4.监控过期键
需求背景
因为开发重复提交,导致电商网站优惠卷过期时间失效
问题分析
如果一个键已经设置了过期时间,这时候在set这个键,过期时间就会取消
解决思路
如何在不影响机器性能的前提下批量获取需要监控键过期时间
1.Keys * 查出来匹配的键名。然后循环读取ttl时间
2.scan * 范围查询键名。然后循环读取ttl时间
Keys 重操作,会影响服务器性能,除非是不提供服务的从节点
Scan 负担小,但是需要去多次才能取完,需要写脚本
脚本内容:
cat 01get_key.sh #!/bin/bash key_num=0 key_name.log for line in $(cat key_list.txt) do while true do scan_num=$(redis-cli -h 192.168.47.75 -p 6380 SCAN ${key_num} match ${line}\* count 1000|awk 'NR==1{print $0}') key_name=$(redis-cli -h 192.168.47.75 -p 6380 SCAN ${key_num} match ${line}\* count 1000|awk 'NR>1{print $0}') echo ${key_name}|xargs -n 1 >> key_name.log ((key_num=scan_num)) if [ ${key_num} == 0 ] then break fi done done