Skip to content

Commit

Permalink
compatible redis with password upgrade from 053 to 060
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Rookie committed Jul 26, 2023
1 parent 7689c9d commit 7514f75
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
33 changes: 18 additions & 15 deletions deploy/redis/scripts/redis-sentinel-setup.sh.tpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh
set -e
set -ex
{{- $clusterName := $.cluster.metadata.name }}
{{- $namespace := $.cluster.metadata.namespace }}
{{- /* find redis-sentinel component */}}
Expand All @@ -17,17 +17,20 @@ set -e
{{- /* build primary pod message, because currently does not support cross-component acquisition of environment variables, the service of the redis master node is assembled here through specific rules */}}
{{- $primary_pod = printf "%s-%s-%d.%s-%s-headless.%s.svc" $clusterName $redis_component.name $candidate_instance_index $clusterName $redis_component.name $namespace }}
{{- $sentinel_monitor := printf "%s-%s %s" $clusterName $redis_component.name $primary_pod }}
cat>/etc/sentinel/redis-sentinel.conf<<EOF
port 26379
sentinel resolve-hostnames yes
sentinel announce-hostnames yes
sentinel monitor {{ $sentinel_monitor }} 6379 2
sentinel down-after-milliseconds {{ $clusterName }}-{{ $redis_component.name }} 5000
sentinel failover-timeout {{ $clusterName }}-{{ $redis_component.name }} 60000
sentinel parallel-syncs {{ $clusterName }}-{{ $redis_component.name }} 1
sentinel auth-user {{ $clusterName }}-{{ $redis_component.name }} $REDIS_SENTINEL_USER
sentinel auth-pass {{ $clusterName }}-{{ $redis_component.name }} $REDIS_SENTINEL_PASSWORD
sentinel sentinel-user $SENTINEL_USER
sentinel sentinel-pass $SENTINEL_PASSWORD
{{- /* $primary_svc := printf "%s-%s.%s.svc" $clusterName $redis_component.name $namespace */}}
EOF
{{- /* build sentinel config */}}
echo "port 26379" >> /etc/sentinel/redis-sentinel.conf
echo "sentinel resolve-hostnames yes" >> /etc/sentinel/redis-sentinel.conf
echo "sentinel announce-hostnames yes" >> /etc/sentinel/redis-sentinel.conf
echo "sentinel monitor {{ $sentinel_monitor }} 6379 2" >> /etc/sentinel/redis-sentinel.conf
echo "sentinel down-after-milliseconds {{ $clusterName }}-{{ $redis_component.name }} 5000" >> /etc/sentinel/redis-sentinel.conf
echo "sentinel failover-timeout {{ $clusterName }}-{{ $redis_component.name }} 60000" >> /etc/sentinel/redis-sentinel.conf
echo "sentinel parallel-syncs {{ $clusterName }}-{{ $redis_component.name }} 1" >> /etc/sentinel/redis-sentinel.conf
if [ ! -z "$REDIS_SENTINEL_PASSWORD" ]; then
echo "sentinel auth-user {{ $clusterName }}-{{ $redis_component.name }} $REDIS_SENTINEL_USER" >> /etc/sentinel/redis-sentinel.conf
echo "sentinel auth-pass {{ $clusterName }}-{{ $redis_component.name }} $REDIS_SENTINEL_PASSWORD" >> /etc/sentinel/redis-sentinel.conf
fi
if [ ! -z "$SENTINEL_PASSWORD" ]; then
echo "sentinel sentinel-user $SENTINEL_USER" >> /etc/sentinel/redis-sentinel.conf
echo "sentinel sentinel-pass $SENTINEL_PASSWORD" >> /etc/sentinel/redis-sentinel.conf
fi
{{- /* $primary_svc := printf "%s-%s.%s.svc" $clusterName $redis_component.name $namespace */}}
8 changes: 6 additions & 2 deletions deploy/redis/scripts/redis-sentinel-start.sh.tpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh
set -e
set -ex
{{- $clusterName := $.cluster.metadata.name }}
{{- $namespace := $.cluster.metadata.namespace }}
{{- /* find redis component */}}
Expand All @@ -12,7 +12,11 @@ set -e
{{- /* build redis engine service */}}
{{- $primary_svc := printf "%s-%s.%s.svc" $clusterName $redis_component.name $namespace }}
echo "Waiting for redis service {{ $primary_svc }} to be ready..."
until redis-cli -h {{ $primary_svc }} -p 6379 -a $REDIS_DEFAULT_PASSWORD ping; do sleep 1; done
if [ ! -z "$REDIS_DEFAULT_PASSWORD" ]; then
until redis-cli -h {{ $primary_svc }} -p 6379 -a $REDIS_DEFAULT_PASSWORD ping; do sleep 1; done
else
until redis-cli -h {{ $primary_svc }} -p 6379 ping; do sleep 1; done
fi
echo "redis service ready, Starting sentinel..."
echo "sentinel announce-ip $KB_POD_FQDN" >> /etc/sentinel/redis-sentinel.conf
exec redis-server /etc/sentinel/redis-sentinel.conf --sentinel
Expand Down
37 changes: 28 additions & 9 deletions deploy/redis/scripts/redis7-start.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
set -ex
echo "include /etc/conf/redis.conf" >> /etc/redis/redis.conf
echo "replica-announce-ip $KB_POD_FQDN" >> /etc/redis/redis.conf
echo "masteruser $REDIS_REPL_USER" >> /etc/redis/redis.conf
echo "masterauth $REDIS_REPL_PASSWORD" >> /etc/redis/redis.conf
{{- $data_root := getVolumePathByName ( index $.podSpec.containers 0 ) "data" }}
if [ -f /data/users.acl ]; then
sed -i "/user default on/d" /data/users.acl
Expand All @@ -12,9 +10,17 @@ if [ -f /data/users.acl ]; then
else
touch /data/users.acl
fi
echo "user default on allcommands allkeys >$REDIS_DEFAULT_PASSWORD" >> /data/users.acl
echo "user $REDIS_REPL_USER on +psync +replconf +ping >$REDIS_REPL_PASSWORD" >> /data/users.acl
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
if [ ! -z "$REDIS_REPL_PASSWORD" ]; then
echo "masteruser $REDIS_REPL_USER" >> /etc/redis/redis.conf
echo "masterauth $REDIS_REPL_PASSWORD" >> /etc/redis/redis.conf
echo "user $REDIS_REPL_USER on +psync +replconf +ping >$REDIS_REPL_PASSWORD" >> /data/users.acl
fi
if [ ! -z "$REDIS_SENTINEL_PASSWORD" ]; then
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
fi
if [ ! -z "$REDIS_DEFAULT_PASSWORD" ]; then
echo "user default on allcommands allkeys >$REDIS_DEFAULT_PASSWORD" >> /data/users.acl
fi
echo "aclfile /data/users.acl" >> /etc/redis/redis.conf

# usage: retry <command>
Expand Down Expand Up @@ -55,19 +61,32 @@ create_replication() {
echo "KB_POD_NAME=$KB_POD_NAME" >> /etc/redis/.kb_set_up.log
if [ -z "$primary" ]; then
echo "Primary pod information not available. shutdown redis-server..."
redis-cli -h 127.0.0.1 -p 6379 -a $REDIS_DEFAULT_PASSWORD shutdown
if [ ! -z "$REDIS_DEFAULT_PASSWORD" ]; then
redis-cli -h 127.0.0.1 -p 6379 -a "$REDIS_DEFAULT_PASSWORD" shutdown
else
redis-cli -h 127.0.0.1 -p 6379 shutdown
fi
exit 1
fi
if [ "$primary" = "$KB_POD_NAME" ]; then
echo "primary instance skip create a replication relationship."
else
primary_fqdn="$primary.$KB_CLUSTER_NAME-$KB_COMP_NAME-headless.$KB_NAMESPACE.svc"
echo "primary_fqdn=$primary_fqdn" >> /etc/redis/.kb_set_up.log
retry redis-cli -h $primary_fqdn -p 6379 -a $REDIS_DEFAULT_PASSWORD ping
redis-cli -h 127.0.0.1 -p 6379 -a $REDIS_DEFAULT_PASSWORD replicaof $primary_fqdn 6379
if [ ! -z "$REDIS_DEFAULT_PASSWORD" ]; then
retry redis-cli -h $primary_fqdn -p 6379 -a "$REDIS_DEFAULT_PASSWORD" ping
redis-cli -h 127.0.0.1 -p 6379 -a "$REDIS_DEFAULT_PASSWORD" replicaof $primary_fqdn 6379
else
retry redis-cli -h $primary_fqdn -p 6379 ping
redis-cli -h 127.0.0.1 -p 6379 replicaof $primary_fqdn 6379
fi
if [ $? -ne 0 ]; then
echo "Failed to create a replication relationship. shutdown redis-server..."
redis-cli -h 127.0.0.1 -p 6379 -a $REDIS_DEFAULT_PASSWORD shutdown
if [ ! -z "$REDIS_DEFAULT_PASSWORD" ]; then
redis-cli -h 127.0.0.1 -p 6379 -a "$REDIS_DEFAULT_PASSWORD" shutdown
else
redis-cli -h 127.0.0.1 -p 6379 shutdown
fi
fi
fi
}
Expand Down

0 comments on commit 7514f75

Please sign in to comment.