|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | | -LOGFILE="/opt/redis/log/redis-cluster-init.log" |
4 | | - |
5 | | -touch $LOGFILE |
6 | | - |
7 | | -# Function to log messages |
8 | | -log() { |
9 | | - echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" | tee -a $LOGFILE |
| 3 | +function wait_for_redis_node() { |
| 4 | + local node=$1; |
| 5 | + until redis-cli -h $node -p 3000 ping > /dev/null 2>&1; do |
| 6 | + printf "Waiting for $node...\n"; |
| 7 | + sleep 5; |
| 8 | + done; |
| 9 | + printf "$node is ready.\n"; |
10 | 10 | } |
11 | 11 |
|
12 | | -log "Starting Redis cluster initialization..." |
13 | | - |
14 | | -# Wait for all Redis nodes to be ready |
15 | | -while ! (redis-cli -h redis-1 -p 3000 ping && \ |
16 | | - redis-cli -h redis-2 -p 3000 ping && \ |
17 | | - redis-cli -h redis-3 -p 3000 ping && \ |
18 | | - redis-cli -h redis-4 -p 3000 ping && \ |
19 | | - redis-cli -h redis-5 -p 3000 ping && \ |
20 | | - redis-cli -h redis-6 -p 3000 ping); do |
21 | | - log "Waiting for Redis nodes to be ready..." |
22 | | - sleep 5 |
23 | | -done |
24 | | - |
25 | | -log "All Redis nodes are ready. Creating cluster..." |
26 | | - |
27 | | -# Create the Redis cluster |
28 | | -{ |
29 | | - yes yes | redis-cli --cluster create redis-1:3000 redis-2:3000 redis-3:3000 redis-4:3000 redis-5:3000 redis-6:3000 --cluster-replicas 1 |
30 | | -} 2>&1 | tee -a $LOGFILE |
31 | | - |
32 | | -log "Redis cluster creation complete." |
33 | | - |
34 | | -# Waiting for cluster creation: |
35 | | -sleep 20 |
| 12 | +function create_redis_cluster() { |
| 13 | + yes yes | redis-cli --cluster create redis-{1..6}:3000 --cluster-replicas 1; |
| 14 | + printf "Redis cluster started.\n"; |
| 15 | +} |
36 | 16 |
|
37 | | -# Start Redis server with cluster enabled |
38 | | -redis-server --port 3000 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes 2>&1 | tee -a $LOGFILE |
| 17 | +printf "Starting Redis cluster initialization...\n"; |
39 | 18 |
|
40 | | -log "Redis server started with cluster enabled." |
| 19 | +for node in redis-{1..6}; do |
| 20 | + wait_for_redis_node $node; |
| 21 | +done; |
41 | 22 |
|
| 23 | +create_redis_cluster; |
0 commit comments