Redis集群启动

要设置 Redis 集群启动,可以遵循以下步骤:

步骤 1: 下载和安装 Redis

首先,需要下载并安装 Redis。可以从 Redis 官方网站下载适合您系统的 Redis 版本。

步骤 2: 配置 Redis 节点

  1. 在每个节点上编辑 Redis 配置文件(redis.conf),确保配置文件启用集群模式(cluster-enabled yes)。
  2. 设置节点的端口号(cluster-node-port)和初始主节点槽分配。

步骤 3: 启动 Redis 节点

  1. 启动每个 Redis 节点,可以使用以下命令启动 Redis: redis-server /path/to/redis.conf

步骤 4: 创建 Redis 集群

  1. 在其中一个节点上运行以下命令来创建 Redis 集群:
redis-cli --cluster create <node1_ip>:<port> <node2_ip>:<port> ... --cluster-replicas <number_of_replicas>

请替换 <node1_ip>:<port>, <node2_ip>:<port>等为实际节点的 IP 地址和端口号,<number_of_replicas> 替换为集群数量,根据集群中实例个数指定。

步骤 5: 验证集群

  1. 使用以下命令检查集群的状态:
redis-cli -c -h <any_node_ip> -p <port> cluster info

步骤 6: 实操记录

问题 1:
# export REDISCLI_AUTH=你的redis密码
# redis-cli --cluster create 192.168.40.128:7001 192.168.40.128:7002 192.168.40.128:7003 --cluster-replicas 3
[ERR] Node 192.168.40.128:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
# redis-cli --cluster create 192.168.40.128:7001 192.168.40.128:7002 192.168.40.128:7003 --cluster-replicas 3
*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 3 nodes and 3 replicas per node.
*** At least 12 nodes are required.
# redis-cli --cluster create 192.168.40.128:7001 192.168.40.128:7002 192.168.40.128:7003 --cluster-replicas 2
*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 3 nodes and 2 replicas per node.
*** At least 9 nodes are required.
# redis-cli --cluster create 192.168.40.128:7001 192.168.40.128:7002 192.168.40.128:7003 --cluster-replicas 1
*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 3 nodes and 1 replicas per node.
*** At least 6 nodes are required.
# redis-cli --cluster create 192.168.40.128:7001 192.168.40.128:7002 192.168.40.128:7003 --cluster-replicas 0
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: fbb08b2669a46f9590d0eca0d5e8abc86627e7f2 192.168.40.128:7001
   slots:[0-5460] (5461 slots) master
M: 1f523d416b0cf931e6cba250b37dccdafadce0a3 192.168.40.128:7002
   slots:[5461-10922] (5462 slots) master
M: a77d78c5429a25d7cc78859c0fae7ce0106be65a 192.168.40.128:7003
   slots:[10923-16383] (5461 slots) master
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
........................

如果以上永远等待加入集群,不能成功,请检查集群成员之间的通讯

问题 2:
# export REDISCLI_AUTH=你的redis密码
# redis-cli --cluster create 172.18.0.3:7001 172.18.0.6:7002 172.18.0.5:7003 172.18.0.8:7004 172.18.0.9:7005 172.18.0.10:7006 --cluster-replicas 1
[ERR] Node 172.18.0.3:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

解决方案:

1 remove the aof/rdb backup file
1 删除 AOF/RDB 备份文件
2 remove the node_conf file
2 删除node_conf文件
3 flushdb if necessary
3 flushdb(如有必要)
https://github.com/redis/redis/issues/3154

搭建成功

# export REDISCLI_AUTH=你的密码
# redis-cli --cluster create 172.18.0.3:7001 172.18.0.6:7002 172.18.0.5:7003 172.18.0.8:7004 172.18.0.9:7005 172.18.0.10:7006 --cluster-replicas 1
Could not connect to Redis at 172.18.0.10:7006: Connection refused
# redis-cli --cluster create 172.18.0.3:7001 172.18.0.6:7002 172.18.0.5:7003 172.18.0.8:7004 172.18.0.9:7005 172.18.0.10:7006 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.18.0.9:7005 to 172.18.0.3:7001
Adding replica 172.18.0.10:7006 to 172.18.0.6:7002
Adding replica 172.18.0.8:7004 to 172.18.0.5:7003
M: ab9f9a5abcfcd35e9b48a19a7d760e653270e8ca 172.18.0.3:7001
   slots:[0-5460] (5461 slots) master
M: b682c1da0f297046c9a263c7738badd2e71d3c0e 172.18.0.6:7002
   slots:[5461-10922] (5462 slots) master
M: 33a085f7652fe0e5d56727e72aafd7fc7bdc5a89 172.18.0.5:7003
   slots:[10923-16383] (5461 slots) master
S: 2b405e1f4da0392d26f78dc8359e8f45cc7139b7 172.18.0.8:7004
   replicates 33a085f7652fe0e5d56727e72aafd7fc7bdc5a89
S: c1cafc13edb73a27937db608557d160cf53f1524 172.18.0.9:7005
   replicates ab9f9a5abcfcd35e9b48a19a7d760e653270e8ca
S: ec85b1cb9543e7a735677cfd866e34931172d5e2 172.18.0.10:7006
   replicates b682c1da0f297046c9a263c7738badd2e71d3c0e
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 172.18.0.3:7001)
M: ab9f9a5abcfcd35e9b48a19a7d760e653270e8ca 172.18.0.3:7001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: c1cafc13edb73a27937db608557d160cf53f1524 172.18.0.9:7005
   slots: (0 slots) slave
   replicates ab9f9a5abcfcd35e9b48a19a7d760e653270e8ca
M: b682c1da0f297046c9a263c7738badd2e71d3c0e 172.18.0.6:7002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 2b405e1f4da0392d26f78dc8359e8f45cc7139b7 172.18.0.8:7004
   slots: (0 slots) slave
   replicates 33a085f7652fe0e5d56727e72aafd7fc7bdc5a89
M: 33a085f7652fe0e5d56727e72aafd7fc7bdc5a89 172.18.0.5:7003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: ec85b1cb9543e7a735677cfd866e34931172d5e2 172.18.0.10:7006
   slots: (0 slots) slave
   replicates b682c1da0f297046c9a263c7738badd2e71d3c0e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# 
问题 3:
1:S 20 Jun 2024 16:10:09.527 * MASTER <-> REPLICA sync started
1:S 20 Jun 2024 16:10:09.527 * Non blocking connect for SYNC fired the event.
1:S 20 Jun 2024 16:10:09.527 * Master replied to PING, replication can continue...
1:S 20 Jun 2024 16:10:09.528 * (Non critical) Master does not understand REPLCONF listening-port: -NOAUTH Authentication required.
1:S 20 Jun 2024 16:10:09.528 * (Non critical) Master does not understand REPLCONF capa: -NOAUTH Authentication required.
1:S 20 Jun 2024 16:10:09.528 * Trying a partial resynchronization (request 9567d66cf904b5cd195e2d0cbc403b56e45507d7:1).
1:S 20 Jun 2024 16:10:09.528 # Unexpected reply to PSYNC from master: -NOAUTH Authentication required.
1:S 20 Jun 2024 16:10:09.528 * Retrying with SYNC...
1:S 20 Jun 2024 16:10:09.528 # MASTER aborted replication with an error: NOAUTH Authentication required.
1:S 20 Jun 2024 16:10:09.528 * Reconnecting to MASTER 172.18.0.6:7002 after failure

解决方案:

对 Redis 的配置文件 redis.conf 进行一些调整,以便节点键的身份验证通过,并启用部分同步。以下是一些关键的配置选项及其说明:

  1. requirepass: 设置密码,以便客户端在连接到 Redis 服务器时进行身份验证。

  2. cluster-require-full-coverage: 设置为 no,以允许 Redis 集群在部分节点不可用时继续运行。

  3. sync: 设置为 partial,以启用部分同步。
    修改 redis.conf 文件的示例:

# 设置密码
requirepass yourpassword
# 允许集群在部分节点不可用时继续运行
cluster-require-full-coverage no

注意事项:

  • 确保节点可以相互通信。
  • 了解 Redis 集群的故障转移和分片策略。
  • 定期进行监控和维护集群,以确保高可用性和性能。
  • 如果没有slave界面也就是 cluster-replicas 0 可能会失败

本文转载:https://blog.csdn.net/weixin_42759398/article/details/137406711

posted @ 2024-04-18 14:59  Journey&Flower  阅读(250)  评论(0编辑  收藏  举报