When I'm close one master node, the whole cluster made a normal switch, the slave Upgrade to master; but When I start to the node that has stopped,A miracle happened, I think that the whole structure is abnormal,

S: 44868faa97063a2cfb4b64008e53b80916882a13 10.181.66.79:6666 slots: (0 slots) slave replicates 199197c667c1c87dfaffb9199ed9de2f4f56e28e S: 36778de3f28089597b808fe1c0ac0866a9193091 10.181.66.78:6668 slots: (0 slots) slave replicates 7013785f2072c0c6e1ee5f139831c411272ea800 M: 199197c667c1c87dfaffb9199ed9de2f4f56e28e 10.181.66.79:6668 slots:10923-16383 (5461 slots) master 1 additional replica(s) M: 0c8625cfa2652b85b6e9b32261bfa1a35714d98f 10.181.66.78:6667 slots:5461-10922 (5462 slots) master 1 additional replica(s) M: 7013785f2072c0c6e1ee5f139831c411272ea800 10.181.66.80:6666 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 615e93012b3cd53eb828b412a80f5f1660c88077 10.181.66.80:6667 slots: (0 slots) slave replicates 0c8625cfa2652b85b6e9b32261bfa1a35714d98f

One of the master and slave is on same server: M: 199197c667c1c87dfaffb9199ed9de2f4f56e28e 10.181.66.79:6668 S: 44868faa97063a2cfb4b64008e53b80916882a13 10.181.66.79:6666

role:master connected_slaves:1 slave0:ip=10.181.66.79,port=6666,state=online,offset=115333,lag=0

role:slave master_host:10.181.66.79 master_port:6668

the master and slave is on same server,dangerous~~~

Comment From: ljluestc

#!/bin/bash

# Array of Redis node IPs and ports
NODES=("10.181.66.78:6667" "10.181.66.78:6668" "10.181.66.79:6666" "10.181.66.79:6668" "10.181.66.80:6666" "10.181.66.80:6667")

# Create the cluster
yes yes | redis-cli --cluster create ${NODES[@]} --cluster-replicas 1

# Anti-affinity configuration
for NODE in "${NODES[@]}"; do
  HOST=$(echo $NODE | cut -d ':' -f 1)
  PORT=$(echo $NODE | cut -d ':' -f 2)

  redis-cli -h $HOST -p $PORT CLUSTER MEET $HOST $PORT
done

# Check and reassign replicas if needed
for NODE in "${NODES[@]}"; do
  HOST=$(echo $NODE | cut -d ':' -f 1)
  PORT=$(echo $NODE | cut -d ':' -f 2)

  redis-cli -h $HOST -p $PORT CLUSTER NODES | while read line; do
    NODE_ID=$(echo $line | awk '{print $1}')
    NODE_ROLE=$(echo $line | awk '{print $3}')
    NODE_IP=$(echo $line | awk '{print $2}' | cut -d ':' -f 1)

    if [[ $NODE_ROLE == *"slave"* ]]; then
      MASTER_ID=$(echo $line | awk '{print $4}')
      MASTER_IP=$(redis-cli -h $HOST -p $PORT CLUSTER NODES | grep $MASTER_ID | awk '{print $2}' | cut -d ':' -f 1)

      if [ "$NODE_IP" == "$MASTER_IP" ]; then
        for NEW_MASTER in "${NODES[@]}"; do
          NEW_MASTER_IP=$(echo $NEW_MASTER | cut -d ':' -f 1)
          if [ "$NEW_MASTER_IP" != "$NODE_IP" ]; then
            redis-cli -h $HOST -p $PORT CLUSTER REPLICATE $NEW_MASTER
            break
          fi
        done
      fi
    fi
  done
done