redis三主三从集群快速搭建

                    redis集群快速搭建

 

1 本文以redis6.2.6版本为例,快速搭建一套三主三从的redis集群,已经提前将脚本写好,只需要在节点上面进行执行即可安装

#!/bin/bash

install_redis() {
    # 解压 Redis 源码包
    tar -zxvf redis-6.2.6.tar.gz
    
    # 创建目录结构
    mkdir -p /opt/redis/cluster/{conf,data,log,pid}
    
    
    # 编译并安装 Redis
    cd redis-6.2.6 && make && make install PREFIX=/opt/redis
}

generate_redis_config() {
    local redis_host=$1
    local redis_dir="/opt/redis/cluster"

    for port in 6379 6380; do
        mkdir -p /opt/redis/cluster/data/${port}
        config_file="$redis_dir/conf/redis_${port}.conf"
        cat <<EOF > "$config_file"
# Redis 配置文件,端口 ${port}
daemonize yes 
bind $redis_host
dir $redis_dir/data/${port}/
pidfile $redis_dir/pid/${port}.pid 
logfile $redis_dir/log/redis-cluster-${port}.log 
port ${port}
cluster-enabled yes 
cluster-config-file $redis_dir/conf/redis-cluster-${port}.conf 
cluster-node-timeout 10000 
appendonly yes 
requirepass Ebupt#202401f
masterauth Ebupt#202401f
EOF
        echo "已生成配置文件: $config_file"
    done
}

start_redis_node() {
    for port in 6379 6380; do
        /opt/redis/bin/redis-server /opt/redis/cluster/conf/redis_${port}.conf
        echo "已启动配置文件: /opt/redis/cluster/conf/redis_${port}.conf 的节点"
    done
}

main() {
    # 安装 Redis
    install_redis
    
    # 生成 Redis 配置文件
    generate_redis_config $1
    
    # 启动 Redis 集群节点
    start_redis_node

    # 生成集群
    # yes|/opt/redis/bin/redis-cli --cluster create $1:6379 $1:6380 $1:6381 $1:6382 $1:6383 $1:6384 --cluster-replicas 1 -a
Ebupt#202401f
} main "$@"

2 将该脚本以及redis-6.2.6.tar.gz同时放置到服务器任意目录之后执行即可,这个操作需要在配置的三台上面进行配置

bash install_redis_cluster.sh xx.xx.xx.xx
#xx.xx.xx.xx为主机的IP地址

3 安装包

  链接:https://pan.baidu.com/s/1GCFILWqIrUMm4HpUFbLV_g
  提取码:hpb9

4 组成集群

/opt/redis/bin/redis-cli --cluster create host1:6379 host2:6380 host2:6379 host3:6380 host3:6379 host1:6380 --cluster-replicas 1 -a Ebupt#202401f
#host1
#host2
#host3
分别为需要部署的主机123

 5 集群扩缩容或者数据迁移

  已经部署完成了一个三主三从的redis集群,配置如下所示

0927e13c0fbb246d460df7137b9f3768901add9b 10.196.52.65:6379@16379 master - 0 1715416447083 1 connected 0-5460
c1f5acfe0347804e0b57c1e20e6c3480b3eac789 10.196.52.67:6379@16379 myself,slave bb1c16862396793a606ed8c0698f5ab7e8f9aeb6 0 1715416446000 2 connected
3ce35d77a25660a093c2d36cce0c95351705259f 10.196.52.65:6380@16380 master - 0 1715416446000 7 connected 10923-16383
ae2370096014cc8b9a3d6804906d34679f519f3d 10.196.52.66:6379@16379 slave 0927e13c0fbb246d460df7137b9f3768901add9b 0 1715416446882 1 connected
4511548d363153bb5b258df324a87538d222b100 10.196.52.67:6380@16380 slave 3ce35d77a25660a093c2d36cce0c95351705259f 0 1715416447885 7 connected
bb1c16862396793a606ed8c0698f5ab7e8f9aeb6 10.196.52.66:6380@16380 master - 0 1715416448888 2 connected 5461-10922

  5.1 向集群节点添加数据

[root@yp-bz-yj4a-04 fourb]# /opt/redis/bin/redis-cli -c -h 10.196.52.67 -p 6379 -a Ebupt#202401f set 4a 4aValue
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK

[root@yp-bz-yj4a-04 fourb]# /opt/redis/bin/redis-cli -c -h 10.196.52.67 -p 6379 -a Ebupt#202401f set 4b 4bValue Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. OK [root@yp-bz-yj4a-04 fourb]# /opt/redis/bin/redis-cli -c -h 10.196.52.67 -p 6379 -a Ebupt#202401f set 4c 4cValue Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. OK [root@yp-bz-yj4a-04 fourb]# /opt/redis/bin/redis-cli -c -h 10.196.52.67 -p 6379 -a Ebupt#202401f set 4d 4dValue Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. OK

  查看数据是不是根据slot平铺在集群上面

[root@yp-bz-yj4a-04 fourb]# /opt/redis/bin/redis-cli -c -h 10.196.52.67 -p 6379 -a Ebupt#202401f cluster keyslot 4a
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(integer) 13782
[root@yp-bz-yj4a-04 fourb]# /opt/redis/bin/redis-cli -c -h 10.196.52.67 -p 6379 -a Ebupt#202401f cluster keyslot 4b Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. (integer) 1461

  [root@yp-bz-yj4a-05 fourb]# /opt/redis/bin/redis-cli -c -h 10.196.52.67 -p 6379 -a Ebupt#202401f cluster keyslot 4c
  Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  (integer) 5524


[root@yp-bz-yj4a-04 fourb]# /opt/redis/bin/redis-cli -c -h 10.196.52.67 -p 6379 -a Ebupt#202401f cluster keyslot 4d
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(integer) 9587

  查看各个节点数据情况

[root@yp-bz-yj4a-05 fourb]# /opt/redis/bin/redis-cli  -h 10.196.52.65 -p 6380 -a Ebupt#202401f
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.196.52.65:6380> keys *
1) "4a"
10.196.52.65:6380> 
[root@yp-bz-yj4a-05 fourb]# /opt/redis/bin/redis-cli  -h 10.196.52.65 -p 6379 -a Ebupt#202401f
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.196.52.65:6379> keys *
1) "4b"
10.196.52.65:6379> 
[root@yp-bz-yj4a-05 fourb]# /opt/redis/bin/redis-cli  -h 10.196.52.66 -p 6379 -a Ebupt#202401f
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.196.52.66:6379> 
10.196.52.66:6379> keys *
1) "4b"
10.196.52.66:6379> 
[root@yp-bz-yj4a-05 fourb]# /opt/redis/bin/redis-cli  -h 10.196.52.66 -p 6380 -a Ebupt#202401f
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.196.52.66:6380> keys *
1) "4d"
2) "4c"
10.196.52.66:6380> 
[root@yp-bz-yj4a-05 fourb]# /opt/redis/bin/redis-cli  -h 10.196.52.67 -p 6379 -a Ebupt#202401f
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.196.52.67:6379> keys *
1) "4d"
2) "4c"

  可以发现所有的数据全部平铺在整个集群上面并且各个主节点和从节点的数据一致

6 扩容步骤

  向该集群重新扩容一主一从,并且把集群的slot迁移上去,已经通过前面的脚本提前将redis集群的2个节点部署完成

/opt/redis/bin/redis-cli --cluster add-node 10.196.52.64:6379 10.196.52.65:6379 -a Ebupt#202401f
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 10.196.52.64:6379 to cluster 10.196.52.65:6379
>>> Performing Cluster Check (using node 10.196.52.65:6379)
M: 0927e13c0fbb246d460df7137b9f3768901add9b 10.196.52.65:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 3ce35d77a25660a093c2d36cce0c95351705259f 10.196.52.65:6380
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: c1f5acfe0347804e0b57c1e20e6c3480b3eac789 10.196.52.67:6379
   slots: (0 slots) slave
   replicates bb1c16862396793a606ed8c0698f5ab7e8f9aeb6
S: ae2370096014cc8b9a3d6804906d34679f519f3d 10.196.52.66:6379
   slots: (0 slots) slave
   replicates 0927e13c0fbb246d460df7137b9f3768901add9b
M: bb1c16862396793a606ed8c0698f5ab7e8f9aeb6 10.196.52.66:6380
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 4511548d363153bb5b258df324a87538d222b100 10.196.52.67:6380
   slots: (0 slots) slave
   replicates 3ce35d77a25660a093c2d36cce0c95351705259f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 10.196.52.64:6379 to make it join the cluster.
[OK] New node added correctly.

  提示已经加入成功

  在将另外一个redis节点以该节点的从节点的角色形式加入该集群

[root@yp-bz-yj4a-05 fourb]# /opt/redis/bin/redis-cli --cluster add-node 10.196.52.64:6380 10.196.52.65:6379 -a Ebupt#202401f --cluster-slave --cluster-master-id 690583bf6464147c59b8b6be5051fc1ce6b1b29b
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 10.196.52.64:6380 to cluster 10.196.52.65:6379
>>> Performing Cluster Check (using node 10.196.52.65:6379)
M: 0927e13c0fbb246d460df7137b9f3768901add9b 10.196.52.65:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 690583bf6464147c59b8b6be5051fc1ce6b1b29b 10.196.52.64:6379
   slots: (0 slots) master
M: 3ce35d77a25660a093c2d36cce0c95351705259f 10.196.52.65:6380
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: c1f5acfe0347804e0b57c1e20e6c3480b3eac789 10.196.52.67:6379
   slots: (0 slots) slave
   replicates bb1c16862396793a606ed8c0698f5ab7e8f9aeb6
S: ae2370096014cc8b9a3d6804906d34679f519f3d 10.196.52.66:6379
   slots: (0 slots) slave
   replicates 0927e13c0fbb246d460df7137b9f3768901add9b
M: bb1c16862396793a606ed8c0698f5ab7e8f9aeb6 10.196.52.66:6380
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 4511548d363153bb5b258df324a87538d222b100 10.196.52.67:6380
   slots: (0 slots) slave
   replicates 3ce35d77a25660a093c2d36cce0c95351705259f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 10.196.52.64:6380 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of 10.196.52.64:6379.
[OK] New node added correctly.

  提示加入成功,可以看到集群已经变成四主四从了,但是此时新加入的节点没有slot,无法存储数据

0927e13c0fbb246d460df7137b9f3768901add9b 10.196.52.65:6379@16379 master - 0 1715420336980 1 connected 0-5460
c1f5acfe0347804e0b57c1e20e6c3480b3eac789 10.196.52.67:6379@16379 myself,slave bb1c16862396793a606ed8c0698f5ab7e8f9aeb6 0 1715420333000 2 connected
3ce35d77a25660a093c2d36cce0c95351705259f 10.196.52.65:6380@16380 master - 0 1715420336000 7 connected 10923-16383
ae2370096014cc8b9a3d6804906d34679f519f3d 10.196.52.66:6379@16379 slave 0927e13c0fbb246d460df7137b9f3768901add9b 0 1715420335000 1 connected
eeffd828d55d8f321c709ab16b91e13086c9d0ff 10.196.52.64:6380@16380 slave 690583bf6464147c59b8b6be5051fc1ce6b1b29b 0 1715420337983 0 connected 
4511548d363153bb5b258df324a87538d222b100 10.196.52.67:6380@16380 slave 3ce35d77a25660a093c2d36cce0c95351705259f 0 1715420336000 7 connected
690583bf6464147c59b8b6be5051fc1ce6b1b29b 10.196.52.64:6379@16379 master - 0 1715420336000 0 connected  #####新加入的节点,无slot
bb1c16862396793a606ed8c0698f5ab7e8f9aeb6 10.196.52.66:6380@16380 master - 0 1715420337000 2 connected 5461-10922

  迁移slot

/opt/redis/bin/redis-cli --cluster reshard 10.196.52.65:6379 -a Ebupt#202401
之后依次选择
接受的节点
slot数量
数据的节点
之后进行迁移
0927e13c0fbb246d460df7137b9f3768901add9b 10.196.52.65:6379@16379 master - 0 1715421403000 1 connected 0-5460
c1f5acfe0347804e0b57c1e20e6c3480b3eac789 10.196.52.67:6379@16379 myself,slave bb1c16862396793a606ed8c0698f5ab7e8f9aeb6 0 1715421402000 2 connected
3ce35d77a25660a093c2d36cce0c95351705259f 10.196.52.65:6380@16380 master - 0 1715421404392 7 connected 10923-16383
ae2370096014cc8b9a3d6804906d34679f519f3d 10.196.52.66:6379@16379 slave 0927e13c0fbb246d460df7137b9f3768901add9b 0 1715421406000 1 connected
eeffd828d55d8f321c709ab16b91e13086c9d0ff 10.196.52.64:6380@16380 slave 690583bf6464147c59b8b6be5051fc1ce6b1b29b 0 1715421406398 0 connected
4511548d363153bb5b258df324a87538d222b100 10.196.52.67:6380@16380 slave 3ce35d77a25660a093c2d36cce0c95351705259f 0 1715421404000 7 connected
690583bf6464147c59b8b6be5051fc1ce6b1b29b 10.196.52.64:6379@16379 master - 0 1715421404000 0 connected
bb1c16862396793a606ed8c0698f5ab7e8f9aeb6 10.196.52.66:6380@16380 master - 0 1715421403000 2 connected 5461-10922
10.196.52.67:6379> cluster nodes
0927e13c0fbb246d460df7137b9f3768901add9b 10.196.52.65:6379@16379 master - 0 1715422069775 1 connected 0-5460
c1f5acfe0347804e0b57c1e20e6c3480b3eac789 10.196.52.67:6379@16379 myself,slave 690583bf6464147c59b8b6be5051fc1ce6b1b29b 0 1715422065000 8 connected
3ce35d77a25660a093c2d36cce0c95351705259f 10.196.52.65:6380@16380 master - 0 1715422067063 7 connected 10923-16383
ae2370096014cc8b9a3d6804906d34679f519f3d 10.196.52.66:6379@16379 slave 0927e13c0fbb246d460df7137b9f3768901add9b 0 1715422067000 1 connected
eeffd828d55d8f321c709ab16b91e13086c9d0ff 10.196.52.64:6380@16380 slave 690583bf6464147c59b8b6be5051fc1ce6b1b29b 0 1715422068000 8 connected
4511548d363153bb5b258df324a87538d222b100 10.196.52.67:6380@16380 slave 3ce35d77a25660a093c2d36cce0c95351705259f 0 1715422068770 7 connected
690583bf6464147c59b8b6be5051fc1ce6b1b29b 10.196.52.64:6379@16379 master - 0 1715422069000 8 connected 5461-10922
bb1c16862396793a606ed8c0698f5ab7e8f9aeb6 10.196.52.66:6380@16380 slave 690583bf6464147c59b8b6be5051fc1ce6b1b29b 0 1715422067063 8 connected

可以看到完成了节点的所有slot的迁移

查验一下数据

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.196.52.64:6379> keys *
1) "4d"
2) "4c"
10.196.52.64:6379> 
[root@yp-bz-yj4a-04 fourb]# /opt/redis/bin/redis-cli  -h 10.196.52.66 -p 6380 -a Ebupt#202401f 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.196.52.66:6380> keys *
1) "4d"
2) "4c"
10.196.52.66:6380> get 4d
(error) MOVED 9587 10.196.52.64:6379

数据以及slot全部迁移过去,但是原来的节点数据依然也还在 待续。。。

2024-05-13继续

  之前迁移的时候检查原来的数据还在,slot迁移完之后数据还是存在的,然后迁移走的slot的节点的主从都会自动成为接受slot节点的从节点,也就是接收端的redis会成为一主和三个从节点

的集群

0927e13c0fbb246d460df7137b9f3768901add9b 10.196.52.65:6379@16379 master - 0 1715567317229 1 connected 0-5460
c1f5acfe0347804e0b57c1e20e6c3480b3eac789 10.196.52.67:6379@16379 myself,slave 690583bf6464147c59b8b6be5051fc1ce6b1b29b 0 1715567314000 8 connected
3ce35d77a25660a093c2d36cce0c95351705259f 10.196.52.65:6380@16380 master - 0 1715567316000 7 connected 10923-16383
ae2370096014cc8b9a3d6804906d34679f519f3d 10.196.52.66:6379@16379 slave 0927e13c0fbb246d460df7137b9f3768901add9b 0 1715567315000 1 connected
eeffd828d55d8f321c709ab16b91e13086c9d0ff 10.196.52.64:6380@16380 slave 690583bf6464147c59b8b6be5051fc1ce6b1b29b 0 1715567317000 8 connected
4511548d363153bb5b258df324a87538d222b100 10.196.52.67:6380@16380 slave 3ce35d77a25660a093c2d36cce0c95351705259f 0 1715567316226 7 connected
690583bf6464147c59b8b6be5051fc1ce6b1b29b 10.196.52.64:6379@16379 master - 0 1715567315224 8 connected 5461-10922
bb1c16862396793a606ed8c0698f5ab7e8f9aeb6 10.196.52.66:6380@16380 slave 690583bf6464147c59b8b6be5051fc1ce6b1b29b 0 1715567314000 8 connected

  剔除迁移走的两个节点的数据

[root@yp-bz-yj4a-06 fourb]# /opt/redis/bin/redis-cli -h 10.196.52.67 -p 6379 -a Ebupt#202401f --cluster del-node 10.196.52.66:6380 bb1c16862396793a606ed8c0698f5ab7e8f9aeb6
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node bb1c16862396793a606ed8c0698f5ab7e8f9aeb6 from cluster 10.196.52.66:6380
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
[root@yp-bz-yj4a-06 fourb]# /opt/redis/bin/redis-cli -h 10.196.52.65 -p 6379 -a Ebupt#202401f --cluster del-node 10.196.52.67:6379 c1f5acfe0347804e0b57c1e20e6c3480b3eac789
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node c1f5acfe0347804e0b57c1e20e6c3480b3eac789 from cluster 10.196.52.67:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.

  迁移过之后的redis集群

[root@yp-bz-yj4a-05 fourb]# /opt/redis/bin/redis-cli -h 10.196.52.65 -p 6379 -a Ebupt#202401f
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.196.52.65:6379> cluster nodes
eeffd828d55d8f321c709ab16b91e13086c9d0ff 10.196.52.64:6380@16380 slave 690583bf6464147c59b8b6be5051fc1ce6b1b29b 0 1715568173000 8 connected
690583bf6464147c59b8b6be5051fc1ce6b1b29b 10.196.52.64:6379@16379 master - 0 1715568172000 8 connected 5461-10922
3ce35d77a25660a093c2d36cce0c95351705259f 10.196.52.65:6380@16380 master - 0 1715568174000 7 connected 10923-16383
0927e13c0fbb246d460df7137b9f3768901add9b 10.196.52.65:6379@16379 myself,master - 0 1715568172000 1 connected 0-5460
ae2370096014cc8b9a3d6804906d34679f519f3d 10.196.52.66:6379@16379 slave 0927e13c0fbb246d460df7137b9f3768901add9b 0 1715568173000 1 connected
4511548d363153bb5b258df324a87538d222b100 10.196.52.67:6380@16380 slave 3ce35d77a25660a093c2d36cce0c95351705259f 0 1715568174899 7 connected

  常用命令整合

1 向redis集群添加添加数据
/opt/redis/bin/redis-cli -c -h 10.196.52.67 -p 6379 -a Ebupt#202401f set 4c 4cValue

2 查找集群数据所在的slot
/opt/redis/bin/redis-cli -c -h 10.196.52.67 -p 6379 -a Ebupt#202401f cluster keyslot 4a

3 查询节点的数据
/opt/redis/bin/redis-cli -h 10.196.52.65 -p 6380 -a Ebupt#202401f

4 向集群添加节点(自动会成为主节点)
/opt/redis/bin/redis-cli --cluster add-node 10.196.52.64:6379 10.196.52.65:6379 -a Ebupt#202401f

5 向刚加的主节点添加从节点
/opt/redis/bin/redis-cli --cluster add-node 10.196.52.64:6380 10.196.52.65:6379 -a Ebupt#202401f --cluster-slave --cluster-master-id 690583bf6464147c59b8b6be5051fc1ce6b1b29b
 
6 slot迁移
/opt/redis/bin/redis-cli --cluster reshard 10.196.52.65:6379 -a Ebupt#202401
 
 
7 删除节点
/opt/redis/bin/redis-cli -h 10.196.52.67 -p 6379 -a Ebupt#202401f --cluster del-node 10.196.52.66:6380 bb1c16862396793a606ed8c0698f5ab7e8f9aeb6

 

posted @ 2024-05-09 17:02  伊铭(netease)  阅读(189)  评论(1编辑  收藏  举报