文档课题:Redis集群添加master到当前集群,并重新分槽.
操作系统:rhel 7.9 64位
数据库:redis 6.2.6
环境说明:
第1台机器:主机名leo-redis626-a 192.168.133.100 8001端口 8002端口
第2台机器:主机名leo-redis626-b 192.168.133.101 8001端口 8002端口
第3台机器:主机名leo-redis626-c 192.168.133.102 8001端口 8002端口
前提条件:三主三从集群环境搭建完成.
1、配置文件
1.1、修改配置文件
[root@leo-redis626-a 8003]# cat /usr/local/redis/redis-cluster/8001/redis.conf
port 8001
daemonize yes
pidfile "/var/run/redis_8001.pid"
#指定数据文件存放位置,必须要指定不同的目录位置,不然会丢失数据
dir /usr/local/redis/redis-cluster/8001/
#启动集群模式
cluster-enabled yes
#集群节点信息文件,这里800x最好和port对应上
cluster-config-file nodes-8001.conf
# 节点离线的超时时间
cluster-node-timeout 5000
#去掉bind绑定访问ip信息
#bind 127.0.0.1
#关闭保护模式
protected-mode no
#启动AOF文件
appendonly yes
#如果要设置密码需要增加如下配置:
#设置redis访问密码
#requirepass root123
#设置集群节点间访问密码,跟上面一致
[root@leo-redis626-a redis-cluster]# mkdir –p /usr/local/redis/redis-cluster/8003
[root@leo-redis626-a 8001]# cp /usr/local/redis/redis-cluster/8001/redis.conf /usr/local/redis/redis-cluster/8003
[root@leo-redis626-a 8003]# pwd
/usr/local/redis/redis-cluster/8003
[root@leo-redis626-a 8003]# vi redis.conf
批量修改:
:%s/8001/8003/g
1.2、传递配置文件
--将leo-redis626-a修改后的配置文件传递到leo-redis626-b,leo-redis626-c.
--在另外两台主机创建目录.
[root@leo-redis626-b 8001]# mkdir -p /usr/local/redis/redis-cluster/8003
[root@leo-redis626-c 8001]# mkdir -p /usr/local/redis/redis-cluster/8003
--传递
[root@leo-redis626-a 8003]# scp redis.conf [email protected]:/usr/local/redis/redis-cluster/8003
[root@leo-redis626-a 8003]# scp redis.conf [email protected]:/usr/local/redis/redis-cluster/8003
2、启动实例
--三台主机分别启动新实例.
[root@leo-redis626-a 8003]# redis-server /usr/local/redis/redis-cluster/8003/redis.conf
[root@leo-redis626-a 8003]# ps -ef|grep redis
avahi 870 1 0 12:50 ? 00:00:00 avahi-daemon: running [leo-redis626-a.local]
root 58863 1 0 16:23 ? 00:00:26 redis-server *:8001 [cluster]
root 58869 1 0 16:24 ? 00:00:23 redis-server *:8002 [cluster]
root 59729 57102 0 17:38 pts/1 00:00:00 redis-cli -c -h 192.168.133.100 -p 8001
root 59894 1 0 17:53 ? 00:00:00 redis-server *:8003 [cluster]
root 59900 56861 0 17:53 pts/0 00:00:00 grep --color=auto redis
[root@leo-redis626-b 8003]# redis-server /usr/local/redis/redis-cluster/8003/redis.conf
[root@leo-redis626-c 8003]# redis-server /usr/local/redis/redis-cluster/8003/redis.conf
3、向集群添加实例
语法:
redis-cli --cluster add-node new-master-host:new-master-port now-host:now-port
实际操作:
[root@leo-redis626-a 8003]# redis-cli --cluster add-node 192.168.133.100:8003 192.168.133.100:8001
>>> Adding node 192.168.133.100:8003 to cluster 192.168.133.100:8001
>>> Performing Cluster Check (using node 192.168.133.100:8001)
M: c036f0322c2f8bdd04b1f156f8ff7c29804eb28b 192.168.133.100:8001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 5d9ff4989ef75877c6683ece2cc39f22fa4160e6 192.168.133.102:8002
slots: (0 slots) slave
replicates 6d9ca761fc09c83b6a3c2273a36b565c4c525ac3
S: 6bf0eff5c1874da48cb64df57accac8eaf7aa57d 192.168.133.100:8002
slots: (0 slots) slave
replicates 81c45df99418144728954cda61dfa9fb3e1c5b3f
M: 6d9ca761fc09c83b6a3c2273a36b565c4c525ac3 192.168.133.101:8001
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 81c45df99418144728954cda61dfa9fb3e1c5b3f 192.168.133.102:8001
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 9f2b2480fecb0a193fdb2feed8c507c7c664cd74 192.168.133.101:8002
slots: (0 slots) slave
replicates c036f0322c2f8bdd04b1f156f8ff7c29804eb28b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.133.100:8003 to make it join the cluster.
[OK] New node added correctly.
4、查看最新集群状态
[root@leo-redis626-b 8001]# redis-cli -c -p 8001
127.0.0.1:8001> cluster nodes
81c45df99418144728954cda61dfa9fb3e1c5b3f 192.168.133.102:8001@18001 master - 0 1681120683000 5 connected 10923-16383
90a666a698fb0042645a10ebe0dde1d3dcbb8215 192.168.133.100:8003@18003 master - 0 1681120682687 0 connected
6bf0eff5c1874da48cb64df57accac8eaf7aa57d 192.168.133.100:8002@18002 slave 81c45df99418144728954cda61dfa9fb3e1c5b3f 0 1681120682000 5 connected
c036f0322c2f8bdd04b1f156f8ff7c29804eb28b 192.168.133.100:8001@18001 master - 0 1681120683333 1 connected 0-5460
5d9ff4989ef75877c6683ece2cc39f22fa4160e6 192.168.133.102:8002@18002 slave 6d9ca761fc09c83b6a3c2273a36b565c4c525ac3 0 1681120682256 3 connected
6d9ca761fc09c83b6a3c2273a36b565c4c525ac3 192.168.133.101:8001@18001 myself,master - 0 1681120682000 3 connected 5461-10922
9f2b2480fecb0a193fdb2feed8c507c7c664cd74 192.168.133.101:8002@18002 slave c036f0322c2f8bdd04b1f156f8ff7c29804eb28b 0 1681120683333 1 connected
5、重新分槽
说明:分槽是从具有槽的master中划分一部分给其他master,过程中不创建新的槽.
语法:redis-cli --cluster reshard new-master-host:new-master:port --cluster-from srcmaster-id1,src-master-id2,src-master-idn --cluster-to target-master-id --cluster-slots slots
实际操作:
[root@leo-redis626-a 8003]# redis-cli --cluster reshard 192.168.133.100:8003 --cluster-from c036f0322c2f8bdd04b1f156f8ff7c29804eb28b, 6d9ca761fc09c83b6a3c2273a36b565c4c525ac3, 81c45df99418144728954cda61dfa9fb3e1c5b3f --cluster-to 90a666a698fb0042645a10ebe0dde1d3dcbb8215 --cluster-slots 1000
6、分槽后确认
[root@leo-redis626-a 8003]# redis-cli -p 8001 -c
127.0.0.1:8001> cluster nodes
90a666a698fb0042645a10ebe0dde1d3dcbb8215 192.168.133.100:8003@18003 master - 0 1681121192000 7 connected 0-999
5d9ff4989ef75877c6683ece2cc39f22fa4160e6 192.168.133.102:8002@18002 slave 6d9ca761fc09c83b6a3c2273a36b565c4c525ac3 0 1681121193000 3 connected
6bf0eff5c1874da48cb64df57accac8eaf7aa57d 192.168.133.100:8002@18002 slave 81c45df99418144728954cda61dfa9fb3e1c5b3f 0 1681121194020 5 connected
c036f0322c2f8bdd04b1f156f8ff7c29804eb28b 192.168.133.100:8001@18001 myself,master - 0 1681121193000 1 connected 1000-5460
6d9ca761fc09c83b6a3c2273a36b565c4c525ac3 192.168.133.101:8001@18001 master - 0 1681121192524 3 connected 5461-10922
81c45df99418144728954cda61dfa9fb3e1c5b3f 192.168.133.102:8001@18001 master - 0 1681121192525 5 connected 10923-16383
9f2b2480fecb0a193fdb2feed8c507c7c664cd74 192.168.133.101:8002@18002 slave c036f0322c2f8bdd04b1f156f8ff7c29804eb28b 0 1681121193000 1 connected
注意:此处并没有平均分配槽.
--在分配1000个槽,此时将srcmaster-id1之间的空格去掉.
[root@leo-redis626-a 8003]# redis-cli --cluster reshard 192.168.133.100:8003 --cluster-from c036f0322c2f8bdd04b1f156f8ff7c29804eb28b,6d9ca761fc09c83b6a3c2273a36b565c4c525ac3,81c45df99418144728954cda61dfa9fb3e1c5b3f --cluster-to 90a666a698fb0042645a10ebe0dde1d3dcbb8215 --cluster-slots 1000
[root@leo-redis626-a 8003]# redis-cli -p 8001 -c
127.0.0.1:8001> cluster nodes
90a666a698fb0042645a10ebe0dde1d3dcbb8215 192.168.133.100:8003@18003 master - 0 1681122978000 7 connected 0-1288 5461-5816 10923-11276
5d9ff4989ef75877c6683ece2cc39f22fa4160e6 192.168.133.102:8002@18002 slave 6d9ca761fc09c83b6a3c2273a36b565c4c525ac3 0 1681122978000 3 connected
6bf0eff5c1874da48cb64df57accac8eaf7aa57d 192.168.133.100:8002@18002 slave 81c45df99418144728954cda61dfa9fb3e1c5b3f 0 1681122978506 5 connected
c036f0322c2f8bdd04b1f156f8ff7c29804eb28b 192.168.133.100:8001@18001 myself,master - 0 1681122977000 1 connected 1289-5460
6d9ca761fc09c83b6a3c2273a36b565c4c525ac3 192.168.133.101:8001@18001 master - 0 1681122978000 3 connected 5817-10922
81c45df99418144728954cda61dfa9fb3e1c5b3f 192.168.133.102:8001@18001 master - 0 1681122978947 5 connected 11277-16383
9f2b2480fecb0a193fdb2feed8c507c7c664cd74 192.168.133.101:8002@18002 slave c036f0322c2f8bdd04b1f156f8ff7c29804eb28b 0 1681122977516 1 connected
说明:如上所示,槽平均分配到新增节点.
参考网址:https://www.bilibili.com/video/BV18K4y197w1/?vd_source=8c872e2fd1d99229b38a73ed6718b776
标签:redis,Redis,分槽,192.168,leo,cluster,集群,8001,8003
From: https://blog.51cto.com/u_12991611/6181365