move to component

This commit is contained in:
2025-07-16 15:44:56 +02:00
parent 3cbf7b389a
commit 9b707bb0df
17 changed files with 1094 additions and 0 deletions

View File

@ -0,0 +1,37 @@
{{- $hostname := env "HOSTNAME" }}
{{- $service := env "VALKEY_HEADLESS_SERVICE" }}
{{- $namespace := env "NAMESPACE" }}
{{- $port := env "VALKEY_PORT" }}
{{- $sentinel_port := env "VALKEY_SENTINEL_PORT" }}
{{- $replicas := env "VALKEY_REPLICAS" }}
{{- $domain := printf "%s.%s.svc.cluster.local" $service $namespace }}
{{- $fqdn := printf "%s.%s" $hostname $domain }}
{{- $hostid := sha1sum $hostname }}
{{- $datadir := env "VALKEY_DATA_DIR" }}
dir {{ $datadir }}
protected-mode no
loglevel {{ env "VALKEY_LOG_LEVEL" }}
appendonly yes
appendfilename "appendonly.aof"
appenddirname "appendonlydir"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
aof-timestamp-enabled no
save ""
replica-announce-port {{ $port }}
replica-announce-ip {{ $fqdn }}
# User-supplied replica configuration:
rename-command FLUSHDB ""
rename-command FLUSHALL ""

View File

@ -0,0 +1,119 @@
{{- $hostname := env "HOSTNAME" }}
{{- $service := env "VALKEY_HEADLESS_SERVICE" }}
{{- $namespace := env "NAMESPACE" }}
{{- $port := env "VALKEY_PORT" }}
{{- $sentinel_port := env "VALKEY_SENTINEL_PORT" }}
{{- $replicas := env "VALKEY_REPLICAS" }}
{{- $domain := printf "%s.%s.svc.cluster.local" $service $namespace }}
{{- $fqdn := printf "%s.%s" $hostname $domain }}
{{- $hostid := sha1sum $hostname }}
################################## INCLUDES ###################################
################################## MODULES #####################################
################################## NETWORK #####################################
bind * -::*
protected-mode no
port {{ $port }}
tcp-backlog 511
timeout 0
tcp-keepalive 300
################################# TLS/SSL #####################################
port {{ env "VALKEY_PORT" }}
################################### RDMA ######################################
################################# GENERAL #####################################
daemonize no
pidfile /opt/bitnami/valkey/tmp/valkey.pid
loglevel notice
logfile ""
databases 16
always-show-logo no
hide-user-data-from-log yes
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
locale-collate ""
################################ SNAPSHOTTING ################################
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
rdb-version-check strict
dbfilename dump.rdb
rdb-del-sync-files no
dir {{ env "VALKEY_DATA_DIR" }}
################################# REPLICATION #################################
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync yes
repl-diskless-sync-delay 5
repl-diskless-sync-max-replicas 0
repl-diskless-load disabled
dual-channel-replication-enabled no
repl-disable-tcp-nodelay no
replica-priority 100
replica-announce-port {{ $port }}
replica-announce-ip {{ $fqdn }}
############################### KEYS TRACKING #################################
################################## SECURITY ###################################
acllog-max-len 128
################################### CLIENTS ####################################
############################## MEMORY MANAGEMENT ################################
############################# LAZY FREEING ####################################
lazyfree-lazy-eviction yes
lazyfree-lazy-expire yes
lazyfree-lazy-server-del yes
replica-lazy-flush yes
lazyfree-lazy-user-del yes
lazyfree-lazy-user-flush yes
################################ THREADED I/O #################################
############################ KERNEL OOM CONTROL ##############################
oom-score-adj no
oom-score-adj-values 0 200 800
#################### KERNEL transparent hugepage CONTROL ######################
disable-thp yes
############################## APPEND ONLY MODE ###############################
appendonly no
appendfilename "appendonly.aof"
appenddirname "appendonlydir"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
aof-timestamp-enabled no
################################ SHUTDOWN #####################################
################ NON-DETERMINISTIC LONG BLOCKING COMMANDS #####################
################################ VALKEY CLUSTER ###############################
########################## CLUSTER DOCKER/NAT support ########################
################################## COMMAND LOG ###################################
commandlog-execution-slower-than 10000
commandlog-slow-execution-max-len 128
commandlog-request-larger-than 1048576
commandlog-large-request-max-len 128
commandlog-reply-larger-than 1048576
commandlog-large-reply-max-len 128
################################ LATENCY MONITOR ##############################
latency-monitor-threshold 0
################################ LATENCY TRACKING ##############################
############################# EVENT NOTIFICATION ##############################
notify-keyspace-events ""
############################### ADVANCED CONFIG ###############################
hash-max-listpack-entries 512
hash-max-listpack-value 64
list-max-listpack-size -2
list-compress-depth 0
set-max-intset-entries 512
set-max-listpack-entries 128
set-max-listpack-value 64
zset-max-listpack-entries 128
zset-max-listpack-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
########################### ACTIVE DEFRAGMENTATION #######################
jemalloc-bg-thread yes

View File

@ -0,0 +1,47 @@
{{- $hostname := env "HOSTNAME" }}
{{- $service := env "VALKEY_HEADLESS_SERVICE" }}
{{- $namespace := env "NAMESPACE" }}
{{- $port := env "VALKEY_PORT" }}
{{- $name := env "VALKEY_NAME" }}
{{- $sentinel_port := env "VALKEY_SENTINEL_PORT" }}
{{- $replicas := env "VALKEY_REPLICAS" }}
{{- $domain := printf "%s.%s.svc.cluster.local" $service $namespace }}
{{- $fqdn := printf "%s.%s" $hostname $domain }}
{{- $hostid := sha1sum $hostname }}
{{- $quorum := env "VALKEY_QUORUM" }}
#-REPLICAS:{{ $replicas }}
dir "/tmp"
port {{ $sentinel_port }}
sentinel monitor mymaster SENTINEL_MASTER {{ $port }} {{ $quorum }}
sentinel down-after-milliseconds mymaster 60000
# User-supplied sentinel configuration:
# End of sentinel configuration
{{ printf "sentinel myid %s" $hostid }}
sentinel announce-hostnames yes
sentinel resolve-hostnames yes
sentinel announce-port {{ $sentinel_port }}
sentinel announce-ip {{ $fqdn }}
# Generated by CONFIG REWRITE
latency-tracking-info-percentiles 50 99 99.9
protected-mode no
# gotemplate-pause!
user default on nopass sanitize-payload ~* &* +@all
# gotemplate-resume!
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
sentinel current-epoch 0
{{- range $i, $e := until ( int $replicas ) }}
{{- $ndeHostname := printf "%s-%d" $name $i }}
{{- $ndeFQDN := printf "%s.%s" $ndeHostname $domain }}
{{- $nodeID := sha1sum $ndeHostname }}
{{- if (ne $fqdn $ndeFQDN) }}
sentinel {{ printf "known-sentinel mymaster %s %s %s" $ndeFQDN $sentinel_port $nodeID }}
sentinel {{ printf "known-replica mymaster %s %s" $ndeFQDN $port }}
{{- end}}
{{- end}}

View File

@ -0,0 +1,81 @@
#!/bin/sh
pingSentinel() {
svc=${VALKEY_HEADLESS_SERVICE:-"localhost"}
resp=$(timeout -s 15 $1 \
valkey-cli \
-h ${svc} \
-p $VALKEY_SENTINEL_PORT \
ping)
ret=${?}
echo $resp
return ${ret}
}
getPrimaryInfo() {
valkey-cli --csv -h ${VALKEY_HEADLESS_SERVICE} -p ${VALKEY_SENTINEL_PORT} sentinel get-primary-addr-by-name "mymaster"| \
awk -F ',' '{ gsub(/"/,"",$0); print $1 " " $2 }'
return ${?}
}
getSentinelMasterName() {
getPrimaryInfo | awk -F ' ' '{print $1}'
}
getFailOverStatus() {
# Check if the failover is finished
local failoverStatus
primaryInfo=$(getPrimaryInfo)
primaryHost=$(echo ${primaryInfo} | awk -F ' ' '{print $1}')
currentHost=$(hostname -f)
if [[ "${primaryHost}" != "${currentHost}" ]]; then
echo "Failover finished"
return 0
else
echo "Failover in progress"
return 1
fi
}
waitForSentinel() {
tout=60
while true; do
response=$(pingSentinel 5)
if [ "${response}" = "PONG" ]; then
echo "Sentinel is responding"
break
return 0
fi
echo "Sentinel is not responding [${response}]"
sleep 1
tout=$((tout - 1))
if [ "${tout}" -le 0 ]; then
echo "Sentinel ping timed out"
return 124
fi
done
sleep 10
}
sentinelIsMaster() {
# Check if the sentinel is master
primaryInfo=$(getPrimaryInfo)
primaryHost=$(echo ${primaryInfo} | awk -F ' ' '{print $1}')
currentHost=$(hostname -f)
if [[ "${primaryHost}" != "${currentHost}" ]]; then
echo "Sentinel is not master"
return 1
else
echo "Sentinel is master"
return 0
fi
}
sentinelReset() {
# Reset the sentinel
valkey-cli -h ${VALKEY_HEADLESS_SERVICE} -p ${VALKEY_SENTINEL_PORT} sentinel reset "${1}"
return $?
}

View File

@ -0,0 +1,27 @@
#!/bin/sh
ping_valkey() {
resp=$(timeout -s 15 $1 \
valkey-cli \
-h localhost \
-p $VALKEY_PORT \
ping)
ret=${?}
echo $resp
return ${ret}
}
response=$(ping_valkey 5)
if [ "$?" -eq "124" ]; then
echo "Timed out"
exit 1
fi
firstWord=$(echo $response | awk 'NR==1 {print $1;}')
if [ "$response" != "PONG" ] && [ "$firstWord" != "LOADING" ] && [ "$firstWord" != "MASTERDOWN" ]; then
echo "Valey is not alive [${response}]"
exit 1
fi
echo "$( date +'[%Y/%m/%d %H:%M:%S]') Valkey is alive"
exit 0

View File

@ -0,0 +1,27 @@
#!/bin/sh
ping_sentinel() {
resp=$(timeout -s 15 $1 \
valkey-cli \
-h localhost \
-p $VALKEY_SENTINEL_PORT \
ping)
ret=${?}
echo $resp
return ${ret}
}
response=$(ping_sentinel 5)
if [ "${?}" -eq 124 ]; then
echo "Sentinel ping timed out"
exit 124
fi
if [ "${response}" != "PONG" ];
then
echo "Sentinel is not responding"
exit 1
fi
echo "$( date +'[%Y/%m/%d %H:%M:%S]') Sentinel is responding"
exit 0

View File

@ -0,0 +1,32 @@
#!/bin/sh
source /opt/scripts/common.sh
tmout=120
while true ; do
echo "I'm the primary pod and you are stopping me. Starting sentinel failover"
echo "Waiting for failover to finish..."
fo=$(getFailOverStatus)
if [ $? -eq 0 ]; then
echo "Primary has been successfully failed over to a different pod."
break
fi
sleep 1
tmout=$((tmout - 1))
if [ "${tmout}" -le 0 ]; then
echo "Failover timed out"
exit 1
fi
done
echo "Primary has been successfuly failed over to a different pod."
echo "Removing myself from the sentinel configuration"
sentinelReset $(hostname -f)
if [ $? -ne 0 ]; then
echo "Failed to remove myself from the sentinel configuration"
exit 1
fi
exit 0

View File

@ -0,0 +1,73 @@
#!/bin/sh
# Run Valkey command
vcli() {
valkey-cli -h 127.0.0.1 -p "${VALKEY_PORT}" "$@"
return $?
}
# Run Sentinel command
vcli-sentinel() {
valkey-cli -h "${VALKEY_HEADLESS_SERVICE}" -p "${VALKEY_SENTINEL_PORT}" sentinel "$@"
return $?
}
getFailOverStatus() {
# Check if the failover is finished
local failoverStatus
primaryInfo=$(vcli-sentinel get-primary-addr-by-name "mymaster")
primaryHost=$(echo ${primaryInfo} | awk -F ' ' '{print $1}')
currentHost=$(hostname -f)
if [[ "${primaryHost}" != "${currentHost}" ]]; then
echo "Failover finished"
return 0
else
echo "Failover in progress"
return 1
fi
}
getRole() {
# Get the role of the current node
VALKEY_ROLE=$(vcli role | head -1)
echo "${VALKEY_ROLE}"
}
isPrimary() {
# Check if the current node is the primary
role=$(getRole)
if [ "${role}" = "master" ]; then
echo "I'm the master"
else
echo "I'm not the master, I'm a ${role}"
fi
}
if isPrimary && ! getFailOverStatus; then
echo "I'm the primary and you are stopping me, pausing client connections"
#Pausing write connections to avoid data loss"
vcli CLIENT PAUSE "22000" WRITE
echo "Starting failover"
vcli-sentinel failover "mymaster"
echo "Waiting for sentinel to complete failover for up to 120s"
tmout=120
while true ; do
echo "I'm the primary pod and you are stopping me. Starting sentinel failover"
getFailOverStatus
if [ $? -eq 0 ]; then
echo "Primary has been successfully failed over to a different pod."
break
fi
echo "Waiting for failover to finish..."
sleep 1
tmout=$((tmout - 1))
if [ "${tmout}" -le 0 ]; then
echo "Failover timed out"
exit 1
fi
done
fi
exit 0

View File

@ -0,0 +1,26 @@
#!/bin/sh
ping_valkey() {
resp=$(timeout -s 15 $1 \
valkey-cli \
-h localhost \
-p $VALKEY_PORT \
ping)
ret=${?}
echo $resp
return ${ret}
}
response=$(ping_valkey 5)
if [ "$?" -eq "124" ]; then
echo "Timed out"
exit 1
fi
if [ "$response" != "PONG" ]; then
echo "Valey is not ready [${response}]"
exit 1
fi
echo "$( date +'[%Y/%m/%d %H:%M:%S]') Valkey is ready"
exit 0

View File

@ -0,0 +1,99 @@
#!/bin/sh
pingSentinel() {
resp=$(timeout -s 15 $1 \
valkey-cli \
-h ${VALKEY_HEADLESS_SERVICE} \
-p ${VALKEY_SENTINEL_PORT} \
ping)
ret=${?}
echo $resp
return ${ret}
}
getPrimaryInfo() {
valkey-cli -t 15 --csv -h ${VALKEY_HEADLESS_SERVICE} -p ${VALKEY_SENTINEL_PORT} sentinel get-primary-addr-by-name "mymaster"| \
awk -F ',' '{ gsub(/"/,"",$0); print $1 " " $2 }'
return ${?}
}
waitForSentinel() {
tout=60
while true; do
response=$(pingSentinel 5)
if [ "${response}" = "PONG" ]; then
echo "Sentinel is responding"
break
return 0
fi
echo "Sentinel is not responding [${response}]"
sleep 1
tout=$((tout - 1))
if [ "${tout}" -le 0 ]; then
echo "Sentinel ping timed out"
return 124
fi
done
sleep 10
}
startValkey() {
# Start Valkey
echo "Running : [valkey-server ${@}]"
valkey-server ${@}
ret=${?}
if [ "${ret}" -ne 0 ]; then
echo "Failed to start Valkey"
exit ${ret}
fi
}
setupPrimary=0
primaryHost=""
primaryPort=""
#if [[ -f /etc/valkey/sentinel.conf ]]; then
# primaryHost="$(awk '/monitor/ {print $4}' /etc/valkey/sentinel.conf)"
# primaryPort="$(awk '/monitor/ {print $5}' /etc/valkey/sentinel.conf)"
# echo "Found previous primary ${primaryHost}:${primaryPort}"
#fi
if [[ ! -f /etc/valkey/replication.conf ]]; then
cp /etc/valkey/replication.conf.orig /etc/valkey/replication.conf
fi
waitForSentinel
ret=${?}
if [ "${ret}" -ne 0 ]; then
exit ${ret}
fi
primaryInfo=$(getPrimaryInfo)
if [ "${?}" -ne 0 ]; then
echo "No primary found, seting up node as primary"
startPrimary=1
else
primaryHost=$(echo ${primaryInfo} | awk -F ' ' '{print $1}')
primaryPort=$(echo ${primaryInfo} | awk -F ' ' '{print $2}')
echo "Primary host is : ${primaryHost}, port: ${primaryPort}"
currentHost=$(hostname -f)
echo "Current host is : ${currentHost}"
if [ "${primaryHost}" != "${currentHost}" ]; then
echo "Not the primary, setting up as replica"
startPrimary=0
else
echo "This is the primary"
startPrimary=1
fi
fi
if [ "${startPrimary}" -eq 1 ]; then
echo "Starting Valkey as primary"
cat $1
startValkey ${@}
else
echo "Starting Valkey as replica"
startValkey ${@} "--replicaof" "${primaryHost}" "${primaryPort}"
fi

View File

@ -0,0 +1,38 @@
#!/bin/sh
# Generate sentinel.conf file for Valkey Sentinel
source /opt/scripts/common.sh
PATH=${PATH}:/
SENTINEL_MASTER=""
updateSentinelMaster(){
cp /etc/valkey/sentinel.conf.orig /etc/valkey/sentinel.conf
hostname=$(hostname -f)
pingSentinel 1
if [ $? -ne 0 ]; then
SENTINEL_MASTER=$(hostname -f)
else
SENTINEL_MASTER=$(getSentinelMasterName)
fi
sed -i "s/SENTINEL_MASTER/${SENTINEL_MASTER}/g" /etc/valkey/sentinel.conf
}
if [ -f /etc/valkey/sentinel.conf ]; then
echo "Sentinel configuration file already exists, starting with this."
reps=$(awk -F ':' '/REPLICAS/ {print $2}' /etc/valkey/sentinel.conf)
if [ "${reps}" = "${VALKEY_REPLICAS}" ]; then
echo "Sentinel configuration file already has the correct number of replicas (${VALKEY_REPLICAS})."
else
echo "Updating Sentinel configuration file with the correct number of replicas (${VALKEY_REPLICAS})."
updateSentinelMaster
fi
else
updateSentinelMaster
fi
valkey-sentinel /etc/valkey/sentinel.conf