github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/deploy/redis/scripts/redis-start.sh (about) 1 #!/bin/sh 2 set -ex 3 # build redis.conf 4 echo "include /etc/conf/redis.conf" >> /etc/redis/redis.conf 5 echo "replica-announce-ip $KB_POD_FQDN" >> /etc/redis/redis.conf 6 {{- $data_root := getVolumePathByName ( index $.podSpec.containers 0 ) "data" }} 7 if [ -f /data/users.acl ]; then 8 sed -i "/user default on/d" /data/users.acl 9 sed -i "/user $REDIS_REPL_USER on/d" /data/users.acl 10 sed -i "/user $REDIS_SENTINEL_USER on/d" /data/users.acl 11 else 12 touch /data/users.acl 13 fi 14 if [ ! -z "$REDIS_REPL_PASSWORD" ]; then 15 echo "masteruser $REDIS_REPL_USER" >> /etc/redis/redis.conf 16 echo "masterauth $REDIS_REPL_PASSWORD" >> /etc/redis/redis.conf 17 echo "user $REDIS_REPL_USER on +psync +replconf +ping >$REDIS_REPL_PASSWORD" >> /data/users.acl 18 fi 19 if [ ! -z "$REDIS_SENTINEL_PASSWORD" ]; then 20 echo "user $REDIS_SENTINEL_USER on allchannels +multi +slaveof +ping +exec +subscribe +config|rewrite +role +publish +info +client|setname +client|kill +script|kill >$REDIS_SENTINEL_PASSWORD" >> /data/users.acl 21 fi 22 if [ ! -z "$REDIS_DEFAULT_PASSWORD" ]; then 23 echo "protected-mode yes" >> /etc/redis/redis.conf 24 echo "user default on allcommands allkeys >$REDIS_DEFAULT_PASSWORD" >> /data/users.acl 25 else 26 echo "protected-mode no" >> /etc/redis/redis.conf 27 fi 28 echo "aclfile /data/users.acl" >> /etc/redis/redis.conf 29 30 # usage: retry <command> 31 retry() { 32 local max_attempts=20 33 local attempt=1 34 until "$@" || [ $attempt -eq $max_attempts ]; do 35 echo "Command '$*' failed. Attempt $attempt of $max_attempts. Retrying in 5 seconds..." 36 attempt=$((attempt + 1)) 37 sleep 3 38 done 39 if [ $attempt -eq $max_attempts ]; then 40 echo "Command '$*' failed after $max_attempts attempts. shutdown redis-server..." 41 if [ ! -z "$REDIS_DEFAULT_PASSWORD" ]; then 42 redis-cli -h 127.0.0.1 -p 6379 -a "$REDIS_DEFAULT_PASSWORD" shutdown 43 else 44 redis-cli -h 127.0.0.1 -p 6379 shutdown 45 fi 46 fi 47 } 48 49 start_redis_server() { 50 exec redis-server /etc/redis/redis.conf \ 51 --loadmodule /opt/redis-stack/lib/redisearch.so ${REDISEARCH_ARGS} \ 52 --loadmodule /opt/redis-stack/lib/redisgraph.so ${REDISGRAPH_ARGS} \ 53 --loadmodule /opt/redis-stack/lib/redistimeseries.so ${REDISTIMESERIES_ARGS} \ 54 --loadmodule /opt/redis-stack/lib/rejson.so ${REDISJSON_ARGS} \ 55 --loadmodule /opt/redis-stack/lib/redisbloom.so ${REDISBLOOM_ARGS} 56 } 57 58 create_replication() { 59 # Waiting for redis-server to start 60 if [ ! -z "$REDIS_DEFAULT_PASSWORD" ]; then 61 retry redis-cli -h 127.0.0.1 -p 6379 -a "$REDIS_DEFAULT_PASSWORD" ping 62 else 63 retry redis-cli -h 127.0.0.1 -p 6379 ping 64 fi 65 66 # Waiting for primary pod information from the DownwardAPI annotation to be available 67 attempt=1 68 max_attempts=20 69 while [ $attempt -le $max_attempts ] && [ -z "$(cat /kb-podinfo/primary-pod)" ]; do 70 echo "Waiting for primary pod information from the DownwardAPI annotation to be available, attempt $attempt of $max_attempts..." 71 sleep 5 72 attempt=$((attempt + 1)) 73 done 74 primary=$(cat /kb-podinfo/primary-pod) 75 echo "DownwardAPI get primary=$primary" >> /etc/redis/.kb_set_up.log 76 echo "KB_POD_NAME=$KB_POD_NAME" >> /etc/redis/.kb_set_up.log 77 if [ -z "$primary" ]; then 78 echo "Primary pod information not available. shutdown redis-server..." 79 if [ ! -z "$REDIS_DEFAULT_PASSWORD" ]; then 80 redis-cli -h 127.0.0.1 -p 6379 -a "$REDIS_DEFAULT_PASSWORD" shutdown 81 else 82 redis-cli -h 127.0.0.1 -p 6379 shutdown 83 fi 84 exit 1 85 fi 86 87 # create a replication relationship, if failed, shutdown redis-server 88 if [ "$primary" = "$KB_POD_NAME" ]; then 89 echo "primary instance skip create a replication relationship." 90 else 91 primary_fqdn="$primary.$KB_CLUSTER_NAME-$KB_COMP_NAME-headless.$KB_NAMESPACE.svc" 92 echo "primary_fqdn=$primary_fqdn" >> /etc/redis/.kb_set_up.log 93 if [ ! -z "$REDIS_DEFAULT_PASSWORD" ]; then 94 retry redis-cli -h $primary_fqdn -p 6379 -a "$REDIS_DEFAULT_PASSWORD" ping 95 redis-cli -h 127.0.0.1 -p 6379 -a "$REDIS_DEFAULT_PASSWORD" replicaof $primary_fqdn 6379 96 else 97 retry redis-cli -h $primary_fqdn -p 6379 ping 98 redis-cli -h 127.0.0.1 -p 6379 replicaof $primary_fqdn 6379 99 fi 100 if [ $? -ne 0 ]; then 101 echo "Failed to create a replication relationship. shutdown redis-server..." 102 if [ ! -z "$REDIS_DEFAULT_PASSWORD" ]; then 103 redis-cli -h 127.0.0.1 -p 6379 -a "$REDIS_DEFAULT_PASSWORD" shutdown 104 else 105 redis-cli -h 127.0.0.1 -p 6379 shutdown 106 fi 107 fi 108 fi 109 } 110 111 create_replication & 112 start_redis_server