公司使用自己搭建的redis集群。考虑到易于维护、成本等因素,考虑将redis迁移到阿里云。
总结redis使用经验

查询原redis内存用量

1
2
3
4
5
6
7
8
集群信息
cluster info

集群节点
cluster nodes

查看内存等
info

使用脚本查询, 源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
REDIS_CLI=${REDIS_CLI:-redis-cli}
REDIS_IP=${REDIS_IP:-127.0.0.1}
REDIS_PORT=${REDIS_PORT:-6379}

function usage()
{
echo "Usage: `basename $0` redis_node [redis-password]"
echo "Example1: `basename $0` 127.0.0.1:6379"
echo "Example2: `basename $0` 127.0.0.1:6379 redis-password"
}

# with two parameters:
# 1) single redis node
# 2) redis password
if test $# -eq 0 -o $# -gt 2; then
usage
exit 1
fi

# 如果有两个参数,则第2个参数为密码
redis_password=
if test $# -eq 2; then
redis_password="$2"
fi

eval $(echo "$1" | awk -F[\:] '{ printf("REDIS_IP=%s\nREDIS_PORT=%s\n",$1,$2) }')
if test -z "$REDIS_IP" -o -z "$REDIS_PORT"; then
echo "Parameter error"
usage
exit 1
fi

# 确保redis-cli可用
which "$REDIS_CLI" > /dev/null 2>&1
if test $? -ne 0; then
echo "\`redis-cli\` not exists or not executable"
exit 1
fi

if test -z "$redis_password"; then
redis_nodes=`redis-cli -h $REDIS_IP -p $REDIS_PORT cluster nodes \
| awk -F[\ \:\@] '!/ERR/{ printf("%s:%s\n",$2,$3); }'`
else
redis_nodes=`redis-cli --no-auth-warning -a "$redis_password" -h $REDIS_IP -p $REDIS_PORT cluster nodes \
| awk -F[\ \:\@] '!/ERR/{ printf("%s:%s\n",$2,$3); }'`
fi
if test -z "$redis_nodes"; then
# standlone
#$REDIS_CLI -h $REDIS_IP -p $REDIS_PORT FLUSHALL
$REDIS_CLI -h $REDIS_IP -p $REDIS_PORT INFO
else
# cluster
for redis_node in $redis_nodes;
do
if test ! -z "$redis_node"; then
eval $(echo "$redis_node" | awk -F[\:] '{ printf("redis_node_ip=%s\nredis_node_port=%s\n",$1,$2) }')

if test ! -z "$redis_node_ip" -a ! -z "$redis_node_port"; then
if test -z "$redis_password"; then
items=(`$REDIS_CLI -h $redis_node_ip -p $redis_node_port INFO MEMORY 2>&1 | tr '\r' ' '`)
else
items=(`$REDIS_CLI --no-auth-warning -a "$redis_password" -h $redis_node_ip -p $redis_node_port INFO MEMORY 2>&1 | tr '\r' ' '`)
fi

used_memory_human=0
used_memory_rss_human=0
used_memory_peak_human=0
maxmemory_human=0
total_system_memory_human=0
for item in "${items[@]}"
do
eval $(echo "$item" | awk -F[\:] '{ printf("name=%s\nvalue=%s\n",$1,$2) }')

if test "$name" = "used_memory_human"; then
used_memory_human=$value
elif test "$name" = "used_memory_rss_human"; then
used_memory_rss_human=$value
elif test "$name" = "used_memory_peak_human"; then
used_memory_peak_human=$value
elif test "$name" = "maxmemory_human"; then
maxmemory_human=$value
elif test "$name" = "total_system_memory_human"; then
total_system_memory_human=$value
fi
done

echo -e "[\033[1;33m${redis_node_ip}:${redis_node_port}\033[m]\tVIRT: \033[0;32;32m$used_memory_human\033[m\tRSS: \033[0;32;32m$used_memory_rss_human\033[m\tPeak: \033[0;32;32m$used_memory_peak_human\033[m\tMax: \033[0;32;32m$maxmemory_human\033[m\tSystem: \033[0;32;32m$total_system_memory_human\033[m"
fi
fi
done
fi

可以查询到集群的内存使用量,方便确认需要购买多大的容量

购买阿里云云数据库Redis版

购买阿里云集群,可以选择单机版、主从、集群版本

可以包月,或者按小时付费

image-20211125181827949

数据同步

参考文章 https://help.aliyun.com/document_detail/126604.htm?spm=a2c4g.11186623.0.0.53e7eaceiFjtzw#concept-228260

阿里云提供了数据同步服务,可以将数据实时同步到新的服务中

  1. 购买数据同步DTS服务
  2. 创建同步服务实例
  3. 开启同步

image-20211125181442715

开启同步时,需要预检查。注意源,目的的服务器的连接,端口,白名单等配置

image-20211125181339133服务迁移

在阿里云管理界面,可以看到redis的内网连接地址、端口。由于才用了集群模式,连接地址已实现了负载均衡,也可以申请直连访问,速度会略快一点。

image-20211125182255971

在自己的各个服务中替换点原集群地址。重启docker服务,测试完成就ok。

1
2
3
4
5
6
# 查看当前的redis连接数
info clients
# 客户端列表
client list
# 查询redis允许的最大连接数
config get maxclients

使用相关命令,可以查看是否还有服务连接原地址。

1
id=6 addr=172.18.1.4:24672 fd=17 name= age=19821204 idle=1 flags=S db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=replconf

参考文章

从自建Redis集群同步至Redis集群实例

[Github redis-tools]https://github.com/eyjian/redis-tools