apiVersion: v1 data: switch_available_redis.sh: |- #!/bin/bash pwd=`pwd` function check_replicaof_alive() { check_redis_ip=$1 check_redis_port=$2 if [ -n "$TLS_REDIS_FILE" ]; then redis-cli -h $check_redis_ip -p $check_redis_port --tls --cacert $TLS_REDIS_FILE ping > /dev/null else redis-cli -h $check_redis_ip -p $check_redis_port ping > /dev/null fi if [[ $? -eq 0 ]]; then return 0 else return 1 fi } function start() { current_date=$(date +%Y-%m-%d) #Get the IP of the current redis synchronization redis_info=$(redis-cli -h $REDIS_HOST -p $HOST_PORT CONFIG GET replicaof) str=(${redis_info//,/}) db_ip=${str[1]} db_port=${str[2]} #1.Check whether the current redis connection address is CM master if [ "$db_ip" = "$REDIS_MASTER" -a "$MASTER_PORT" = "$db_port" ];then check_redis 0 elif [ "$db_ip" = "$REDIS_SLAVE" -a "$SLAVE_PORT" = "$db_port" ];then check_redis 1 else check_redis 2 fi } function check_redis() { CHECK_TYPE=$1 # Starting time start_time=$(date +%s) while true; do # Use redis-cli to detect connections check_replicaof_alive $REDIS_MASTER $MASTER_PORT # check return value if [[ $? -eq 0 ]]; then #If the current redis slaveof is not the master IP, check whether the master IP is alive and change the redis slaveof if [ "$CHECK_TYPE" != "0" ]; then echo "[`date '+%Y-%m-%d %T'`] update redis slaveof: $REDIS_MASTER $MASTER_PORT " up_redis $REDIS_MASTER $MASTER_PORT break fi sleep 1 break else echo "[`date '+%Y-%m-%d %T'`] unable to connect to $REDIS_MASTER $MASTER_PORT redis server" #If the current redis slaveof is slave IP, jump out of the loop and no longer continue to change redis slaveof if [ "$CHECK_TYPE" = "1" ]; then sleep 1 break fi fi # Check if the duration is exceeded current_time=$(date +%s) elapsed_time=$((current_time - start_time)) if [[ $elapsed_time -ge $CHECK_DURATION ]]; then check_replicaof_alive $REDIS_SLAVE $SLAVE_PORT if [[ $? -eq 0 ]]; then echo "[`date '+%Y-%m-%d %T'`] update redis slaveof: $REDIS_SLAVE $SLAVE_PORT redis server" up_redis $REDIS_SLAVE $SLAVE_PORT break else echo "[`date '+%Y-%m-%d %T'`] unable to connect to $REDIS_SLAVE $SLAVE_PORT redis server" fi fi sleep 1 done } function up_redis() { UP_IP=$1 UP_PORT=$2 check_replicaof_alive $UP_IP $UP_PORT if [[ $? -eq 0 ]]; then redis-cli -h $REDIS_HOST -p $HOST_PORT REPLICAOF $UP_IP $UP_PORT echo "[`date '+%Y-%m-%d %T'`] successfully changed Redis slaveof : $UP_IP $UP_PORT" sleep $INTERVAL_TIME else echo "[`date '+%Y-%m-%d %T'`] Change failed, unable to connect to $UP_IP $UP_PORT redis server" fi } REDIS_HOST="127.0.0.1" HOST_PORT=6379 #Redis connection timeout (seconds) CHECK_DURATION=120 # Redis master-slave switch synchronization interval time INTERVAL_TIME=5m REDIS_MASTER="" MASTER_PORT="" REDIS_SLAVE="" SLAVE_PORT="" TLS_REDIS_FILE="" usage() { echo "Usage: $0 [-m ] [-s ] [-T ] [-t