手工创建Redis 集群
  Za8XBu7WHFX4 2023年11月22日 27 0

官方的工具redis-trib.rb需要使用ruby,在kylin上不好安装。所以需要手工配置redis cluster。

1.使用配置文件启动redis

按照ip后缀拷贝到机器(90-92)上,每个机器上启动两个示例,分别使用6379和6380端口。
redis启动命令:./redis-server redis.conf

启动失败处理:

  • 错误提示:
1593407:M 07 Jul 2021 10:42:31.637 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1593407:M 07 Jul 2021 10:42:31.638 # WARNING Your kernel has a bug that could lead to data corruption during background save. Please upgrade to the latest stable kernel.
1593407:M 07 Jul 2021 10:42:31.638 # Redis will now exit to prevent data corruption. Note that it is possible to suppress this warning by setting the following config: ignore-warnings ARM64-COW-BUG
  • 错误处理:
# 错误1
执行以下命令
echo 1 > /proc/sys/vm/overcommit_memory
#错误2/3(该bug在kernel5.10以后版本修复,只反针对arm架构的服务器)
redis配置文件中放开配置`ignore-warnings ARM64-COW-BUG`

配置文件里边已经包含了启动集群必须的配置。启动之后可以用ps -ef | grep redis确认redis已经启动,并且可以看到redis进程上有[cluster]标志:

[root@i-biiiaknk ~]# ps -ef | grep redis
root     1706432       1  0 09:55 ?        00:00:13 redis-server 130.1.14.90:6379 [cluster]
root     1706438       1  0 09:55 ?        00:00:08 redis-server 130.1.14.90:6380 [cluster]
root     1790777 1654673  0 11:16 pts/0    00:00:00 grep redis

2.集群握手

只要一个在集群里面的机器meet了其他机器,这个集群里的其他机器也能感知到刚刚meet的集群。所以我们采用如下命令:

redis-cli -h 110.128.6.35 -p 6379 -a rxtd@123 cluster meet 110.128.6.36 6379
redis-cli -h 110.128.6.35 -p 6379 -a rxtd@123 cluster meet 110.128.6.37 6379
redis-cli -h 110.128.6.35 -p 6379 -a rxtd@123 cluster meet 110.128.6.35 6380
redis-cli -h 110.128.6.35 -p 6379 -a rxtd@123 cluster meet 110.128.6.36 6380
redis-cli -h 110.128.6.35 -p 6379 -a rxtd@123 cluster meet 110.128.6.37 6380

这样6个节点都连接上了。可以用cluster nodes命令查看:

[root@i-biiiaknk ~]# redis-cli -h 110.128.6.35 -p 6379 -a rxtd@123 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
b41ca9860dae529f7f577bd76f2f56b22e5e48c4 110.128.6.36:6380@16380 master - 0 1625626481411 4 connected
0b253ee5a7072d5b878e4c1c175a49fe063fdbb5 110.128.6.35:6380@16380 master - 0 1625626479404 3 connected
14ed1ee1382b22b294b7c938ed4f8e552c6ebb88 110.128.6.35:6379@16379 myself,master - 0 1625626480000 1 connected
95469852802e89cbf167e54354ec9df803df03d8 110.128.6.37:6379@16379 master - 0 1625626481000 2 connected
c1aab5839bc4a3879c92789ce99686ddd6daff13 110.128.6.36:6379@16379 master - 0 1625626480507 5 connected
cca133906cfcf416a10199648e62a1ca8fadccfb 110.128.6.37:6380@16380 master - 0 1625626481512 0 connected

3.建立副本

现在所有的机器都是master,我们需要建立副本,把一些机器设置成slave。这需要使用cluster replicate命令:

redis-cli -h 110.128.6.35 -p 6380 -a rxtd@123 cluster replicate 14ed1ee1382b22b294b7c938ed4f8e552c6ebb88
redis-cli -h 110.128.6.36 -p 6380 -a rxtd@123 cluster replicate c1aab5839bc4a3879c92789ce99686ddd6daff13
redis-cli -h 110.128.6.37 -p 6380 -a rxtd@123 cluster replicate 95469852802e89cbf167e54354ec9df803df03d8

注意这里使用的是节点id,一定要用cluster nodes命令获得正确的节点id。另外,cluster replicate命令是把当前节点设置成指定节点的slave,这个不要搞反了。
设置完成之后可以再使用cluster nodes命令确认三个6380的节点都变成slave了,并且指向正确的master节点。

4.分配slot

redis cluster一共有16383个槽,需要分配到三个节点。我们需要写一个脚本addslots.sh

start=$1
end=$2
ip=$3
port=$4
for slot in `seq ${start} ${end}`
do
        echo "slot:${slot}"
        redis-cli -h ${ip} -p ${port} -a rxtd@123 cluster addslots ${slot}
done

然后使用这个脚本分配slot:

./addslots.sh 0 5461 110.128.6.35 6379
./addslots.sh 5462 10922 110.128.6.36 6379
./addslots.sh 10923 16383 110.128.6.37 6379

注意,一定要在master节点上add,要不然slot的配置不能同步到所有的节点上去!!!不要问我怎么知道的,那是一个悲伤的故事。。。
可以使用cluster nodes命令来确定master节点。
如果不小心加错了slot,可以在添加的节点上使用cluster delslots命令删除。如果用上边的脚本加了很多,那么可以相应的写一个delslot.sh来删除:

start=$1end=$2ip=$3port=$4for slot in `seq ${start} ${end}`do        echo "slot:${slot}"        redis-cli -h ${ip} -p ${port} -a rxtd@123 cluster delslots ${slot}done

这样设置完毕之后,用cluster info命令就能看到集群已经能正常工作了。

[root@i-biiiaknk ~]# redis-cli -h 110.128.6.35 -p 6379 -a rxtd@123 cluster infoWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.cluster_state:okcluster_slots_assigned:16384cluster_slots_ok:16384cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:6cluster_size:3cluster_current_epoch:5cluster_my_epoch:1cluster_stats_messages_ping_sent:1347cluster_stats_messages_pong_sent:1360cluster_stats_messages_meet_sent:5cluster_stats_messages_sent:2712cluster_stats_messages_ping_received:1360cluster_stats_messages_pong_received:1352cluster_stats_messages_received:2712

5.配置文件

  • 110.128.6.35_6379
bind 110.128.6.35
port 6379
daemonize yes
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/dbdata/redis/log/redis-6379.log"
save 900 1
save 300 10
save 60 10000
dbfilename "dump_6379.rdb"
dir "/dbdata/redis/file"
maxclients 10000
maxmemory 100000000
appendonly yes
appendfilename "appendonly_6379.aof"
appendfsync everysec
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
cluster-enabled yes
cluster-config-file "nodes-6379.conf"
cluster-node-timeout 5000
#禁用清空所有记录
rename-command FLUSHALL ""
#禁用清空数据库
rename-command FLUSHDB ""
#禁用客户端连接后可查看所有存在的键
rename-command KEYS ""
# Generated by CONFIG REWRITE
notify-keyspace-events "gxE"
masterauth "rxtd@123"
requirepass "rxtd@123"
ignore-warnings ARM64-COW-BUG
  • 110.128.6.35_6380
bind 110.128.6.35
port 6380
daemonize yes
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/dbdata/redis/log/redis-6380.log"
save 900 1
save 300 10
save 60 10000
dbfilename "dump_6380.rdb"
dir "/dbdata/redis/file"
maxclients 10000
maxmemory 100000000
appendonly yes
appendfilename "appendonly_6380.aof"
appendfsync everysec
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
cluster-enabled yes
cluster-config-file "nodes-6380.conf"
cluster-node-timeout 5000
#禁用清空所有记录
rename-command FLUSHALL ""
#禁用清空数据库
rename-command FLUSHDB ""
#禁用客户端连接后可查看所有存在的键
rename-command KEYS ""
# Generated by CONFIG REWRITE
notify-keyspace-events "gxE"
masterauth "rxtd@123"
requirepass "rxtd@123"
ignore-warnings ARM64-COW-BUG
  • 110.128.6.36_6379
bind 110.128.6.36
port 6379
daemonize yes
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/dbdata/redis/log/redis-6379.log"
save 900 1
save 300 10
save 60 10000
dbfilename "dump_6379.rdb"
dir "/dbdata/redis/file"
maxclients 10000
maxmemory 100000000
appendonly yes
appendfilename "appendonly_6379.aof"
appendfsync everysec
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
cluster-enabled yes
cluster-config-file "nodes-6379.conf"
cluster-node-timeout 5000
#禁用清空所有记录
rename-command FLUSHALL ""
#禁用清空数据库
rename-command FLUSHDB ""
#禁用客户端连接后可查看所有存在的键
rename-command KEYS ""
# Generated by CONFIG REWRITE
notify-keyspace-events "gxE"
masterauth "rxtd@123"
requirepass "rxtd@123"
ignore-warnings ARM64-COW-BUG
  • 110.128.6.36_6380
bind 110.128.6.36
port 6380
daemonize yes
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/dbdata/redis/log/redis-6380.log"
save 900 1
save 300 10
save 60 10000
dbfilename "dump_6380.rdb"
dir "/dbdata/redis/file"
maxclients 10000
maxmemory 100000000
appendonly yes
appendfilename "appendonly_6380.aof"
appendfsync everysec
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
cluster-enabled yes
cluster-config-file "nodes-6380.conf"
cluster-node-timeout 5000
#禁用清空所有记录
rename-command FLUSHALL ""
#禁用清空数据库
rename-command FLUSHDB ""
#禁用客户端连接后可查看所有存在的键
rename-command KEYS ""
# Generated by CONFIG REWRITE
notify-keyspace-events "gxE"
masterauth "rxtd@123"
requirepass "rxtd@123"
ignore-warnings ARM64-COW-BUG
  • 110.128.6.37_6379
bind 110.128.6.37
port 6379
daemonize yes
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/dbdata/redis/log/redis-6379.log"
save 900 1
save 300 10
save 60 10000
dbfilename "dump_6379.rdb"
dir "/dbdata/redis/file"
maxclients 10000
maxmemory 100000000
appendonly yes
appendfilename "appendonly_6379.aof"
appendfsync everysec
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
cluster-enabled yes
cluster-config-file "nodes-6379.conf"
cluster-node-timeout 5000
#禁用清空所有记录
rename-command FLUSHALL ""
#禁用清空数据库
rename-command FLUSHDB ""
#禁用客户端连接后可查看所有存在的键
rename-command KEYS ""
# Generated by CONFIG REWRITE
notify-keyspace-events "gxE"
masterauth "rxtd@123"
requirepass "rxtd@123"
ignore-warnings ARM64-COW-BUG
  • 110.128.6.37_6380
bind 110.128.6.37
port 6380
daemonize yes
pidfile "/var/run/redis_6380.pid"
loglevel notice
logfile "/dbdata/redis/log/redis-6380.log"
save 900 1
save 300 10
save 60 10000
dbfilename "dump_6380.rdb"
dir "/dbdata/redis/file"
maxclients 10000
maxmemory 100000000
appendonly yes
appendfilename "appendonly_6380.aof"
appendfsync everysec
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
cluster-enabled yes
cluster-config-file "nodes-6380.conf"
cluster-node-timeout 5000
#禁用清空所有记录
rename-command FLUSHALL ""
#禁用清空数据库
rename-command FLUSHDB ""
#禁用客户端连接后可查看所有存在的键
rename-command KEYS ""
# Generated by CONFIG REWRITE
notify-keyspace-events "gxE"
masterauth "rxtd@123"
requirepass "rxtd@123"
ignore-warnings ARM64-COW-BUG
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月22日 0

暂无评论

推荐阅读
  xaeiTka4h8LY   2024年05月31日   38   0   0 MySQL数据库
  xaeiTka4h8LY   2024年05月31日   29   0   0 Dockerredis
  xaeiTka4h8LY   2024年05月31日   39   0   0 nosqlredis
  xaeiTka4h8LY   2024年05月17日   47   0   0 数据库SQL
  xaeiTka4h8LY   2024年05月31日   36   0   0 数据库mongodb
Za8XBu7WHFX4
作者其他文章 更多