feat(node): adding all in one node support

This commit is contained in:
2025-05-20 15:52:43 +02:00
parent 3f10373773
commit d655d0449f
22 changed files with 1188 additions and 83 deletions

View File

@ -0,0 +1,60 @@
#!/bin/sh
pingSentinel() {
svc=${VALKEY_SERVICE:-"localhost"}
resp=$(timeout -s 15 $1 \
valkey-cli \
-h ${svc} \
-p $VALKEY_SENTINEL_PORT \
ping)
ret=${?}
echo $resp
return ${ret}
}
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
}
getPrimaryInfo() {
valkey-cli --csv -h ${VALKEY_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}'
}
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
}

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,36 @@
#!/bin/sh
vcli() {
valkey-cli -h 127.0.0.1 -p "${VALKEY_SENTINEL_PORT}" "sentinel" "$@"
return $?
}
getFailOverStatus() {
# Check if the failover is finished
local failoverStatus
primaryInfo=($(vcli 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
}
tmout=120
while getFailOverStatus; do
echo "I'm the primary pod and you are stopping me. Starting sentinel failover"
echo "Waiting for failover to finish..."
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."
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_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 "I'm not the primary, 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_SERVICE} \
-p ${VALKEY_SENTINEL_PORT} \
ping)
ret=${?}
echo $resp
return ${ret}
}
getPrimaryInfo() {
valkey-cli --csv -h ${VALKEY_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,26 @@
#!/bin/sh
PATH=${PATH}:/
SENTINEL_MASTER=""
# Generate sentinel.conf file for Valkey Sentinel
source /opt/scripts/common.sh
if [ -f /etc/valkey/sentinel.conf ]; then
echo "Sentinel configuration file already exists, starting with this."
else
cp /etc/valkey/sentinel.conf.orig /etc/valkey/sentinel.conf
hostname=$(hostname -f)
pingSentinel 1
if [ $? -ne 0 ]; then
SENTINEL_MASTER=$(hostname -f)
else
getPrimaryInfo
SENTINEL_MASTER=$(getSentinelMasterName)
fi
sed -i "s/SENTINEL_MASTER/${SENTINEL_MASTER}/g" /etc/valkey/sentinel.conf
fi
valkey-sentinel /etc/valkey/sentinel.conf

View File

@ -0,0 +1,151 @@
#!/bin/bash
. /opt/bitnami/scripts/libos.sh
. /opt/bitnami/scripts/liblog.sh
. /opt/bitnami/scripts/libvalidations.sh
get_port() {
hostname="$1"
type="$2"
port_var=$(echo "${hostname^^}_SERVICE_PORT_$type" | sed "s/-/_/g")
port=${!port_var}
if [ -z "$port" ]; then
case $type in
"SENTINEL")
echo 26379
;;
"VALKEY")
echo 6379
;;
esac
else
echo $port
fi
}
get_full_hostname() {
hostname="$1"
full_hostname="${hostname}.${HEADLESS_SERVICE}"
echo "${full_hostname}"
}
VALKEYPORT=$(get_port "$HOSTNAME" "VALKEY")
HEADLESS_SERVICE="valkey-headless.mse-dev.svc.cluster.local"
if [ -n "$VALKEY_EXTERNAL_PRIMARY_HOST" ]; then
VALKEY_SERVICE="$VALKEY_EXTERNAL_PRIMARY_HOST"
else
VALKEY_SERVICE="valkey.mse-dev.svc.cluster.local"
fi
SENTINEL_SERVICE_PORT=$(get_port "valkey" "SENTINEL")
validate_quorum() {
if is_boolean_yes "$VALKEY_TLS_ENABLED"; then
quorum_info_command="valkey-cli -h $VALKEY_SERVICE -p $SENTINEL_SERVICE_PORT --tls --cert ${VALKEY_TLS_CERT_FILE} --key ${VALKEY
_TLS_KEY_FILE} --cacert ${VALKEY_TLS_CA_FILE} sentinel primary mymaster"
else
quorum_info_command="valkey-cli -h $VALKEY_SERVICE -p $SENTINEL_SERVICE_PORT sentinel primary mymaster"
fi
info "about to run the command: $quorum_info_command"
eval $quorum_info_command | grep -Fq "s_down"
}
trigger_manual_failover() {
if is_boolean_yes "$VALKEY_TLS_ENABLED"; then
failover_command="valkey-cli -h $VALKEY_SERVICE -p $SENTINEL_SERVICE_PORT --tls --cert ${VALKEY_TLS_CERT_FILE} --key ${VALKEY_TL
S_KEY_FILE} --cacert ${VALKEY_TLS_CA_FILE} sentinel failover mymaster"
else
failover_command="valkey-cli -h $VALKEY_SERVICE -p $SENTINEL_SERVICE_PORT sentinel failover mymaster"
fi
info "about to run the command: $failover_command"
eval $failover_command
}
get_sentinel_primary_info() {
if is_boolean_yes "$VALKEY_TLS_ENABLED"; then
sentinel_info_command="timeout 90 valkey-cli -h $VALKEY_SERVICE -p $SENTINEL_SERVICE_PORT --tls --cert ${VALKEY_TLS_CERT_FILE} -
-key ${VALKEY_TLS_KEY_FILE} --cacert ${VALKEY_TLS_CA_FILE} sentinel get-primary-addr-by-name mymaster"
else
sentinel_info_command="timeout 90 valkey-cli -h $VALKEY_SERVICE -p $SENTINEL_SERVICE_PORT sentinel get-primary-addr-by-name myma
ster"
fi
info "about to run the command: $sentinel_info_command"
retry_while "eval $sentinel_info_command" 2 5
}
[[ -f $VALKEY_PASSWORD_FILE ]] && export VALKEY_PASSWORD="$(< "${VALKEY_PASSWORD_FILE}")"
[[ -f $VALKEY_PRIMARY_PASSWORD_FILE ]] && export VALKEY_PRIMARY_PASSWORD="$(< "${VALKEY_PRIMARY_PASSWORD_FILE}")"
# check if there is a primary
primary_in_persisted_conf="$(get_full_hostname "$HOSTNAME")"
primary_port_in_persisted_conf="$VALKEY_PRIMARY_PORT_NUMBER"
primary_in_sentinel="$(get_sentinel_primary_info)"
valkeyRetVal=$?
if [[ -f /opt/bitnami/valkey-sentinel/etc/sentinel.conf ]]; then
primary_in_persisted_conf="$(awk '/monitor/ {print $4}' /opt/bitnami/valkey-sentinel/etc/sentinel.conf)"
primary_port_in_persisted_conf="$(awk '/monitor/ {print $5}' /opt/bitnami/valkey-sentinel/etc/sentinel.conf)"
info "Found previous primary ${primary_in_persisted_conf}:${primary_port_in_persisted_conf} in /opt/bitnami/valkey-sentinel/etc/sent
inel.conf"
debug "$(cat /opt/bitnami/valkey-sentinel/etc/sentinel.conf | grep monitor)"
fi
if [[ $valkeyRetVal -ne 0 ]]; then
if [[ "$primary_in_persisted_conf" == "$(get_full_hostname "$HOSTNAME")" ]]; then
# Case 1: No active sentinel and in previous sentinel.conf we were the primary --> PRIMARY
info "Configuring the node as primary"
export VALKEY_REPLICATION_MODE="primary"
else
# Case 2: No active sentinel and in previous sentinel.conf we were not primary --> REPLICA
info "Configuring the node as replica"
export VALKEY_REPLICATION_MODE="replica"
VALKEY_PRIMARY_HOST=${primary_in_persisted_conf}
VALKEY_PRIMARY_PORT_NUMBER=${primary_port_in_persisted_conf}
fi
else
# Fetches current primary's host and port
VALKEY_SENTINEL_INFO=($(get_sentinel_primary_info))
info "Current primary: VALKEY_SENTINEL_INFO=(${VALKEY_SENTINEL_INFO[0]},${VALKEY_SENTINEL_INFO[1]})"
VALKEY_PRIMARY_HOST=${VALKEY_SENTINEL_INFO[0]}
VALKEY_PRIMARY_PORT_NUMBER=${VALKEY_SENTINEL_INFO[1]}
if [[ "$VALKEY_PRIMARY_HOST" == "$(get_full_hostname "$HOSTNAME")" ]]; then
# Case 3: Active sentinel and primary it is this node --> PRIMARY
info "Configuring the node as primary"
export VALKEY_REPLICATION_MODE="primary"
else
# Case 4: Active sentinel and primary is not this node --> REPLICA
info "Configuring the node as replica"
export VALKEY_REPLICATION_MODE="replica"
fi
fi
if [[ -n "$VALKEY_EXTERNAL_PRIMARY_HOST" ]]; then
VALKEY_PRIMARY_HOST="$VALKEY_EXTERNAL_PRIMARY_HOST"
VALKEY_PRIMARY_PORT_NUMBER="${VALKEY_EXTERNAL_PRIMARY_PORT}"
fi
if [[ -f /opt/bitnami/valkey/mounted-etc/replica.conf ]];then
cp /opt/bitnami/valkey/mounted-etc/replica.conf /opt/bitnami/valkey/etc/replica.conf
fi
if [[ -f /opt/bitnami/valkey/mounted-etc/valkey.conf ]];then
cp /opt/bitnami/valkey/mounted-etc/valkey.conf /opt/bitnami/valkey/etc/valkey.conf
fi
echo "" >> /opt/bitnami/valkey/etc/replica.conf
echo "replica-announce-port $VALKEYPORT" >> /opt/bitnami/valkey/etc/replica.conf
echo "replica-announce-ip $(get_full_hostname "$HOSTNAME")" >> /opt/bitnami/valkey/etc/replica.conf
ARGS=("--port" "${VALKEY_PORT}")
if [[ "$VALKEY_REPLICATION_MODE" = "replica" ]]; then
ARGS+=("--replicaof" "${VALKEY_PRIMARY_HOST}" "${VALKEY_PRIMARY_PORT_NUMBER}")
fi
ARGS+=("--protected-mode" "no")
ARGS+=("--include" "/opt/bitnami/valkey/etc/replica.conf")
ARGS+=("--include" "/opt/bitnami/valkey/etc/valkey.conf")
exec valkey-server "${ARGS[@]}"